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.