Java Client and a C++ Server Send and Receive via Tcp Socket

Java client and a C++ server send and receive via TCP Socket

PrintWriter buffer (when autoflush is true) is only flushed by calling println or printf. Calling print may not flush the buffer (Javadoc). Try calling println or use a OutputStreamWriter directly and flush().
Be aware of using the right charset (You can set it up in OutputStreamWriter constructor).

Sending a message via TCP Socket between C++ and Java

Your Java client reads the data from the socket using in.readLine(); which basically reads the data until it finds a line separator or carriage return character.
I am not so familiar with C++, but it seems that your C++ server only sends "hello" without the line separator. This makes the Java client waits forever.
To fix this problem, you can send "hello\n" or "hello\r\n" from your C++ server.

TCP Java client sending Data to a C++ Server

Java code is only sending only a single character for the length but C++ expects a 4 byte string. You might try

m_out.write(String.format("%4d%s",json.length(),json));
m_out.flush();

Network Communication between a java socket (server) and a C++ socket (client)

Here I put a simple code to connect to a server. It may help you if this is your problem.

void client(const char* server_address, short server_port)
{
int sockfd;
struct sockaddr_in servaddr;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

memset(&servaddr, 0x00, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(server_port);
inet_pton(AF_INET, server_address, &servaddr.sin_addr);

connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

//from this point you can start write to the server and wait for its respose

std::string buffer = "testquery";
writen(sockfd, buffer.c_str(), buffer.length());

char *ReadBuffer[512];
while(1)
{
memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
int n = readn(sockfd, ReadBuffer, sizeof(ReadBuffer));
if(n <= 0)
{
//or you dont have anything to read, or you have a problem
break;
}
//this function does the hard job of knowing what to do with all these data
processBuffer(ReadBuffer, n);
}

close(sockfd);

}

I'm using Posix standard and the code is very simplified but I think its a start point.

Regards.

How can I send data from a Java client to a C++ server?

You're using an ObjectOutputStream, which does Java object serialisation. This is not (easily) portable across languages. Since you're only sending a String, you don't need to use an ObjectOutputStream - just use the OutputStream returned from the socket.

Sending a complex object from Java client to C server via Socket

Fundamentally the question is, "How to serialize/deserialize objects in a
language agnostic manner?
" Specifically Java and C in your case. Since you'll
be sending this data over a network, it is also important to take care of network order/endianness issues.

I assume you have access to both the the client and the server. This means you
get to choose how to serialize the data. (If not, the answer is simple. Write
to the specs of what the other is expecting)

Personally, I would use Protocol Buffers.
There are Java bindings
and C bindings.

If you don't like Protocol Buffers, there are other options like:

  • JSON (already mentioned)
  • YAML
  • Apache Thrift
  • XDR
  • roll your own
  • ...


Related Topics



Leave a reply



Submit