Mubashira Zaman Mubashira Zaman - 28 days ago 7
C Question

Sending a string from C client to Java server

I'm trying to send a string from my c client to a Java server, after which the server sends a text file to the client.

This is the part of client code that sends the string.

int n = write(sock_fd,"Ready",5);
if (n < 0)
printf("ERROR writing to socket\n");
recv_file(sock_fd, filename);

And this is the server part of java code:

InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from client is " + message);
String FILENAME = "data.txt";
sendFile(socket, "data.txt");

Now I have verified that if I remove the part in the server code where it tries to read the string from c client, the rest of the code works fine and the file is transmitted. But if do not comment the string receiving code, both the server and client keep waiting.

I will be grateful if somebody solves this issue for me.

P.S. I know this question has been asked before but that didn't help me, so I started a new thread.

Answer Source

br.readLine() wants to read a line. The client never sends a newline, so the server is waiting for a newline... forever!

Add a newline to the command sent by the client:

int n = write(sock_fd,"Ready\n", 6);