(1)Server端
- import java.io.*;
- import java.net.*;
- /**
- *修改 acceptConnections() 以用缺省为 50(或任何您想要的大于 1 的指定数字)实例化 ServerSocket。
- *修改 ServerSocket 的 handleConnection() 以用 ConnectionHandler 的一个实例生成一个新的 Thread。
- * @author Administrator
- */
- public class MultithreadedRemoteFileServer {
- int listenPort;
- public MultithreadedRemoteFileServer(int listenPort) {
- this.listenPort=listenPort;
- }
- //允许客户机连接到服务器,等待客户机请求
- public void acceptConnections() {
- try {
- //指定待发数(backlog 值)是5,一次可以放五个请求到队列中
- 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);
- }
- }
- //与客户机Socket交互以将客户机所请求的文件的内容发送到客户机
- public void handleConnection(Socket connectionToHandle) {
- new Thread(new ConnectionHandler(connectionToHandle)).start();
- }
- public static void main(String args[]) {
- MultithreadedRemoteFileServer server = new MultithreadedRemoteFileServer(1001);
- server.acceptConnections();
- }
- }
import java.io.*;
import java.net.*;
/**
*修改 acceptConnections() 以用缺省为 50(或任何您想要的大于 1 的指定数字)实例化 ServerSocket。
*修改 ServerSocket 的 handleConnection() 以用 ConnectionHandler 的一个实例生成一个新的 Thread。
* @author Administrator
*/
public class MultithreadedRemoteFileServer {
int listenPort;
public MultithreadedRemoteFileServer(int listenPort) {
this.listenPort=listenPort;
}
//允许客户机连接到服务器,等待客户机请求
public void acceptConnections() {
try {
//指定待发数(backlog 值)是5,一次可以放五个请求到队列中
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);
}
}
//与客户机Socket交互以将客户机所请求的文件的内容发送到客户机
public void handleConnection(Socket connectionToHandle) {
new Thread(new ConnectionHandler(connectionToHandle)).start();
}
public static void main(String args[]) {
MultithreadedRemoteFileServer server = new MultithreadedRemoteFileServer(1001);
server.acceptConnections();
}
}
(2)定义一个实现runable接口的类:ConnectionHandler
- import java.io.*;
- import java.net.*;
- /**
- *借用 RemoteFileServer 的 handleConnection() 方法的代码实现 ConnectionHandler 类。
- * @author Administrator
- */
- public class ConnectionHandler implements Runnable {
- protected Socket socketToHandle;
- public ConnectionHandler(Socket socketToHandle) {
- this.socketToHandle=socketToHandle;
- }
- public void run() {
- try {
- PrintWriter streamWriter = new PrintWriter(socketToHandle.getOutputStream());
- //读Socket
- BufferedReader streamReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));
- String fileToRead = streamReader.readLine();
- BufferedReader fileReader = new BufferedReader(new FileReader(fileToRead));
- String line =null;
- while((line=fileReader.readLine())!=null) {
- streamWriter.println(line);//写Socket
- }
- fileReader.close();
- //如果您在关闭streamWriter之前关闭streamReader,则您可以往Socket写任何东西,
- //但却没有任何数据能通过通道(通道被关闭了)
- streamWriter.close();
- streamReader.close();
- }
- catch(Exception e) {
- System.out.println("Error handling a client: "+e);
- e.printStackTrace();
- }
- }
- }
import java.io.*;
import java.net.*;
/**
*借用 RemoteFileServer 的 handleConnection() 方法的代码实现 ConnectionHandler 类。
* @author Administrator
*/
public class ConnectionHandler implements Runnable {
protected Socket socketToHandle;
public ConnectionHandler(Socket socketToHandle) {
this.socketToHandle=socketToHandle;
}
public void run() {
try {
PrintWriter streamWriter = new PrintWriter(socketToHandle.getOutputStream());
//读Socket
BufferedReader streamReader = new BufferedReader(new InputStreamReader(socketToHandle.getInputStream()));
String fileToRead = streamReader.readLine();
BufferedReader fileReader = new BufferedReader(new FileReader(fileToRead));
String line =null;
while((line=fileReader.readLine())!=null) {
streamWriter.println(line);//写Socket
}
fileReader.close();
//如果您在关闭streamWriter之前关闭streamReader,则您可以往Socket写任何东西,
//但却没有任何数据能通过通道(通道被关闭了)
streamWriter.close();
streamReader.close();
}
catch(Exception e) {
System.out.println("Error handling a client: "+e);
e.printStackTrace();
}
}
}
(3)Client端
- import java.io.*;
- import java.net.*;
- /**
- * 用您想连接的机器的 IP 地址和端口实例化 Socket。
- * 获取 Socket 上的流以进行读写。
- * 把流包装进 BufferedReader/PrintWriter 的实例,如果这样做能使事情更简单的话。
- * 对 Socket 进行读写。
- * 关闭打开的流。
- * @author Administrator
- */
- public class RemoteFileClient {
- protected BufferedReader socketReader;
- protected PrintWriter socketWriter;
- protected String hostIp;
- protected int hostPort;
- /**
- * 构造方法
- */
- public RemoteFileClient(String hostIp, int hostPort) {
- this.hostIp = hostIp;
- this.hostPort=hostPort;
- }
- /**
- * 向服务器请求文件的内容,告诉服务器我们想要什么文件并在服务器传回其内容时接收该内容
- */
- public String getFile(String fileNameToGet) {
- StringBuffer fileLines = new StringBuffer();
- try {
- socketWriter.println(fileNameToGet);
- socketWriter.flush();
- String line = null;
- while((line=socketReader.readLine())!=null)
- fileLines.append(line+"\n");
- }
- catch(IOException e) {
- System.out.println("Error reading from file: "+fileNameToGet);
- }
- return fileLines.toString();
- }
- /**
- * 连接到远程服务器,创建我们的 Socket 并让我们访问该套接字的流
- */
- public void setUpConnection() {
- try {
- Socket client = new Socket(hostIp,hostPort);
- socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
- socketWriter = new PrintWriter(client.getOutputStream());
- }
- catch(UnknownHostException e) {
- System.out.println("Error1 setting up socket connection: unknown host at "+hostIp+":"+hostPort);
- }
- catch(IOException e) {
- System.out.println("Error2 setting up socket connection: "+e);
- }
- }
- /**
- * 使用完毕连接后负责“清除”。
- */
- public void tearDownConnection() {
- try {
- socketWriter.close();
- socketReader.close();
- }catch(IOException e) {
- System.out.println("Error tearing down socket connection: "+e);
- }
- }
- public static void main(String args[]) {
- RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1",1001);
- remoteFileClient.setUpConnection();
- StringBuffer fileContents = new StringBuffer();
- fileContents.append(remoteFileClient.getFile("D:/test.txt"));
- //remoteFileClient.tearDownConnection();
- System.out.println(fileContents);
- }
- }