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

时间:2021-02-24 14:51:38

1、创建服务器类

 import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner; /**
* 服务器
* @author Administrator
*
*/
public class Server { private int duankou = 9000;//端口号
private ServerSocket server;//声明服务器
private static Socket socket;//声明客户端
private String serName;
public Server(){
try {
init();
} catch (IOException e) {
e.printStackTrace();
}
} /*
* 创建服务器,开始监听
*/
private void init() throws IOException{
server = new ServerSocket(duankou);
System.out.println("------服务器已开启--------");
System.out.println("请输入服务器名字:");
Scanner sc = new Scanner(System.in);
serName = sc.next();
while(true){
socket = server.accept();
hands(socket);
}
} private void hands(Socket socket) {
String key = socket.getInetAddress().getHostAddress()+":"+socket.getPort();
System.out.println("监听到的客户端:"+key);
Thread thread = new Thread(new ThreadSocket(socket));
thread.setName(serName);
thread.start();
} public static void main(String[] args) {
Server server = new Server();
} }

2、创建客户端类

 import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner; /**
* 客户端
* @author Administrator
*
*/
public class Client { private int port = 9000;
private String ip="127.0.0.1";
private static Socket socket;
private String cliName;
public Client(){
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
} private void init() throws Exception { System.out.println("-----客户端已开启-----");
System.out.println("请输入客户端名字:");
Scanner sc = new Scanner(System.in);
cliName = sc.next();
socket = new Socket(ip,port);
} public void hands() throws Exception{
Thread threadReader = new Thread(new ThreadReader(socket.getInputStream()),Thread.currentThread().getName());
Thread threadWriter = new Thread(new ThreadWriter(socket.getOutputStream()));
threadWriter.setName(cliName);
threadReader.start();
threadWriter.start(); } public static void main(String[] args) throws Exception {
Client client = new Client();
client.hands();
} }

3、服务器处理socket多线程

 import java.io.IOException;
import java.net.Socket; /**
* 服务器处理Socket线程
* @author Administrator
*
*/
public class ThreadSocket implements Runnable{ private Socket socket; public ThreadSocket(Socket socket){
this.socket = socket;
} @Override
public void run() { try {
Thread threadReader = new Thread(new ThreadReader(socket.getInputStream()));
Thread threadWriter = new Thread(new ThreadWriter(socket.getOutputStream()));
threadReader.start();
threadWriter.start(); } catch (IOException e) {
e.printStackTrace();
} } }

4、读取输入流多线程

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; /**
* 读取输入流线程
* @author Administrator
*
*/
public class ThreadReader implements Runnable{ //private static int HEAD_SIZE=5;//传输最大字节长度
//private static int BUFFER_SIZE=10;//每次读取10个字节
private InputStream is;
public ThreadReader(InputStream is) {
this.is = is;
} @Override
public void run() { try {
while(true){
byte[] b = new byte[1024];
int length = is.read(b);
String message = new String(b,0,length);
System.out.println(Thread.currentThread().getName()+":"+message);
}
} catch (IOException e) {
e.printStackTrace();
} } }

5、发送数据多线程

 import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner; /**
* 发送数据线程
* @author Administrator
*
*/
public class ThreadWriter implements Runnable{ private OutputStream os;
public ThreadWriter(OutputStream os) {
this.os = os;
} @Override
public void run() { try {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println(Thread.currentThread().getName()+"说:");
String message = sc.next();
os.write(message.getBytes());
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
} } }