Reputation: 1
I am trying to write a simple chat application which has its server coded in C and client side coded in Java. I am able to send data from client side to server i.e. java to C, but am having problems when i am sending data from server in C to client in Java. Here is the code for both. Part of CLient COde in java
DataInputStream inDataStream;
DataOutputStream outDataStream;
String clientmessage;
String ipaddress,servermessage="";
System.out.print("Input the IP Address: ");
DataInputStream dis=new DataInputStream(System.in);
ipaddress=dis.readLine();
Socket sock=new Socket(ipaddress,PORT);
System.out.println("Server found..... ");
do
{
System.out.print("Enter your message here: ");
dis=new DataInputStream(System.in);
clientmessage=dis.readLine();
//outDataStream=new DataOutputStream(sock.getOutputStream());
//outDataStream.writeUTF(clientmessage);
PrintWriter out =new PrintWriter(sock.getOutputStream(), true);
out.println(clientmessage+"\n");
out.flush();
BufferedReader input =new BufferedReader(new InputStreamReader(sock.getInputStream()));
servermessage = input.readLine();
System.out.println("Server's message: "+servermessage);
//input.flush();
}while(!servermessage.equals("bye"));
}
}
Part of server code in C:
listen(listenfd, 10);
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
do
{
if ((num = recv(connfd, recvBuff, 1024,0))== -1) {
perror("recv");
exit(1);
}
//printf("bytes received = %d\n",num);
//recvBuff[num]='\0';
//printf("%d %d %d %d\n",recvBuff[0],recvBuff[1],recvBuff[2],recvBuff[3]);
printf("Client's message: %s\n",recvBuff);
printf("Enter your message here: ");
scanf("%s",sendBuff);
//printf("sending...... %s\n",sendBuff);
strcat(sendBuff,"\n");
if((send(connfd,sendBuff,strlen(sendBuff),0))==-1)
{
fprintf(stderr, "Failure Sending Message\n");
close(connfd);
exit(1);
}
//printf("sent...... %s\n",sendBuff);
}while(strcmp(sendBuff,"bye"));
close(connfd);
close(listenfd);
}
First the client is sending data to server which is received perfectly by server in C. When send()
in C sends data to client, it does so word by word. For eg- if i send "Hello People" , the server first sends "Hello" and then waits, till that time client has sent another message which is received successfully by server. Now the server doesn't ask for a new string to check rather it just sends the remaining data of the previous time which was 'People' .
Can somebody point out where am going wrong? Thanks in advance.
Upvotes: 0
Views: 727
Reputation: 5535
Apart from the normal read() / send() api recommendation, i suspect your problem is the classic scanf
scanf("%s",sendBuff);
Because you mentioned it works the first time fine, and doesn't wait for the input the second time.
flush the input buffer using something like
scanf("%s\n",sendBuff);
Upvotes: 1
Reputation: 9526
Your question is a bit unclear, but I think your problem is this: you have written the data from the C program into the kernel, but the kernel hasn't sent it on to the network yet. On Linux you can run netstat -tope
to see how much data is in those buffers.
Why has it not sent it? Basically because it's trying to accumulate a maximum-sized packet and send it all at once, rather than sending a series of small packets. Your program is going to send more data when it gets some from the user. You haven't done anything to tell the kernel to send right away.
There are several fairly complicated algorithms that govern this, the most famous being Nagle's algorithm.
The short answer is that you probably want to set TCP_NODELAY
on the socket which tells the kernel to send immediately rather than waiting for more data.
Alternatively if you recv
on the socket that will tell the kernel you're interested in a response from the other party, which will cue the kernel to send what it currently has. For robustness you might want the server to ack the messages anyhow, so you can tell if the connection has dropped.
Upvotes: 0