//服务端
import com.hl.bluetooth.util.CRC16; import com.hl.bluetooth.util.FrameCheckFailedException; import com.hl.bluetooth.util.stream.StreamUtils; import com.hl.bluetooth.util.util; import lombok.SneakyThrows; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; import java.util.Arrays; /** * @author hwx * @date 2021/11/4 */ public class TcpServer extends ServerSocket { private static final Logger logger = LoggerFactory.getLogger(TcpServer.class); public TcpServer(int serverPort) throws IOException { super(serverPort); try { while (true) { Socket socket = accept(); new ServerThread(socket); } } catch (Exception e) { logger.error("tcp服务器已停止监听"); } finally { close(); } } class ServerThread extends Thread /*建立服务端线程*/ { private Socket socket; private DataInputStream inputStream; private InputStream in; private InputStream i; private byte[] bytes; private DataOutputStream out; public ServerThread(Socket s) throws Exception { logger.info("tcp服务器开始监听......"); this.socket = s; in = socket.getInputStream(); inputStream = new DataInputStream(in); /* in = new BufferedReader(new InputStreamReader( socket.getInputStream(), "GB2312")); *//*国标码*/ out = new DataOutputStream(socket.getOutputStream()); String str = s.getInetAddress().getHostAddress(); StreamUtils.setOutputStream(s.getInetAddress().getHostAddress(), new DataOutputStream(socket.getOutputStream())); StreamUtils.setInputStream(s.getInetAddress().getHostAddress(), new DataInputStream(socket.getInputStream())); // 开始线程 start(); } @SneakyThrows @Override public void run() { try {
Scanner scanner = new Scanner(System.in);// 装饰标准输入流,用于从控制台输入 while (true) {
String send = scanner.nextLine();//读取控制台输入的内容
System.out.println("客户端:" + send);//输出键盘输出内容提示 ,也就是客户端向服务器端发送的消息
// 把从控制台得到的信息传送给服务器
out.writeUTF("客户端:" + send);//将客户端的信息传递给服务器
String accpet = in.readUTF();// 读取来自服务器的信息
System.out.println(accpet);//输出来自服务器的信息
} } catch (Exception e) { logger.error(socket.getInetAddress().getHostAddress() + "连接断开-----" + e.getMessage(), e); } finally { DataOutputStream outputStream = StreamUtils.getOutputStream(socket.getInetAddress().getHostAddress()); outputStream.close(); inputStream.close(); socket.close(); } }
//客户端
import java.io.*; import java.net.Socket; import java.net.UnknownHostException; /** * @author hwx * @date 2021/11/4 */ public class TcpClient { /** * 定义socket连接 */ private Socket socket; private DataInputStream inputStream; /** * 打印到文本输出流 */ private PrintWriter writer; private OutputStream outputStream; public TcpClient(int serverPort) { /*定义客户端端口号*/ try { /*抛异常*/ socket = new Socket("192.168.153.202", serverPort); inputStream = new DataInputStream( /*标准输入输出流*/ socket.getInputStream()); writer = new PrintWriter(socket.getOutputStream()); outputStream = socket.getOutputStream(); while (true) { BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); String message = in.readLine();if ("finish".equals(message)) { System.out.println("服务器已停止监听"); break; } } outputStream.close(); writer.close(); inputStream.close(); /*关闭流*/ socket.close(); } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); /*在命令行打印异常信息在程序中出错的位置及原因*/ } } }