1

I'm new to the TCP/IP protocol, and would like to create a server for a chat, in C++.

For now, I decided to begin all my transmissions with a character to tell what's next (a login request, a message to post, the user-name of a client, ...).

So, I use values like 0x01, 0x02, ... for what is related to login, information request, ... and 0x10 for the messages.

Hopelessly, when I try to connect to my server with another program I created, I receive one transmission from the server, which begin by 0x01 (so all is still OK), but then when I reply with something beginning by 0x03, the server only receive it when I kill the client...

I think that it's the select function that doesn't say that there's something to read.

Is that normal ?

And if yes, should I replace this values by values that are printable and does not have a special meaning ?

dido22
  • 41
  • 5
  • 2
    TCP is working with bytes so values 0-255 regardless if they are printable of nonprintable characters. Choose totally different aproach. Put all into one message with header. – Erik Šťastný Feb 22 '17 at 13:35
  • Yes, TCP is quite happy with binary data.Share the relevant sections of both your client and server/ – Colin Feb 22 '17 at 13:37
  • @ErikŠťastný But does select also works with 0-255 values ? – dido22 Feb 22 '17 at 13:38
  • We dont know which classes or dlls are you using for communication.We can not know what is select? is some method or what? But TCP has nothing to do with characters. TCP is sending and receiving arrays of bytes. for example i am using in my app this format: `4bytes length of message, 4 bytes ID message, xxxx bytes of data` and this is one message sended via TCP – Erik Šťastný Feb 22 '17 at 13:41
  • 1
    if you want any help you will have to post code. – Erik Šťastný Feb 22 '17 at 13:47
  • IMHO your problem has nothing to do with your use of non-printable characters. Try send some printable characters back and forward, to see if that works. If still fails, then printable or not is irrelevant. Suggestion: find a tutorial with a working example and start from there. – ravenspoint Feb 22 '17 at 13:57
  • TCP has no real concept of messages, but only of stream. That means that it ensures that all bytes sent by one side will reach other side in same order, but messages can be concatenated (one read for more than one message) or read by chunk (many reads for one single message). But it transfers any byte with no special meaning... – Serge Ballesta Feb 22 '17 at 14:39
  • @ErikŠťastný TCP works with octets. These are often called bytes, but C++ (and also C) have a different definition of "byte" (specifically "the size of a char", which can be 9 or 16 or 32 bits) and this question is tagged C++. – Martin Bonner supports Monica Feb 22 '17 at 15:11

1 Answers1

4

TCP is entirely a binary protocol. It defines a packet header which follows the IP datagram header. A continuous data-stream is divided into packets so it (hopefully) goes smoothly from point A to point B. The stream comprises arbitrary octets (bytes).

Are you sure that the only difference between the messages is the initial byte value, $01 vs $03? Perhaps the message size is different? Perhaps one instance does something to flush the stream? Perhaps the messages are being sent in sequence on a session?

If you need to quickly transmit short, discrete messages, UDP might help prevent surprising latencies.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • @osgx UDP has no retransmission mechanism, but you can add your own. TCP has no reliable flushing mechanism, and workarounds tend to suck. Pick your poison. – Potatoswatter Feb 22 '17 at 14:39