views:

35

answers:

2

Hi, i've a minimal server which wait until a client connect ,then he start a thread which will send a reply back to the client, the problem is the reply.

This is the code of the server:

 int port = 1234;
 ServerSocket servSock = null;
 servSock = new ServerSocket(port);       
  while (true) {
   Socket link = servSock.accept();
   serverThread st = new serverThread(link);
   st.start();
  }

This is the code of the run() method of the thread,the one which send the answer back, sk is the socket "link" passed by parameter in the server code

 public void run() {
    String dato = "";
    InputStream i = null;
    try {
        i = sk.getInputStream();
    } catch (IOException ex) {
        Logger.getLogger(serverThread.class.getName()).log(Level.SEVERE, null, ex);
    }
        Scanner input = new Scanner(i);
     //i receive the data sent
        while (input.hasNext()) {
            dato += input.nextLine();
        }   
    // then i send a reply
    DataOutputStream outputStream=new DataOutputStream(sk.getOutputStream());
    outputStream.writeInt(1);
    outputStream.close();

Client side ( only the code which should receive the answer from the server) :

        Socket link;
        int valid = 0;
        String url="localhost";
        int port=1234;
        link = new Socket(InetAddress.getByName(url), port);

        //i've to send some data to the server
        PrintWriter output = new PrintWriter(link.getOutputStream(), true);
        String a = new String(Base64.encode(mex));
        output.println(createXml(tag, a));
        output.flush();


        //then i need to receive an answer from the server
        DataInputStream answerI = new DataInputStream(link.getInputStream());

        while(answerI.available()!=0)// but answerI.available() is always equal 0
           valid = answerI.readInt();

        answerI.close();
        output.close (); 
        link.close();

With this code the istruction valid = answerI.readInt(); is not reached.

Without the while cycle, the client freeze at line : valid = answerI.readInt();

Can anyone help me?

Thank you in advance

A: 

I'm guessing that the server is blocked in a call to input.hasNext(). This will return false when the socket is closed, and its InputStream returns -1 to signal the end of the stream. However, the socket is still open. The client can send another line; the Scanner is blocking to see what the client's next move will be.

There are ways to shutdown "half" of a socket, so that the server can tell that the client has closed its sending channel but can still receive a response.

However, this approach is complicated. I suggest a change to the protocol so that the server can determine when it is allowed to respond.

erickson
thank you! now it works!
michele
A: 

In this protocol you don't need the reply if it is always '1'. Just close the socket. The client should block in a read() which will return -1 when the server closes the socket.

EJP
the reply will not be always 1, i've put 1 to test the protocol, currently i close the input stream and the output stream separately in the server and in the client, for example:1 - client send mex to server then closes the output stream2 - server receive the mex then closes the inputstream,3 - server open an outputstream and the client open an inputstream4 - server send the reply back to the client.Now it works :) thank you!
michele