select-function

Using select() system call for listening on stdin and the server

大城市里の小女人 提交于 2019-12-22 10:02:08
问题 I want to use select system call to multiplex STDIN and SOCKFD (connected to a server) such that I must be listening to both STDIN and SOCKFD and depending upon where the data is available for read I must proceed further. Note: #define STDIN 0 This is what happens when I do the following. I connect to the server the usual way [ socket() then connect() ] I add STDIN and connection socket descriptor 'SOCKFD' to the fd_set (named 'readset') meant for reading. Then I call select(). Use FD_ISSET

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

♀尐吖头ヾ 提交于 2019-12-20 09:38:18
问题 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 sockaddr_in servaddr; int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1) perror("Socket"); bzero((void *) &servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(6782); servaddr.sin_addr.s_addr = inet_addr(<server_ip_address>); if (-1 == connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)))

How to use select() to read input from keyboard in C

吃可爱长大的小学妹 提交于 2019-12-18 13:19:26
问题 I am trying to use select() to read keyboard input and I got stuck in that I do not know how to read from keyboard and use a file descriptor to do so. I've been told to use STDIN and STDIN_FILENO to approach this problem but I am still confused. How can I do it? 回答1: Youre question sounds a little confused. select() is used to block until input is available. But you do the actual reading with normal file-reading functions (like read , fread , fgetc , etc.). Here's a quick example. It blocks

Using sleep and select with signals

故事扮演 提交于 2019-12-12 13:35:10
问题 I want to use the select() function to wait for 1 second, as my program uses signals to control stuff, so sleep() would return prematurely. The weird thing is that when using select() it also returns prematurely. I am calling select like this struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 1000000; select (0 ,NULL, NULL, NULL, &timeout); but whenever a signal arrives, it returns (I am using a nano second timer for the signal) Anyone knows why? 回答1: Try something like this:

Unexpected results with select and recvfrom

孤街浪徒 提交于 2019-12-11 08:04:45
问题 fd_set rset; struct timeval tv; FD_ZERO(&rset); FD_SET(sockfd, &rset); tv.tv_sec = 1; tv.tv_usec = 0; for(;;) { for(count = 0; count < elements in sockaddr_in array; count++) { //flag_array is filled with -1 before for(;;) if(flag_array[count] == -1 && select(sockfd+1, &rset, NULL, NULL, &tv)) { recvfrom(...) } tv.tv_sec = 1; FD_ZERO(&rset);//this fixed it FD_SET(sockfd, &rset);//and this too } //contact everyone from sockaddr array (works like a charm!) } If I don't send my message from my

Unusual select / sock stream behavior behavior

一笑奈何 提交于 2019-12-11 04:08:57
问题 I'm writing a small program to request a chunk of a file, and then have another program return that specific chunk of the file. I can get this to work using files up to about 555000 bytes, but on anything lager than that, I get unusual behavior. In my loop, I check a progress buffer, which is an array of integers, to see whether or not I have a specific chunk of the file. If I do, then I don't send a request for that chunk, but if I don't, then I request the chunk from a peer. I have a linked

Using select() system call for listening on stdin and the server

对着背影说爱祢 提交于 2019-12-06 03:47:50
I want to use select system call to multiplex STDIN and SOCKFD (connected to a server) such that I must be listening to both STDIN and SOCKFD and depending upon where the data is available for read I must proceed further. Note: #define STDIN 0 This is what happens when I do the following. I connect to the server the usual way [ socket() then connect() ] I add STDIN and connection socket descriptor 'SOCKFD' to the fd_set (named 'readset') meant for reading. Then I call select(). Use FD_ISSET to determine which fd is ready for read. The problem with this set up is that FD_ISSET is always true

How to use select() to read input from keyboard in C

笑着哭i 提交于 2019-11-30 09:31:09
I am trying to use select() to read keyboard input and I got stuck in that I do not know how to read from keyboard and use a file descriptor to do so. I've been told to use STDIN and STDIN_FILENO to approach this problem but I am still confused. How can I do it? Youre question sounds a little confused. select() is used to block until input is available. But you do the actual reading with normal file-reading functions (like read , fread , fgetc , etc.). Here's a quick example. It blocks until stdin has at least one character available for reading. But of course unless you change the terminal to

non blocking socket select returns 1 after connect

江枫思渺然 提交于 2019-11-28 09:43:17
问题 First of all I would like to say that this is another problem than this one: Similar but not the same My code looks like this: struct addrinfo hints, *res; struct sockaddr* serveraddr; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; int res2 = getaddrinfo(ip, port, &hints, &res); printf("getaddrinfo() res: %d, %d\n", res2, errno); serveraddr = res->ai_addr; //create new socket int soc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); printf("socket()