0

I have implemented a client/server to send files . When there is no more messages exchanged after sending the file , the code works perfectly , but if the client sends some string to the server directly after the code of receiving the file both client and server stop doing anything and the file is not sent it's something like if they both get stuck in deadlock but I'm not really sure :

Here is the code to send the file without sending anything after it , which works: Client

String filename;
BufferedReader UIn = new BufferedReader(new InputStreamReader(System.in));
Socket peer = new Socket("localhost",9999);
System.out.print("Enter the file name to download :");
filename= UIn.readLine();

///////////////////////////////

DataOutputStream OutToServer;
OutToServer = new DataOutputStream(peer.getOutputStream());
OutToServer.writeBytes(filename+"\n");

FileOutputStream fos = new FileOutputStream(new File("D:/new.txt"));
BufferedOutputStream  out = new BufferedOutputStream(fos);
InputStream in = peer.getInputStream();
buffer = new byte[1024];

while((count=in.read(buffer))>0)
{
  fos.write(buffer, 0, count);
  System.out.println(buffer);
}

fos.close();
System.out.println("done");

Server:

ServerSocket server =null; 
try {
  server = new ServerSocket(9999);
  while(true)
  {
    client= server.accept();
    //System.out.println("Connection accepted");
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
    DataOutputStream outToclient =new DataOutputStream(client.getOutputStream());
    String request=inFromClient.readLine();
    file = new File(request);

    if (file.exists())
    {
      OutputStream out = client.getOutputStream();
      BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));

      while((count =in.read(buffer)) >0)
      {
        out.write(buffer,0,count);
        out.flush();
      }
    }

    // System.out.println(request);
    // outToclient.writeBytes("alaa\n");

  }
} catch (IOException ex) {
  Logger.getLogger(ServerWork.class.getName()).log(Level.SEVERE, null, ex);
}

But if I try to send anything after the loop between client and server it stops working . Is it because I'm using readLine() and writeBytes()?

t0mppa
  • 3,983
  • 5
  • 37
  • 48
Alaa
  • 539
  • 3
  • 8
  • 29

1 Answers1

1

You are using both DataOutputStream and OutputStream. I think that they should work together but what I guess you should do is to flush the buffer (or close it).

After you're done writing everything you want to send, some data may still be in the buffer. Now you will have to flush the buffer, to force it to send all the data.(as it is said here).

outToclient.writeBytes("alaa\n");
outToclient.flush();
Community
  • 1
  • 1
bogdan.herti
  • 1,110
  • 2
  • 9
  • 18
  • is it a problem to use both OutputStream and DataOutputStream ? – Alaa Nov 24 '13 at 19:28
  • 1
    @Alaa on the client side, you are using an InputStream, and not an DataInputStream. Thus, you don't take benefits of using the DataOutputStream's `writeBytes` method on the server side. You could then try to find another way of sending the extra message, just using the OutputStream. – bogdan.herti Nov 24 '13 at 20:15
  • so now if I use DataOutputStream at both sides , the problem will be solved ? and really thanks a lot for helping me understand the problem in the first place , it's appreciated – Alaa Nov 24 '13 at 20:24
  • I am not sure but it should work. If it is not working don't forget to `flush` the buffer. Anyway, tell us the result. – bogdan.herti Nov 24 '13 at 20:35
  • 1
    Thanks a lot for your help .. thanks to your notes all my problems are solved . thanks – Alaa Nov 24 '13 at 21:48