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,