0

I’m making a program in C++ using windows sockets where I want a single socket but the ability to send data from different threads and distinguish server side which thread sent the data as they have different applications. Essentially logical data streams using a single socket connection. I’m having trouble finding a way to differentiate the packets server side. The best I can think of is starting each packet with a few bytes indicating which thread has sent it. Are there any better ways to be doing this?

Similar to How to use single port for multiple logical data streams (Winsock)? but I'm stuck at differentiating the packets.

Community
  • 1
  • 1
Acebond
  • 55
  • 1
  • 7

2 Answers2

0

If the so-called packets are not all of a fixed length that the server knows, you need also an indication where a packet starts or ends.

Armali
  • 18,255
  • 14
  • 57
  • 171
0

Your best bet (I think) is to associate a unique id with each task, create a single thread that handles the networking and (as you said) adds the task-id to the packet.

On the receiver side you do the same: a single thread reads the packets and distributes their content according to the task-id.

Attila
  • 28,265
  • 3
  • 46
  • 55