0

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.

9
  • You can look here stackoverflow.com/questions/8803581/… Commented Apr 21, 2014 at 9:56
  • 2
    Closely read the man-pages for recv()/send() and learn that those two functions do not necessarily receive/send as much bytes as they were told to, but few. So looping around such calls counting until all data expected had been received/sent is a good idea, not to say an essential necessity. Commented Apr 21, 2014 at 10:03
  • Allthough the code intialising the sockets is not shown, I strongly doubt is uses raw sockets. I retagged the question accordingly. Commented Apr 21, 2014 at 10:25
  • Also note that your send function in C will not send the terminating null byte as strlen doesn't count that. Commented Apr 21, 2014 at 11:06
  • 1
    "... tested the code with messages ...": Without implementing your own application level protocol there are no messages, as TCP is stream oriented. Commented Apr 21, 2014 at 15:26

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

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.