Java实现TCP / IP协议下的聊天室(运用多线程)
分为了6个文件,总共分为两个大块:其中一个为Client客户端,另一个是Server服务器端,每一个大块里分别有主运行块以及发送和接受模块,然后用多线程来实现实时的收和发
Client类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package chatApps;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class Client { private String IP; private int host; public static void main (String[] args) { Client client = new Client("10.120.203.132" ,12345 ); client.go(); } public Client (String IP,int host) { this .IP = IP; this .host = host; } public void go () { try { Socket client = new Socket(IP,host); new Thread(new CReceive(client)).start(); new Thread(new CSend(client)).start(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
CReceive类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package chatApps;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class CReceive implements Runnable { private Socket client = null ; public CReceive (Socket client) { this .client = client; } @Override public void run () { try { BufferedReader buffer = new BufferedReader(new InputStreamReader(client.getInputStream())); String str; while ((str = buffer.readLine())!=null ){ System.out.println("The server:" +str); } } catch (IOException e) { e.printStackTrace(); } } }
CSend类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 在这里插入代码片package chatApps;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class CSend implements Runnable { private Socket client = null ; public CSend (Socket client) { this .client = client; } @Override public void run () { BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); try { PrintStream output = new PrintStream(client.getOutputStream()); String str; while ((str = input.readLine())!=null ){ output.println(str); } } catch (IOException e) { e.printStackTrace(); } } }
Server类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 package chatApps;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class Server { int host; public static void main (String[] args) { Server server1 = new Server(12345 ); try { server1.go(); } catch (Exception e) { e.printStackTrace(); } } public Server (int host) { this .host = host; } public void go () throws Exception { Socket client = null ; ServerSocket server = new ServerSocket(12345 ); System.out.println("Waiting for connection..." ); while (true ){ client = server.accept(); System.out.println("The connection has been established." ); new Thread(new SReceive(client)).start(); new Thread(new SSend(client)).start(); } } }
SReceive类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package chatApps;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class SReceive implements Runnable { private Socket client = null ; public SReceive (Socket client) { this .client = client; } @Override public void run () { try { BufferedReader buffer = new BufferedReader(new InputStreamReader(client.getInputStream())); String str; while ((str = buffer.readLine())!=null ){ System.out.println("The client:" +str); } } catch (IOException e) { e.printStackTrace(); } } }
SSend类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package chatApps;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketTimeoutException;import java.net.UnknownHostException;public class SSend implements Runnable { private Socket client; public SSend (Socket client) { this .client = client; } @Override public void run () { BufferedReader intput = new BufferedReader(new InputStreamReader(System.in)); try { PrintStream output = new PrintStream(client.getOutputStream()); output.println("The connection has been established." ); String str; while ((str = intput.readLine())!=null ){ output.println(str); } } catch (IOException e) { e.printStackTrace(); } } }
最终所能实现的效果就是可以服务器端与客户端之间相互交流,当然客户端所选的IP地址一定要是服务器所在的IP地址,并且服务器与客户端所选的端口必须相同