1

I have a problem in a client-server application. The client sends a picture to the server and the server responds with a reply message.

Here is my server code:

public class Server
{
    public static void main(String[] args) throws Exception
    {
        String response="response";
        ServerSocket socket = new ServerSocket(3333);
        while (true)
        {
            Socket clientSocket = socket.accept();

            DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
            FileOutputStream fout = new FileOutputStream("output.jpg");
            int i;
            while ( (i = dis.read()) > -1) 
                fout.write(i);    

            DataOutputStream outToClient= new    DataOutputStream(clientSocket.getOutputStream());

            outToClient.writeBytes(response);

            fout.flush();
            fout.close();
            dis.close();
            outToClient.close();
            clientSocket.close();
        }
    }
}

Client:

public static void main(String[] args) throws Exception
{
    // TODO Auto-generated method stub
    String sentence;
    int i;
    FileInputStream fis = new FileInputStream ("pathphoto.jpg");   
    Socket sock = new Socket ("hostname",3333);
    DataOutputStream os = new DataOutputStream(sock.getOutputStream());
    System.out.println("Sending....");
    while ((i = fis.read()) > -1)
        os.write(i);

    BufferedReader inFromServer= new BufferedReader(new InputStreamReader(sock.getInputStream()));
    sentence=inFromServer.readLine();
    System.out.println("FROM SERVER: " + sentence);
    fis.close();
    os.close();
    sock.close();
}
}

The problem is that the client doesn't receive the response from the server and I think along these lines:

BufferedReader inFromServer= new BufferedReader(new InputStreamReader(sock.getInputStream()));
sentence=inFromServer.readLine();

Because without them the server sends the response.

Any advice on how to fix it?

3
  • 1
    how are you running this? in two separate threds? if yes, are both the threds alive all the time? Commented Nov 5, 2012 at 12:18
  • does any one of your applications close down before the other can read/write to a socket? check if you are not closing sockets too fast. Commented Nov 5, 2012 at 12:41
  • You have an extra } at your client code hope that is just a typo or that it does have something to close. Commented Jun 30, 2013 at 7:05

1 Answer 1

1

Its not stuck in BufferedReader, it is actually stuck in while ((i = fis.read()) > -1) Since your client never told server length of stream, or closed stream server will try to read next byte from inputstream and will be stuck when client is done sending file and waiting for response from server.

When you remove code to read response back from server, client goes ahead and closes all streams and in that case server reads a -1 and goes ahead.

Sign up to request clarification or add additional context in comments.

1 Comment

i try to put os.close before BufferedRedear but it launch an exception becouse it closes the socket

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.