供各位參考。
不過有一點我還是不太懂,為何fd_set要copy一份起來,是因為fd_set 經過select function後就無效了嗎?我試過只用一個fd_set,果真只能接收到第一個request,除非每次select前重新把所有的fd做FD_SET()。
Select的用法可以參考:石頭閒語:select() - I/O Multiplexer
#include#include #include #include #include #include #include #include #define SVR_IP "127.0.0.1" #define SVR_PORT 8080 #define BUF_SIZE 1024 int main (int argc, char **argv) { struct sockaddr_in server_addr; socklen_t len; fd_set active_fd_set; int sock_fd; int max_fd; int flag = 1; char buff[BUF_SIZE]; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(SVR_IP); server_addr.sin_port = htons(SVR_PORT); len = sizeof(struct sockaddr_in); /* Create endpoint */ if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket()"); return -1; } else { printf("sock_fd=[%d]\n", sock_fd); } /* Set socket option */ if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int)) < 0) { perror("setsockopt()"); return -1; } /* Bind */ if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("bind()"); return -1; } else { printf("bind [%s:%u] success\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port)); } /* Listen */ if (listen(sock_fd, 128) == -1) { perror("listen()"); return -1; } FD_ZERO(&active_fd_set); FD_SET(sock_fd, &active_fd_set); max_fd = sock_fd; while (1) { int ret; struct timeval tv; fd_set read_fds; /* Set timeout */ tv.tv_sec = 2; tv.tv_usec = 0; /* Copy fd set */ read_fds = active_fd_set; ret = select(max_fd + 1, &read_fds, NULL, NULL, &tv); if (ret == -1) { perror("select()"); return -1; } else if (ret == 0) { printf("select timeout\n"); continue; } else { int i; /* Service all sockets */ for (i = 0; i < FD_SETSIZE; i++) { if (FD_ISSET(i, &read_fds)) { if (i == sock_fd) { /* Connection request on original socket. */ struct sockaddr_in client_addr; int new_fd; /* Accept */ new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &len); if (new_fd == -1) { perror("accept()"); return -1; } else { printf("Accpet client come from [%s:%u] by fd [%d]\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), new_fd); /* Add to fd set */ FD_SET(new_fd, &active_fd_set); if (new_fd > max_fd) max_fd = new_fd; } } else { /* Data arriving on an already-connected socket */ int recv_len; /* Receive */ memset(buff, 0, sizeof(buff)); recv_len = recv(i, buff, sizeof(buff), 0); if (recv_len == -1) { perror("recv()"); return -1; } else if (recv_len == 0) { printf("Client disconnect\n"); } else { printf("Receive: len=[%d] msg=[%s]\n", recv_len, buff); /* Send (In fact we should determine when it can be written)*/ send(i, buff, recv_len, 0); } /* Clean up */ close(i); FD_CLR(i, &active_fd_set); } } // end of if } //end of for } // end of if } // end of while return 0; }
yozgat
回覆刪除tunceli
hakkari
zonguldak
adıyaman
UEİGDF
https://titandijital.com.tr/
回覆刪除mersin parça eşya taşıma
osmaniye parça eşya taşıma
kırklareli parça eşya taşıma
tokat parça eşya taşıma
6E8
9430F
回覆刪除binance %20 referans kodu
5A9DF
回覆刪除karabük sesli sohbet siteleri
hatay sesli sohbet uygulamaları
kilis mobil sohbet odaları
eskişehir sesli sohbet odası
görüntülü sohbet kadınlarla
hakkari telefonda kadınlarla sohbet
bitlis kadınlarla sohbet
muhabbet sohbet
mobil sohbet
CEC27
回覆刪除yozgat ücretsiz sohbet odaları
uşak mobil sohbet et
ardahan sohbet uygulamaları
Mersin Tamamen Ücretsiz Sohbet Siteleri
şırnak ücretsiz görüntülü sohbet
Tekirdağ Canlı Sohbet Odaları
ankara telefonda kızlarla sohbet
hatay canli sohbet
Urfa Mobil Sohbet Odaları
1E60C
回覆刪除Threads Takipçi Hilesi
Likee App Beğeni Satın Al
Apenft Coin Hangi Borsada
Satoshi Coin Hangi Borsada
Madencilik Nedir
Threads Yeniden Paylaş Hilesi
Kripto Para Kazanma Siteleri
Aptos Coin Hangi Borsada
Coin Üretme
61B35
回覆刪除referans kimligi nedir
paribu
bybit
bitget
kripto kanalları telegram
bitexen
bitcoin nasıl üretilir
binance ne demek
mercatox
087DA
回覆刪除canlı güvenilir show
30F3FFE0ED
回覆刪除şov
görüntülü şov
ücretli şov
skype show
görüntülü şov whatsapp numarası
whatsapp ücretli show
cam şov
telegram show
canli web cam show