标签:
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 }
标签:
原文地址:http://www.cnblogs.com/hellochennan/p/5373198.html