一个用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. }  
先启动服务端,再启动客户端,在客户端的控制台输入消息并回车,就可以发送消息给客户端了,客户端收到消息后,马上会回复一个消息
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值