Java:编写一个网络通信程序,要求:编写服务器端程序

该博客介绍了如何使用Java编写一个网络通信服务器端程序,处理多个客户端连接。服务器端监听3600端口,接收来自192.168.168.10的客户端连接。当客户端发送'QUIT'时,服务器会关闭。程序使用ExecutorService线程池来管理客户端连接,每个连接在一个独立的线程中处理,接收到的数据会被广播回所有在线客户端。

题目:

    编写一个网络通信程序,要求:编写服务器端程序(客户端程序不做要求,可以做为加分)。

  (1)端口号:服务器端、客户端均采用3600;

  (2)服务器端的IP地址为192.168.168.10;

  (3)当“客户端”输入“QUIT”后,服务器端将自动关闭;

  (4)服务器端将接收到的数据返回客户端。

package com.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main 
    {
	private static final int PORT = 3600;
	private List<Socket> mList = new ArrayList<Socket>();
	private ServerSocket server = null;
	private ExecutorService mExecutorService = null; // thread pool
	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		try {
			server = new ServerSocket(PORT);
			mExecutorService = Executors.newCachedThreadPool(); // create a
																// thread pool
			System.out.print("server start ...");
			Socket client = null;
			while (true) {
				client = server.accept();
				mList.add(client);
				mExecutorService.execute(new Service(client)); // start a new
																// thread to
																// handle the
																// connection
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class Service implements Runnable {
		private Socket socket;
		private BufferedReader in = null;
		private String msg = "";

		public Service(Socket socket) {
			this.socket = socket;
			try {
				in = new BufferedReader(new InputStreamReader(
						socket.getInputStream()));
				msg = "user" + this.socket.getInetAddress() + "come toal:"
						+ mList.size();
				this.sendmsg();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				while (true) {
					if ((msg = in.readLine()) != null) {
						if (msg.equals("exit")) {
							System.out.println("ssssssss");
							mList.remove(socket);
							in.close();
							msg = "user:" + socket.getInetAddress()
									+ "exit total:" + mList.size();
							socket.close();
							this.sendmsg();
							break;
						} else {
							msg = socket.getInetAddress() + ":" + msg;
							this.sendmsg();
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		public void sendmsg() {
			System.out.println(msg);
			int num = mList.size();
			for (int index = 0; index < num; index++) {
				Socket mSocket = mList.get(index);
				PrintWriter pout = null;
				try {
					pout = new PrintWriter(new BufferedWriter(
							new OutputStreamWriter(mSocket.getOutputStream())),
							true);
					pout.println(msg);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是蟹老板

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

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

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

打赏作者

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

抵扣说明:

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

余额充值