对于一个功能齐全的Socket,都要包含以下基本结构,其工作过程包含以下四个基本的步骤:

  1. 创建Socket;
  2. 打开连接到Socket的输入/出流;
  3. 按照一定的协议对Socket进行读/写操作;
  4. 关闭Socket。

 

基于TCP的socket实现

SocketClient.java

1 public class SocketClient {
 2     
 3     public static void main(String[] args) throws InterruptedException {
 4         try {
 5             // 和服务器创建连接
 6             Socket socket = new Socket("localhost",8088);
 7             
 8             // 要发送给服务器的信息
 9             OutputStream os = socket.getOutputStream();
10             PrintWriter pw = new PrintWriter(os);
11             pw.write("Hello Server");
12             pw.flush();
13             // 关闭输出流
14             socket.shutdownOutput();
15             
16             // 从服务器接收的信息
17             InputStream is = socket.getInputStream();
18             BufferedReader br = new BufferedReader(new InputStreamReader(is));
19             String info = null;
20             while((info = br.readLine())!=null){
21                 System.out.println("服务器消息:"+info);
22             }
23             
24             br.close();
25             is.close();
26             os.close();
27             pw.close();
28             socket.close();
29         } catch (Exception e) {
30             e.printStackTrace();
31         }
32     }
33  
34 }

 

SocketServer.java

1 public class SocketServer {
 2     
 3     public static void main(String[] args) {
 4         try {
 5             // 创建服务端socket
 6             ServerSocket serverSocket = new ServerSocket(6300);        
 7             // 创建客户端socket
 8             Socket socket = new Socket();    
 9             //循环监听等待客户端的连接
10             while(true){
11                 // 监听客户端
12                 socket = serverSocket.accept();    
13                 ServerThread thread = new ServerThread(socket);
14                 thread.start();
15             }
16         } catch (Exception e) {
17             
18             e.printStackTrace();
19         }
20     }
21  
22 }

 

ServerThread.java

1 public class ServerThread extends Thread{
 2     
 3     private Socket socket = null;
 4     
 5     public ServerThread(Socket socket) {
 6         this.socket = socket;
 7     }
 8  
 9     @Override
10     public void run() {
11         InputStream is=null;
12         InputStreamReader isr=null;
13         BufferedReader br=null;
14         OutputStream os=null;
15         PrintWriter pw=null;
16         try {
17             is = socket.getInputStream();
18             isr = new InputStreamReader(is);
19             br = new BufferedReader(isr);
20             String info = null;
21             while((info=br.readLine())!=null){
22                 System.out.println("客户端消息:" + info);
23             }
24             // 关闭输入流
25             socket.shutdownInput();
26        // 写入数据到客户端
27             os = socket.getOutputStream();
28             pw = new PrintWriter(os);
29             pw.write("Hello  Client");
30             pw.flush();
31         } catch (Exception e) {
32             e.printStackTrace();
33         } finally{
34             //关闭资源
35             try {
36                 if(pw!=null)
37                     pw.close();
38                 if(os!=null)
39                     os.close();
40                 if(br!=null)
41                     br.close();
42                 if(isr!=null)
43                     isr.close();
44                 if(is!=null)
45                     is.close();
46                 if(socket!=null)
47                     socket.close();
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }
51         }
52     }
53  
54 }
55