Search code examples
csocketstcp

tcp socket : what is the way to make all clients connected at different times receive the same data using c language?


About TCP socket, when a client connects to the server, the server sends datas to the specified server port, and the client reads datas from this port. Can the later connected clients only read new datas? If so, what is the way to make all clients connected at different times receive the same datas?

For example:

The client 1 connects to the server, the server sends the data "123456", 2 seconds later, the client 2 alse connects to the server, now the server will send data "abcdefg". if I want the client 2 also received the data "123456", I have to resend data "123456", a question occured:the client 1 received the data "123456" again, because the two client use same server port.I want to send a time, but the 2 client can received.


Solution

  • "a question occured:the client 1 received the data "123456" again, because the two client use same server port."

    No, that will not happen. The server-client connection consists of the server ip+port and the client ip+port, where the client port will be unique during the session (it will not be the port it connected to). Any message the server sends trough this connected socket will be sent with the destination of the client ip+port and will only reach one receiver.