Reputation: 233
Say we have a server program with socket sa and new_socket. The tutorial I'm using takes socket sa and new_socket, and two structures of sockaddr_in
named server
and client
; then binds the socket to an IP address:port, then calls listen()
function. When listen
returns, the program calls
new_socket = accept(sa, (struct sockaddr*)&client, &length);
My question is, lets say there are 3 people connecting...
Do I need to have 3 different structs and 3 different new_sockets for each accept
function, say if i want my server to serve 3 different clients connecting to it?
Also, why do we need a new_socket for accept
? Why is there two different sockets 1 for bind
and 1 for accept
? Shouldn't socket operations be performed on the bind
ed one?
I'm trying to implement a class for sockets to make it easier on me, and as a good way to practice my oop skills....
Upvotes: 0
Views: 3560
Reputation: 781751
You need one socket for the server in general. This socket is where you set the port that the server is listening on, and call accept()
to wait for incoming connections.
In addition, you need a socket for each client connection. This is a new socket that is returned by accept()
. This is necessary because a server can handle multiple clients. If you used one socket for everything, there would be no way to tell which client was sending you a message, or indicate which client to send a reply to.
Upvotes: 2