Hi all, I have the following problem:
My server sends the string str="1 2 4 dds \n" continuously to a C++ client.Here is the java code of the server:
private class SendingThread implements Runnable{
public Socket s;
public DataOutputStream oo;
public ObjectInputStream oi;
////private QueryTransferObject q;
boolean connected;
public SendingThread() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void run() {
try {
Socket s = new Socket("137.132.153.60",8080);
connected = true;
byte[] str;
str=new byte[25600];
str="1 2 4 dds \n".getBytes();
while (connected) {
try {
ObjectOutputStream oo=new ObjectOutputStream(s.getOutputStream());
oo.write(str);
oo.flush();
System.out.print("send \n");
// Log.d("SendingThread", "C: Send data object finished.");
// connected = false;
Thread.sleep(2000);
} catch (Exception e) {
// Log.e("SendingThread", "S: Error", e);
}
}
Here is the partial C code of the client:
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
while(1)
{
printf("I am here\n");
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
}
The client keeps displaying this result:
I am here
Here is the message: �
I am here
Here is the message: w
1 2 4 dds
It seems that the message is not received correctly. Can anyone give some insight on how to fix it ? Thank you!