导航

TCPSocket ---- TCPServer & TCPClient .java

Posted on 2013-04-14 20:41  ChanHuman  阅读(374)  评论(0编辑  收藏  举报

TCPServer:

 

 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(8888); 
 7         Socket sk = ss.accept();
 8         System.out.println("Client Connected!");
 9         InputStream is =sk.getInputStream(); 
10         DataInputStream dis = new DataInputStream(is);
11         String s = dis.readUTF();
12         System.out.println(s); 
13     }
14 }

 

TCPClient:

 

 1 import java.net.*;
 2 import java.io.*;
 3 
 4 public class TCPClient{
 5     public static void main(String[] args) throws Exception{
 6         Socket cl = new Socket("127.1.1.0", 8888); 
 7         OutputStream os =cl.getOutputStream(); 
 8         DataOutputStream dos = new DataOutputStream(os);
 9         dos.writeUTF("I LOVE U!!");
10     }
11 }