博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 多线程Socket编程通讯--实现聊天室代码
阅读量:5312 次
发布时间:2019-06-14

本文共 5161 字,大约阅读时间需要 17 分钟。

1、创建服务器类

1 import java.io.IOException; 2 import java.net.ServerSocket; 3 import java.net.Socket; 4 import java.util.Scanner; 5  6 /** 7  * 服务器 8  * @author Administrator 9  *10  */11 public class Server {12 13     private int duankou = 9000;//端口号14     private ServerSocket server;//声明服务器15     private static Socket socket;//声明客户端16     private String serName;17     public Server(){18         try {19             init();20         } catch (IOException e) {21             e.printStackTrace();22         }23     }24     25     /*26      * 创建服务器,开始监听27      */28     private void init() throws IOException{29         server = new ServerSocket(duankou);30         System.out.println("------服务器已开启--------");31         System.out.println("请输入服务器名字:");32         Scanner sc = new Scanner(System.in);33         serName = sc.next();34         while(true){35             socket = server.accept();36             hands(socket);37         }38     }39 40 41     private void hands(Socket socket) {42         String key = socket.getInetAddress().getHostAddress()+":"+socket.getPort();43         System.out.println("监听到的客户端:"+key);44         Thread thread = new Thread(new ThreadSocket(socket));45         thread.setName(serName);46         thread.start();47     }48     49     public static void main(String[] args) {50         Server server = new Server();51     }52     53 }

2、创建客户端类

1 import java.io.IOException; 2 import java.net.Socket; 3 import java.net.UnknownHostException; 4 import java.util.Scanner; 5  6 /** 7  * 客户端 8  * @author Administrator 9  *10  */11 public class Client {12     13     private int port = 9000;14     private String ip="127.0.0.1";15     private static Socket socket;16     private String cliName;17     public Client(){18         try {19             init();20         } catch (Exception e) {21             e.printStackTrace();22         } 23     }24 25     private void init() throws Exception {26 27         System.out.println("-----客户端已开启-----");28         System.out.println("请输入客户端名字:");29         Scanner sc = new Scanner(System.in);30         cliName = sc.next();31         socket = new Socket(ip,port);32     }33     34     public void hands() throws Exception{35         Thread threadReader = new Thread(new ThreadReader(socket.getInputStream()),Thread.currentThread().getName());36         Thread threadWriter = new Thread(new ThreadWriter(socket.getOutputStream()));37         threadWriter.setName(cliName);38         threadReader.start();39         threadWriter.start();40         41     }42     43     public static void main(String[] args) throws Exception  {44         Client client = new Client();45         client.hands();46     }47 48 }

 

 3、服务器处理socket多线程

1 import java.io.IOException; 2 import java.net.Socket; 3  4 /** 5  * 服务器处理Socket线程 6  * @author Administrator 7  * 8  */ 9 public class ThreadSocket implements Runnable{10 11     private Socket socket;12     13     public ThreadSocket(Socket socket){14         this.socket = socket;15     }16     17     @Override18     public void run() {19 20         try {21             Thread threadReader = new Thread(new ThreadReader(socket.getInputStream()));22             Thread threadWriter = new Thread(new ThreadWriter(socket.getOutputStream()));23             threadReader.start();24             threadWriter.start();25         26         } catch (IOException e) {27             e.printStackTrace();28         }29         30     }31 32 }

 

4、读取输入流多线程

1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5  6 /** 7  * 读取输入流线程 8  * @author Administrator 9  *10  */11 public class ThreadReader implements Runnable{12 13     //private static int HEAD_SIZE=5;//传输最大字节长度14     //private static int BUFFER_SIZE=10;//每次读取10个字节15     private InputStream is;16     public ThreadReader(InputStream is) {17         this.is = is;18     }19 20     @Override21     public void run() {22 23         try {24             while(true){25                 byte[] b = new byte[1024];26                 int length = is.read(b);27                 String message = new String(b,0,length);28                 System.out.println(Thread.currentThread().getName()+":"+message);29             }30         } catch (IOException e) {31             e.printStackTrace();32         }33         34     }35 36 }

 

5、发送数据多线程

1 import java.io.IOException; 2 import java.io.OutputStream; 3 import java.util.Scanner; 4  5 /** 6  * 发送数据线程 7  * @author Administrator 8  * 9  */10 public class ThreadWriter implements Runnable{11 12     13     private OutputStream os;14     public ThreadWriter(OutputStream os) {15         this.os = os;16     }17 18     @Override19     public void run() {20 21         try {22             Scanner sc = new Scanner(System.in);23             while(true){24             System.out.println(Thread.currentThread().getName()+"说:");25             String message = sc.next();26             os.write(message.getBytes());27             os.flush();28             }29         } catch (IOException e) {30             e.printStackTrace();31         }32         33     }34 35 }

 

转载于:https://www.cnblogs.com/jpwz/p/5705160.html

你可能感兴趣的文章
Node.js 入门:Express + Mongoose 基础使用
查看>>
一步步教你轻松学奇异值分解SVD降维算法
查看>>
使用pager进行分页
查看>>
UVA - 1592 Database
查看>>
Fine Uploader文件上传组件
查看>>
javascript中的传递参数
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
consonant combination
查看>>
驱动的本质
查看>>
Swift的高级分享 - Swift中的逻辑控制器
查看>>
Swagger简单介绍
查看>>
Python数据分析入门案例
查看>>
vue-devtools 获取到 vuex store 和 Vue 实例的?
查看>>
Linux 中【./】和【/】和【.】之间有什么区别?
查看>>
内存地址对齐
查看>>
看门狗 (监控芯片)
查看>>
css背景样式
查看>>
JavaScript介绍
查看>>
开源网络漏洞扫描软件
查看>>