C, socket programming: Connecting multiple clients to server using select()

后端 未结 5 1995
有刺的猬
有刺的猬 2021-02-02 00:23

I\'m trying to make a server that can be connected to by multiple clients. Here\'s my code so far:

Client:

int main(int argc, char **argv) {

  struct so         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-02 01:01

    1) It is a good practice to use PF_INET(protocol family) rather than
    AF_INET(address family) during the Socket creation .

    2) within the while(1) loop
    each time it is advisable to make your readfds empty by using FD_ZERO(&readfds). in the recv() call you should use i rather than clientsocks[i] you have to check return value of recv is negative(which indicating error in reading) if that is the case you do not have to print the message. during printing the message make sure the stdout/server is ready for writing anything to it which you can do it by using writefds (3rd argument of select).

提交回复
热议问题