ServerSoket/Socket聊天简单实现V3(多线程,多个客户端对一个服务器,客户端可接收可发送,服务器可接收可发送【发送后需等到再次接收到消息才能再次发送】)

41 篇文章 0 订阅
23 篇文章 0 订阅

知识点:每当服务器端接收到一个客户端连接,就会另起一个新的线程进行处理,这样就不会因为某个客户端处理的阻塞,而导致其他客户端得不到处理。
待优化:新建线程比较耗费资源,考虑使用线程池做初步优化,减少因频繁创建线程带来的开销
源码如下:分服务器Server/客户端Client/工具类Util三个类。

package communication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Dylaniou
 * 需要先启动服务器
 */
public class Server {
	static ServerSocket serverSocket = null;
	static int port = 18000;
	static Socket socket = null;
	static int clientID = 0;
	public static void main(String[] args){
		try {
			serverSocket = new ServerSocket(port);
		} catch (IOException e) {
			Util.printlnfo("IO异常,服务器启动失败");
			e.printStackTrace();
		}
		Util.printlnfo("服务器启动成功,等待客户端连接");
		while(true){
			try {
				socket = serverSocket.accept();
			} catch (IOException e) {
				Util.printlnfo("IO异常,等待客户端连接失败");
				e.printStackTrace();
			}
			++clientID;
			Util.printlnfo("收到客户端连接" );
			
			
			new Thread(new ServerDeal(socket,clientID)).start();
			/*DataOutputStream dos = null;
			BufferedReader br = null;
			BufferedReader br2 = null;
			BufferedWriter bw = null;
			
			try {
				dos = new DataOutputStream(socket.getOutputStream());
				dos.writeInt(clientID);//告知客户端,服务器为其分配的ID
				dos.flush();
				Util.printlnfo("发送为客户端分配的ID:"+ clientID + ",准备接收客户端消息" );
				br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				br2 = new BufferedReader(new InputStreamReader(System.in));
				bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				String line = null;
				while((line = br.readLine())!=null){
					Util.printlnfo("客户端" + clientID + ":" + line+ ",服务器请回答~");
					line = br2.readLine();
					bw.write(line);
					bw.newLine();
					bw.flush();
					Util.printlnfo("服务器:" + line+ ",客户端请回答~");
					line = null;
				}
			} catch (IOException e) {
				Util.printlnfo("读写时出现IO异常");
				e.printStackTrace();
			}finally {
				try {
					if(dos!=null) dos.close();
					if(br2!=null) br2.close();
					if(br2!=null) bw.close();
					if(br2!=null) br.close();
				} catch (IOException e) {
					Util.printlnfo("关闭时出现IO异常");
					e.printStackTrace();
				}
			}*/
		}
	}
}
class ServerDeal extends Thread{
	Socket socket;
	int clientID;
	ServerDeal(Socket socket,int clientID){
		this.socket = socket;
		this.clientID = clientID;
	}
	public void run(){
		DataOutputStream dos = null;
		BufferedReader br = null;
		BufferedReader br2 = null;
		BufferedWriter bw = null;
		
		try {
			dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(clientID);//告知客户端,服务器为其分配的ID
			dos.flush();
			Util.printlnfo("发送为客户端分配的ID:"+ clientID + ",准备接收客户端消息" );
			br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			br2 = new BufferedReader(new InputStreamReader(System.in));
			bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			String line = null;
			while((line = br.readLine())!=null){
				Util.printlnfo("客户端" + clientID + ":" + line+ ",服务器请回答~");
				line = br2.readLine();
				bw.write(line);
				bw.newLine();
				bw.flush();
				Util.printlnfo("服务器:" + line+ ",客户端请回答~");
				line = null;
			}
		} catch (IOException e) {
			Util.printlnfo("读写时出现IO异常");
			e.printStackTrace();
		}finally {
			try {
				if(dos!=null) dos.close();
				if(br2!=null) br2.close();
				if(br2!=null) bw.close();
				if(br2!=null) br.close();
			} catch (IOException e) {
				Util.printlnfo("关闭时出现IO异常");
				e.printStackTrace();
			}
		}
	}
}
package communication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author Dylaniou
 * 需要先启动服务器
 */
public class Client {
	static Socket socket = null;
	static int port = 18000;
	static int clientID = 0;
	public static void main(String[] args) {
		try {
			socket = new Socket("localhost",port);
		} catch (UnknownHostException e) {
			Util.printlnfo("客户端与服务器建立socket连接失败:无法识别的主机");
			e.printStackTrace();
		} catch (IOException e) {
			Util.printlnfo("客户端与服务器建立socket连接失败:IO异常");
			e.printStackTrace();
		}
		Util.printlnfo("客户端与服务器成功建立socket连接,准备接收服务器为其分配的ID");
		
		DataInputStream dis = null;
		BufferedReader br = null;
		BufferedReader br2 = null;
		BufferedWriter bw = null;
		
		try {
			//获取服务器分配的ID
			dis = new DataInputStream(socket.getInputStream());
			clientID = dis.readInt();
			Util.printlnfo("成功接收到服务器为其分配的ID:" + clientID + ",准备发送消息");
			//发送命令行消息给服务器
			//将命令行字节输入流转换为字符输入流,利用缓冲字符输入流读取命令行消息
			br = new BufferedReader(new InputStreamReader(System.in));
			//将socket字节输出流转换为字符输出流,利用缓冲字符输出流输出命令行消息
			bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
			br2 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			String line =null;
			while((line = br.readLine())!=null){
				Util.printlnfo( "客户端" + clientID + ":" + line+",服务器请回答~");
				bw.write(line);
				bw.newLine();//用于终止输出,不然命令行输入再多内容也无法发送出去
				bw.flush();
				Util.printlnfo("服务器:" + br2.readLine() + ",客户端"+ clientID + "请回答~");
				line = null;
			}
		} catch (IOException e) {
			Util.printlnfo("读写时出现IO异常");
			e.printStackTrace();
		}finally {
			try {
				if(dis!=null) dis.close();
				if(br2!=null) br2.close();
				if(br2!=null) bw.close();
				if(br2!=null) br.close();
			} catch (IOException e) {
				Util.printlnfo("关闭时出现IO异常");
				e.printStackTrace();
			}
		}
	}
}
package communication;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Util {
	public static String getCurrentTime(){
		Date date=new Date(); 
		 //这个方法也是需要导包的 
		//注意第二个mm要大写,不然月份会有错误 
		SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd hh:mm:ss:SSS"); 
		return sdf.format(date);
	}
	public static void printlnfo(String info){
		System.out.println(getCurrentTime()+":"+info);
	}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dylanioucn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值