服务器和客户端正在发送多个对象

时间:2014-05-17 22:33:40

标签: java sockets object client-server

我希望服务器将对象发送到客户端,然后客户端使用其他消息来回答服务器,依此类推。

我有以下代码,但我收到异常消息。我想我不理解在套接字上发送和接收对象的逻辑。

服务器代码:

public class Server implements Runnable {

int MAX_NUM_CLIENT = 5;
int clientNum = 0;
boolean isStart = false; // if ranking algorithm starts it is true
Socket csocket;
Server(Socket csocket) {
    this.csocket = csocket;
}
BufferedReader reader = new BufferedReader(new InputStreamReader
        (getClass().getClassLoader().getResourceAsStream("questions.txt")));
ArrayList<String> questions = new ArrayList<String>();


public static void main(String args[]) 
        throws Exception {
    ServerSocket ssock = new ServerSocket(9654);
    System.out.println("Listening");

    //wait connection
    while (true) {
        Socket sock = ssock.accept();
        System.out.println("Connected");
        new Thread(new Server(sock)).start();
    }

}

public void run() {

    try {

        if(questions.isEmpty()){
            String line = null;
            while((line = reader.readLine()) != null){
                questions.add(line);
            }
        }

        ObjectOutputStream objectOutput = new ObjectOutputStream(csocket.getOutputStream());
        ObjectInputStream objectInput = new ObjectInputStream(csocket.getInputStream());

        //InetAddress host = InetAddress.getLocalHost();            

        objectOutput.writeObject(questions);

        P0Computation comp = new P0Computation();  //some calculations
        P0Sending send1 = comp.P1Calculation1(); //some calculations

        objectOutput.writeObject(send1);

        int [] pjans = new int[2];
        pjans[0] = objectInput.readInt(); // read client's answer
        pjans[1] = objectInput.readInt(); // read client's answer           
        objectOutput.writeInt(comp.P0Calculation2(pjans));

        csocket.close();
    }
    catch (IOException e) {
        System.err.println("Client left...");;
    }
}

}

客户代码:

public class Client {

public static void main(String args[]) throws UnknownHostException, IOException, ClassNotFoundException {
    Socket ssocket = null;
    ssocket = new Socket("192.7.1.6", 9654);

    ObjectOutputStream objectOutput = new ObjectOutputStream(ssocket.getOutputStream());
    ObjectInputStream objectInput = new ObjectInputStream(ssocket.getInputStream());

    ArrayList<String> questions = (ArrayList<String>) objectInput.readObject();
    P0Sending p0ans = (P0Sending) objectInput.readObject();

    PjComputation comp = new PjComputation();

    objectOutput.writeInt(comp.PjComputation1(p0ans.QX, p0ans.c, p0ans.g)[0]);
    objectOutput.writeInt(comp.PjComputation1(p0ans.QX, p0ans.c, p0ans.g)[1]);

    int beta = objectInput.readInt();



}

}

以下是异常消息:

Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Client.main(Client.java:16)

1 个答案:

答案 0 :(得分:0)

此问题通常是由写入已被对等方关闭的连接引起的。