socket编程(二)--服务器端实现多线程处理客户端请求

需求:与socket编程(一)相同的需求

客户端代码如下:

package com.socket.clientsocket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class Sclient {

	/**
	 * @param args
	 */
	
	protected BufferedReader socketReader;
	protected PrintWriter socketWriter;
	protected String hostIp;
	protected int hostPort;
	
	
	public Sclient(String ahostIp,int ahostPort){
		this.hostIp = ahostIp;
		this.hostPort = ahostPort;
		
	}
	public void setUpConnection(){
	
		try {
			//1.创建Socket,建立连接
			Socket socket = new Socket(hostIp,hostPort);
			//2.获取输入流和输出流,并进行封装
			socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			socketWriter = new PrintWriter(socket.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void tearDownConnection(){
		
		try {
			socketReader.close();
			socketWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	//根据需求,对流进行处理
	public String getFile(String fileNameToGet){
		StringBuffer sb = new StringBuffer();
		socketWriter.println(fileNameToGet);
		socketWriter.flush();
		String line = null;
		try {
			while((line = socketReader.readLine()) != null){
				sb = sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	public static void main(String[] args) {
		//创建客户端
		Sclient client = new Sclient("198.27.0.166",8200);
		//建立连接
		client.setUpConnection();
		//需求操作
		String content = client.getFile("c:\\log.txt");
		//关闭连接
		client.tearDownConnection();
	}

}

 服务器端代码如下:

package com.socket.multiserversocket;

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

public class SserverM {

	/**
	 * @param args
	 */
	
	protected int listenPort = 3000;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		SserverM server = new SserverM();
		
		server.acceptConnections();

	}
	
	//将处理丢给一个实现Runnable接口类的新线程
	public void handleConnection(Socket connectionToHandle) {
	     new Thread(new ConnectionHander(connectionToHandle)).start();
	}


	
	public void acceptConnections() {
        try {
        	//5为最多等待客户端数目,连上正在处理的请求,总共能处理6个,如果再有请求,将拒绝连接
        ServerSocket server = new ServerSocket(listenPort, 5);
        Socket incomingConnection = null;
        while (true) {
            incomingConnection = server.accept();
            handleConnection(incomingConnection);
        }
    } catch (BindException e) {
    System.out.println("Unable to bind to port " + listenPort);
    } catch (IOException e) {
    System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
    }
}


}

 实现Runnable接口的处理器

package com.socket.multiserversocket;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ConnectionHander implements Runnable {
	
	protected Socket incommingSocket;
	
	public ConnectionHander(Socket aIncommingSocket){
		this.incommingSocket = aIncommingSocket;
	}

	public void run() {
		
		try {
			InputStream inputFromSocket = this.incommingSocket.getInputStream();
			OutputStream outputToSocket = this.incommingSocket.getOutputStream();
			
			BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
			FileReader fileReader = new FileReader(new File(streamReader.readLine()));
			
			
			BufferedReader bufferedFileReader = new BufferedReader(fileReader);
			
			PrintWriter socketWriter = new PrintWriter(outputToSocket);
			String line = null;
			while((line = bufferedFileReader.readLine()) != null){
				socketWriter.println(line);
			}
			
			streamReader.close();
			fileReader.close();
			socketWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		
		

	}

}
 

你可能感兴趣的:(socket)