一個用Java寫的簡單的TCP聊天程序

原文地址:http://blog.csdn.net/yubo_725/article/details/45331487   感謝作者分享。


服務端代碼:

[java] view plain copy
 print?
  1. package com.test.server;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6. import java.net.ServerSocket;  
  7. import java.net.Socket;  
  8.   
  9. public class Server {  
  10.       
  11.     public static void main(String[] args) {  
  12.         new Server().startServer();  
  13.     }  
  14.       
  15.     public void startServer(){  
  16.         try {  
  17.             //服務器在9990端口監聽客戶端的連接  
  18.             ServerSocket ss = new ServerSocket(9999);  
  19.             System.out.println("server is listening...");  
  20.             while(true){  
  21.                 //阻塞的accept方法,當一個客戶端連接上,纔會返回Socket對象  
  22.                 Socket s = ss.accept();  
  23.                 System.out.println("a client has connected!");  
  24.                 //開啓線程處理通信  
  25.                 new CommunicateThread(s).start();  
  26.             }  
  27.         } catch (IOException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.       
  32.     public class CommunicateThread extends Thread{  
  33.           
  34.         Socket socket;  
  35.         DataInputStream dis;  
  36.         DataOutputStream dos;  
  37.           
  38.         public CommunicateThread(Socket socket){  
  39.             this.socket = socket;  
  40.             try {  
  41.                 dis = new DataInputStream(socket.getInputStream());  
  42.                 dos = new DataOutputStream(socket.getOutputStream());  
  43.             } catch (IOException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.         }  
  47.           
  48.         @Override  
  49.         public void run() {  
  50.             super.run();  
  51.             //讀取客戶端發過來的消息  
  52.             String msg = null;  
  53.             try {  
  54.                 while((msg = dis.readUTF()) != null){  
  55.                     System.out.println("client send msg :" + msg);  
  56.                     String replyMsg = "server reply : " + msg;  
  57.                     dos.writeUTF(replyMsg);  
  58.                     System.out.println("server reply msg :" + replyMsg);  
  59.                 }  
  60.             } catch (IOException e) {  
  61.                 e.printStackTrace();  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66. }  
客戶端代碼:

[java] view plain copy
 print?
  1. package com.test.client;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6. import java.net.Socket;  
  7. import java.util.Scanner;  
  8.   
  9. public class Client {  
  10.       
  11.     public static void main(String[] args) {  
  12.         new Client().startClient();  
  13.     }  
  14.       
  15.     public void startClient(){  
  16.         try {  
  17.             //連接到服務器  
  18.             Socket socket = new Socket("localhost"9999);  
  19.             DataInputStream dis = new DataInputStream(socket.getInputStream());  
  20.             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());  
  21.             Scanner scanner = new Scanner(System.in);  
  22.             String line = null;  
  23.             listenServerReply(dis);  
  24.             while((line = scanner.nextLine()) != null){//讀取從鍵盤輸入的一行  
  25.                 dos.writeUTF(line);//發給服務端  
  26.                 System.out.println("client send msg : " + line);  
  27.             }  
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32.       
  33.     //監聽服務端回覆的消息  
  34.     public void listenServerReply(final DataInputStream dis){  
  35.         new Thread(){  
  36.             @Override  
  37.             public void run() {  
  38.                 super.run();  
  39.                 String line = null;  
  40.                 try {  
  41.                     while((line = dis.readUTF()) != null){  
  42.                         System.out.println("client receive msg from server: " + line);  
  43.                     }  
  44.                 } catch (IOException e) {  
  45.                     e.printStackTrace();  
  46.                 }  
  47.             }  
  48.         }.start();  
  49.     }  
  50.   
  51. }  
先啓動服務端,再啓動客戶端,在客戶端的控制檯輸入消息並回車,就可以發送消息給客戶端了,客戶端收到消息後,馬上會回覆一個消息
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章