标签:
1 import java.net.*; 2 import java.io.*; 3 4 public class TCPServer { 5 public static void main(String[] args) throws Exception { 6 ServerSocket ss = new ServerSocket(6666); 7 while(true) { 8 Socket s = ss.accept(); 9 System.out.println("a client connect!"); 10 DataInputStream dis = new DataInputStream(s.getInputStream()); 11 System.out.println(dis.readUTF()); 12 Thread.sleep(3000); 13 System.out.println("hello client!"); 14 dis.close(); 15 s.close(); 16 } 17 18 } 19 }
1 import java.net.*; 2 import java.io.*; 3 4 public class TCPClient { 5 public static void main(String[] args) throws Exception { 6 Socket s = new Socket("127.0.0.1", 6666); 7 OutputStream os = s.getOutputStream(); 8 DataOutputStream dos = new DataOutputStream(os); 9 Thread.sleep(3000); 10 dos.writeUTF("hello server!"); 11 dos.flush(); 12 dos.close(); 13 s.close(); 14 } 15 }
标签:
原文地址:http://www.cnblogs.com/roger-h/p/4506908.html