A: 

That's one of the reasons applications separate data and signaling path (FTP, VoIP, IM file transfers, IRC DCC, etc.). That said, the chat delay shouldn't be that big - maybe a second or two on a really saturated link.

I think they do have a point. Also it's much easier to maintain secure architecture that way. Just test it. If you think the delay is acceptable, it probably is.

viraptor
Adding more sockets is troublesome, I'll have to reauthenticate the client and keep the different sockets synchronized. I know it's probably the best solution, but it's not nice to implement.
Nefzen
It sounds like what you really want is a protocol on top of TCP so that you can have multiple data streams piped through a single TCP socket. I'm sure there are protocols suited to that task, but I'm not heavily involved in network programming so I don't know where you should look. Of course my 'roll your own' solution should do what you want as well.
dss539
+3  A: 

I think your best solution would be to not send data and chat through the same channel.

But if you must, you could chunk the data yourself and maintain a send queue. Size the chunks to be small enough to give your "half a second" response time.

When you want to send chat, insert it at a higher position in the queue. You'll have to create a scheme for tagging the chunks to ensure proper reassembly on the other end.

If I misunderstood your problem and you don't need to interrupt data while it's being sent, you can just use a priority queue and mark the chat messages with a higher priority.

Regardless of how you approach it, I think that once you have handed the data over to the API, it is out of your hands and you can't control the ordering.

I'm eager to see if anyone else has a better answer for you. This is an interesting question.

dss539
Down this path it'd be easier to keep two separate queues for packets... one for chat packets and one for data packets. That way you can simply check the 'chat' queue first when sending out data, no need for a priority queue or similar.
jerryjvl
how can I "chunk the data" myself? I am planning on sending 512kb or so messages, yes, but I might reach a situation where I send 10 messages and clog up the TCP window. I saw Java having a "Java Message Service" that gives a best effort priority, which is fine. I thought I saw the same in C#, but turns out it wasn't for TCP.
Nefzen
By "chunking the data yourself" I mean that if you have a 20 MB file you could break it into 128 kB file chunks. Then send the data synchronously so that when you call the "send" method, your thread blocks until that data is sent. (If you use the asynchronous send method you'll still have the same problem because it will just queue up the data and move on.) You will probably have a "sender" thread that just consumes data in your priority queue 1 chunk at a time. If a chat message appears during the send of a 20mb file, it will have to wait for at most 128kb of the file to be transferred.
dss539