1

I'm writing a C socket program to chat and transfer file between multi clients. There is a lot of topic about TCP client-server but I didn't find any solution to solve my problem. I would like to ask how we use just one port for both connections.
Here is what I've tried and it works well, but I'm using two separate ports for chatting and sharing:


*Server:
1) create a socket and bind it to port 1234 for chatting
2) create another socket and bind it to port 4321 for sharing file
-> we have two listen sockets waiting for clients to connect.

*Client:
1) Create a socket and connect to port 1234 of server //chatting
2) Create a second socket and connect to port 4321 of server //sharing
--> for each client, we need two socket connect to two ports of the server -> we can chat and share file at the same time.

The problem is it seems not efficient and face up to some problems:
a) Use two ports -> require the server open port forwarding on both
b) If we have multi clients connect to server at the same time, server will detect wrong the chatting and sharing socket of two clients

I also found a suggestion to use a socket and mark flag/header the transmit data then we can separate chatting and sharing data. But if the buffer size of client and server is different, we can't receive full of packet each time to detect the type of receiving packet.

What is the best way to use one port for chat and share concurrently?
and if we integrate more services like stream music, video,etc. How to use multi connection on the same port?
If we use multi connection on the same port how to detect type of connection (chat or share) when a new client is accepted?
Thank you,

Caslo
  • 11
  • 1
  • 1
    Port forwarding? Server doesn't need that. Just the sockets, unless you have a strange configuration. And how would the server "detect wrong" anything? But you can use one connection also and multiplex the data as you said. But again, why do you think there's a problem with "receiving a full packet"? – Sami Kuhmonen Apr 03 '16 at 08:27
  • ah. port forwarding: I want it can communicate via internet then I have to run server on a machine, port forwarding from router to my computer. "detect wrong" : I use two threads to listen connect from clients, then if client 1 and client 2 connect to server at the same time, server could detect chatting of client 1 and sharing of client 2 as a new pair connection from a client. – Caslo Apr 03 '16 at 08:47
  • about "receving a full packet". if I set buffer at client = 100 and buffer at server = 50, then when I transmit 100 bytes from client , server will receive two packet of 50 bytes. if I header 100 bytes as: my data --> server will receive first packet: { a half of data } and the second packet: { other half of data }. It's wrong if there is multi client send its 100byte to server at the same time. Eg: server will detect: { a half of data client 1} and { other half of data client 2} .i understand right? – Caslo Apr 03 '16 at 08:55
  • 1
    TCP is a stream, not a packet protocol. Every packet you send inside the stream will of course need a header with length so you know the size. You *never* send just data. – Sami Kuhmonen Apr 03 '16 at 08:57
  • yeah, I know that, but if I have only one stream ( a socket with a port) and I transmit text message and my file by this stream. how the server know what it is receiving is message to display on the screen or file data to store? – Caslo Apr 03 '16 at 09:04
  • 1
    By you telling so in the header – Sami Kuhmonen Apr 03 '16 at 09:19
  • how about if the file data has something like a message flag inside? eg: I transmit a buffer as: {'File' '/File' } and if it's a message: { 'Mess' '/Mess' }. At the server, buffer size is < client buffer size. --> using ret = recv() --> server will receive the first buffer with size of "ret": { 'M' < a half of client buffer> } , and the second buffer : { } , if start with a message flag -> server will display file data on the screen? – Caslo Apr 03 '16 at 10:04
  • Again, the header will have the *size* of the packet. There is no way to mix things up. Buffer sizes don't matter in this sense. You always handle the whole packet and that's it. You don't just randomly throw away the size and start reading some random sized buffers and *assume* there's a header there. – Sami Kuhmonen Apr 03 '16 at 10:06
  • Ops, I'm really confuse. "the header" which you're saying is yourself define header or the header at kernel layer? from the client: send(fd, buffer_s, 100, flag) and at the server: ret = recv(fd, buffer_r, 50, flag) --> I don't know how to handle buffer_r to get full of 100bytes, first we receive a buffer_r with "ret" bytes, this buffer is only 50 bytes. How I know the client has sent 100 bytes? //i'm sorry, I don't have enough reputation number to chat instead of comment. – Caslo Apr 03 '16 at 10:23
  • A header you define, which says what you're sending, how much etc. Just as you can say to a friend "these 10 boxes go to the bedroom and next five to the kitchen" while moving, you give instructions to your code on the other side. So first you say what you're sending, then you send it. – Sami Kuhmonen Apr 03 '16 at 10:27
  • thank you, your answer could be the solution for my problem. but what happen if the network is not stable and I even just received " a half of header" not full header? my header = 5 bytes, and I just received 2 bytes in the first buffer. I think use header to include too much information is not a good way to go. I would like to know how the other apps do it with socket. – Caslo Apr 03 '16 at 10:39
  • There are only two possibilities: you receive all the data or the connection drops. In the latter case you just forget you were ever receiving anything. In the first case you handle the packet. These are the basics of network programming so please do read up on them and see how it is done from examples. – Sami Kuhmonen Apr 03 '16 at 10:41
  • Thank you, I'm following the book "Unix network programming" still not complete reading. I'm looking for another way to transfer file and chat concurrently rather than use a header define by myself. because my define header is also considered as data when copy from process to kernel. I know the kernel has itself MTU, should I use a buffer = MTU to improve the speed of transfer? – Caslo Apr 03 '16 at 10:51
  • You can read [this answer](http://stackoverflow.com/a/36345290/3740093) that I wrote a few days ago. It explains how length- and header-prefixing is done for TCP. – Visual Vincent Apr 03 '16 at 17:31
  • Also, if you receive only half a header you just have to keep reading until you've gotten all 5 bytes (they will be sent some time unless the connection is completely dropped). THEN you decide what to do with the packet. – Visual Vincent Apr 03 '16 at 17:38

0 Answers0