Troglavi
Troglavi

Reputation: 11

Garbage in TCP message - C++/Java communication

I'm having a slight problem in my C++ client - Java server application. Sockets successfully connect, and transmit several messages, but then comes this part:

msgBuilder<<"TASK?\n";
mymsg = msgBuilder.str();
send(tcp_sock,mymsg.c_str(),8,0);
msgBuilder.str("");
msgBuilder.clear();

The java side reads

msg = in.readLine();
while(!(msg.equals("TASK?"))){
System.out.println("Got "+msg+" expected TASK?");
msg = in.readLine();
}

Problem is, it reads two of what I assume are null characters which I cannot paste here, for some reason. I can get over it by switching to "contains" I guess, but I am interested in what's causing this?

Upvotes: 1

Views: 292

Answers (1)

user207421
user207421

Reputation: 310909

send(tcp_sock,mymsg.c_str(),8,0);

should be

send(tcp_sock,mymsg.c_str(),mymsg.length(),0); // or size() or whatever it is. Not 8.

Upvotes: 3

Related Questions