0

I want that a server sends an object to a client, then the client answer the server with another message and so on.

I have following code but I get exception message. I think I did not understand the logic of sending and receiving object over socket.

Server code:

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...");;
    }
}

}

Client code:

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();



}

}

Here is exception message:

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)
apxcode
  • 7,696
  • 7
  • 30
  • 41
user1914367
  • 171
  • 1
  • 2
  • 12

1 Answers1

0

This problem is usually caused by writing to a connection that had already been closed by the peer.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • This question is about a 'connection reset' exception, not a 'socket closed' exception, which is caused when *you* have closed *your own* socket and then continued to use it. If you have a question of your own, ask it as a question. In any case please stop posting irrelevant answers and comments here. – user207421 May 18 '14 at 12:41