ServerSocket


#1

Hello,
I’m trying to make a client in Juce to connect to my basic Java server. I’m having trouble with the read and the write commands of serversocket.

s = new StreamingSocket();
s->connect(String(“localhost”), 8000));
char buf[6];
sprintf(buf, “%d\n”, 1);
if (s->waitUntilReady(false, 5000) == 1) {
numOfBytesWrite = s->write(buf, 6);
numOfBytesWrite = s->write(buf, 6);
}

The first write command writes “1” to the server, as expected.
The second write, writes “\ˇ\ø” without the two .
Is this some kind of terminator character?

What is the correct way to write strings to the socket?

Thanx

Gilles


#2

A socket just sends binary data, not null-terminated strings, so your code would write several bytes of uninitialised junk. There’s no such thing as “sending a string” - it’s up to both ends to understand and encode the data in whatever way they need to.