lucia1112
lucia1112
2011-09-11 22:16

有关客户端与服务器端通信的问题

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

写了一段服务器端和客户端通信代码,实现以下功能:客户端将文件发送到服务器端,服务器端接收完文件后给客户端发送一个消息。
运行代码后出现以下问题不知如何神马原因:
客户端文件可以正确地传到服务器端,但是服务器端接收完文件后发送给客户端地确认消息,客户端收不到,报java.net.SocketException: Socket is closed的异常,请各位帮忙看看

客户端代码
package client;

import java.io.*;
import java.net.*;

public class ClientThread extends Thread{
private int hostPort=3000;
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
private BufferedReader streamReader;

public ClientThread(){
    setUpConnection();

}

public void setUpConnection() 
{ 
try 
{ 

    Socket client = new Socket("127.0.0.1", hostPort); 
    //socketWriter = new PrintWriter(client.getOutputStream());
    InputStream inputFromSocket = client.getInputStream();
    streamReader = new BufferedReader(
            new InputStreamReader(inputFromSocket));
    handleConnection(client);
} catch (UnknownHostException e) { 
System.out.println("Error setting up socket connection: unknown host"); 
} catch (IOException e) { 
System.out.println("Error setting up socket connection: " + e); 
} 
} 



public void handleConnection(Socket client) {
    try {
        File file=new File("a.txt");
        FileReader fileReader = new FileReader(new File("a.txt"));
        BufferedReader bufferedFileReader = new BufferedReader(fileReader);
        PrintWriter streamWriter = new PrintWriter(
                client.getOutputStream());
        String line = null;
        while ((line = bufferedFileReader.readLine()) != null) {
            streamWriter.println(line);
        }
        fileReader.close();
        streamWriter.close();

        //////读取服务器收到文件后的返回内容
        String str=streamReader.readLine();
        while(str!=null){
            System.out.println(str);
        }


    } catch (Exception e) {
        System.out.println("Error handling a client: " + e);
    }
    } 

}

服务器端代码

import java.net.*;
import java.io.*;

public class ServerThread extends Thread{
private int listenPort = 3000;

public ServerThread(){
    acceptConnections();
}

public void acceptConnections() {
    try {
        ServerSocket server = new ServerSocket(listenPort);
        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);
    }
}

public void handleConnection(Socket incomingConnection) {
    try {
        BufferedReader  socketReader = new BufferedReader( 
                new InputStreamReader(incomingConnection.getInputStream())); 

        String line = null;
        while ((line = socketReader.readLine()) != null) {
            //streamWriter.println(line);
            System.out.println(line);
        }
        socketReader.close();

        /////////接受完服务器发送的文件后给客户端
        OutputStream os=incomingConnection.getOutputStream();
        os.write("".getBytes());
    } catch (Exception e) {
        System.out.println("Error handling a client: " + e);
    }
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答

为你推荐

换一换