5.TCP通信案例
public class TCPClient {public static void main(String[] args) throws IOException {Socket socket = new Socket( "127.0.0.1",8888);OutputStream os = socket.getOutputStream();os.write("你好服務(wù)器".getBytes());InputStream is = socket.getInputStream();byte[] bytes = new byte[1024];int len = is.read(bytes);System.out.println(new String(bytes,0,len));socket.close();}}
?
服務(wù)端:
public class TCPServer {public static void main(String[] args) throws IOException {ServerSocket server = new ServerSocket(8888);Socket socket =server.accept();InputStream is = socket.getInputStream();byte[] bytes = new byte[1024];int len = is.read(bytes);System.out.println(new String(bytes,0,len));OutputStream os = socket.getOutputStream();os.write("收到謝謝".getBytes());socket.close();server.close();}}
轉(zhuǎn)載于:https://www.cnblogs.com/sdrbg/p/11107140.html
總結(jié)
- 上一篇: 实验十四-团队项目评审课程学习总结
- 下一篇: linux通常使用的 rc 和 .(点)