tcp socket实例(改进)

上一篇的程序服务器端只能接受一个客户端请求,本篇改为模拟多客户端程序
客户端:
package socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class MultiClient {

	public static void main(String[] args) {

		MultiClient mc = new MultiClient();
		mc.sendSubsequentRequest();
	}

	private void sendSubsequentRequest() {

		for(int i=0;i<100;i++){ //模拟多个并发用户向客户端同时发送请求,其实在一台机子上还是有先后顺序的
			new Thread(new RequestSender()).start();
		}
	}

}

class RequestSender implements Runnable {

	@Override
	public void run() {
		try {
			callServer();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void callServer() throws UnknownHostException, IOException {

		int reqInt = 23;
		Socket s = new Socket("localhost", 8888); // 客户端给出IP和端口号

		InputStream is = s.getInputStream();
		OutputStream os = s.getOutputStream();
		DataInputStream dis = new DataInputStream(is);
		DataOutputStream dos = new DataOutputStream(os);

		dos.writeInt(reqInt);
		dos.flush();
		int serverInt = dis.readInt();
		System.out.println("server answer " + reqInt + "*18 = " + serverInt);
		dis.close();
		dos.close();

	}
}


服务器端:
只是加了一个while循环
package socket;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//简单的处理多客户端
public class MultiServer {
	public MultiServer() {

	}

	public static void main(String[] args) {
		MultiServer ms = new MultiServer();
		try {
			ms.listen(8888);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void listen(int port) throws IOException {

		ServerSocket server = new ServerSocket(port);
		while (true) {
			Socket socket = server.accept();
			InputStream is = socket.getInputStream();
			OutputStream os = socket.getOutputStream();
			DataInputStream dis = new DataInputStream(is);
			DataOutputStream dos = new DataOutputStream(os);
			int receiveInt = dis.readInt(); // 读取客户端请求
			System.out.println("The message from client is:" + receiveInt);
			dos.writeInt(receiveInt * 18); // 返回client端
			dos.flush(); // 强制清空缓冲区
			dos.close();
			dis.close();
			is.close();

		}
	}
}

简单的多客户端处理,加一个死循环即可,但如此服务器会阻塞用户的请求,即是服务器在没有处理完当前用户请求的情况下,是不能处理其他用户请求的,见改进的ModifiedServer

改进的server
package socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ModifiedServer {
	
	/*
	 *处理客户端请求的线程类
	 *////////////////////////////////////////////
		class RequestHandler implements Runnable {  //处理客户端请求的类定义在服务器端类主体的内部
			private Socket socket = null;

			public RequestHandler(Socket s) {
				this.socket = s;
			}

			@Override
			public void run() {
				processRequest();
			}

			private void processRequest() {

				try {
					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();
					DataInputStream dis = new DataInputStream(is);
					DataOutputStream dos = new DataOutputStream(os);

					int receiveInt = dis.readInt(); // 读取客户端请求
					System.out.println("The message from client" +clientCount+ "is:"
							+ receiveInt);
					dos.writeInt(receiveInt * 18); // 返回client端
					dos.flush(); // 强制清空缓冲区
					dos.close();
					dis.close();
					is.close();

					delay(200l);   //模拟业务处理延时
					clientCount--; //处理完成,减少client端的数量
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			private void delay(long l) {

				try {
					Thread.sleep(l);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

		}
	////////////////////////////////////////////////////
	
	int clientCount = 0;   //客户端并发请求动态数目
	static int port = 8888;
	int maxClientCount = 200;

	public static void main(String[] args) {
		ModifiedServer ms = new ModifiedServer();
		try {
			ms.listenServer(port);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//服务器监听方法
	private void listenServer(int port) throws IOException {
		ServerSocket server = new ServerSocket(port);
		System.out.println("Server is listening port " + port);
		while (true) {
			clientCount++;
			if (clientCount <= maxClientCount) {
				Socket socket = server.accept();  //没有客户端请求到来时,系统将在此阻塞
				new Thread(new RequestHandler(socket));
			} else {
				System.out.println("服务器所能承受的客户端请求最大并发数已经到上限!");
			}
		}

	}

}

虽然实现了多用户并发访问,但每次客户端访问,服务器端就要建立连接,花销较大,下篇请见用线程池实现套接字编程

你可能感兴趣的:(java,.net,socket,dos,OS)