Using select() for non-blocking sockets?

The problem is that when I run them, nothing happens The real problem is that people have been pasting stuff from Beej for years without understanding it. That's why I don't really like that guide; it gives large blocks of code without really explaining them in detail You're not reading anything and not sending anything; no fgets, scanf, cin etc. Here's what I would do: FD_SET(sock, &read_flags); FD_SET(STDIN_FILENO, &read_flags); /* .. snip .. */ if(FD_ISSET(STDIN_FILENO, &read_flags)) { fgets(out, len, stdin); } This will monitor stdin and read form it when input is available; then, when the socket si writeable ( FD_ISSET(sock, &write_flags) ) it will send the buffer.

The problem is that when I run them, nothing happens. The real problem is that people have been pasting stuff from Beej for years without understanding it. That's why I don't really like that guide; it gives large blocks of code without really explaining them in detail.

You're not reading anything and not sending anything; no fgets, scanf, cin etc. Here's what I would do: FD_SET(sock, &read_flags); FD_SET(STDIN_FILENO, &read_flags); /* .. snip .. */ if(FD_ISSET(STDIN_FILENO, &read_flags)) { fgets(out, len, stdin); } This will monitor stdin and read form it when input is available; then, when the socket si writeable (FD_ISSET(sock, &write_flags)) it will send the buffer.

Thank you for the reply. Yes, I have been reading Beej's tutorial for the last few days and am barely getting by. I tried adding that to my code, but now I get a segmentation fault whenever I type something into either terminal on that terminal (type 'hi' into server, server seg faults, client closes fine, vice versa).

I tried getline to make sure it wasn't just fgets. GDB doesn't really help - tells me the backtrace is just #0 0x08048c93 in main (). Sorry to ask (little desperate at this point), but do you know what could be causing this?

– Sterling Jul 16 at 6:54 @Sterling compile using -g. Also, for starters declare out and in like so: char out255;. – cnicutar Jul 16 at 7:03 weird...when I just change those declarations the program doesn't seg fault, but it goes straight through the while loop and exits.

– Sterling Jul 16 at 7:06 @Sterling Try to debug it. Add printf lines if you need to. Add a printf line right after the "select" printf("Select fired\n"); etc.– cnicutar Jul 16 at 7:09 Will do.

Another weird thing maybe worth noting...select fires a different number of times for the two programs and a different number of times each time I run it. Sometimes 10, sometimes 20, sometimes 5, then closes the socket. Somehow the recv call is.

I have the program working correctly now. I'm too tired to explain it, so I'll just post the code to compare with the above code if anyone in the future needs the info. Server - #define PORT "4950" #define STDIN 0 struct sockaddr name; void set_nonblock(int socket) { int flags; flags = fcntl(socket,F_GETFL,0); assert(flags!

= -1); fcntl(socket, F_SETFL, flags | O_NONBLOCK); } // get sockaddr, IPv4 or IPv6: void *get_in_addr(struct sockaddr *sa) { if (sa->sa_family == AF_INET) return &(((struct sockaddr_in*)sa)->sin_addr); return &(((struct sockaddr_in6*)sa)->sin6_addr); } int main(int agrc, char** argv) { int status, sock, adrlen, new_sd; struct addrinfo hints; struct addrinfo *servinfo; //will point to the results //store the connecting address and size struct sockaddr_storage their_addr; socklen_t their_addr_size; fd_set read_flags,write_flags; // the flag sets to be used struct timeval waitd = {10, 0}; // the max wait time for an event int sel; // holds return value for select(); //socket infoS memset(&hints, 0, sizeof hints); //make sure the struct is empty hints. Ai_family = AF_INET; hints. Ai_socktype = SOCK_STREAM; //tcp hints.

Ai_flags = AI_PASSIVE; //use local-host address //get server info, put into servinfo if ((status = getaddrinfo("127.0.0.1", PORT, &hints, &servinfo))! = 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } //make socket sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); if (sock ai_addr, servinfo->ai_addrlen) = '\0') cout.

The real problem is that people have been pasting stuff from Beej for years without understanding it. That's why I don't really like that guide; it gives large blocks of code without really explaining them in detail.

I have the program working correctly now. I'm too tired to explain it, so I'll just post the code to compare with the above code if anyone in the future needs the info.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions