Java 多线程Socket编程通讯--实现聊天室代码

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     @Override
18     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     @Override
21     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     @Override
19     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 }

 

posted @ 2016-07-25 20:00  Web1024  阅读(18141)  评论(2编辑  收藏  举报