1 //聊天软件项目TCP升级版
 2 import java.io.*;
 3 import java.net.*;
 4 class TcpClient2 
 5 {
 6     public static void main(String[] args)throws Exception 
 7     {
 8         Socket s = new Socket("192.168.1.254",10004);
 9         OutputStream out = s.getOutputStream();
10         out.write("服务端,你好".getBytes());
11         InputStream in = s.getInputStream();
12         byte[] buf = new byte[1024];
13         int len = in.read(buf);
14         System.out.println(new String(buf,0,len));
15         s.close();
16     }
17 }
18 
19 
20 class TcpServer2
21 {
22     public static void main(String[] args) throws Exception
23     {
24         ServerSocket ss = new ServerSocket(10004);
25         Socket s = ss.accept();
26         String ip = s.getInetAddress().getHostAddress();
27         System.out.println(ip+"....connected");
28         InputStream in = s.getInputStream();
29         byte[] buf = new byte[1024];
30         int len = in.read(buf);
31         System.out.println(new String(buf,0,len));
32         OutputStream out = s.getOutputStream();
33         Thread.sleep(10000);
34         out.write("哥们收到,你也好".getBytes());
35         s.close();
36         ss.close();
37     }
38 }

 

相关文章:

  • 2021-08-26
  • 2021-06-09
猜你喜欢
  • 2022-02-15
  • 2021-05-27
  • 2021-12-27
  • 2021-10-06
  • 2021-09-22
  • 2021-09-07
  • 2021-11-21
相关资源
相似解决方案