Error Reading From Socket Bad File Descriptor
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more accept bad file descriptor about Stack Overflow the company Business Learn more about hiring developers or posting ads python socket error bad file descriptor with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack
Socket Error Errno 9 Bad File Descriptor
Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Linux server socket - Bad file descriptor up vote 3 down vote
Raise Error Ebadf Bad File Descriptor
favorite I have a problem with a server socket under Linux. For some reason unknown to me the server socket vanishes and I get a Bad file descriptor error in the select call that waits for an incomming connection. This problem always occurs when I close an unrelated socket connection in a different thread. This happens on an embedded Linux with 2.6.36 Kernel. Does anyone know why this would socket bad file descriptor python happen? Is it normal that a server socket can simply vanish resulting in Bad file descriptor? edit: The other socket code implements a VNC Server and runs in a completely different thread. The only thing special in that other code is the use of setjmp/longjmp but that should not be a problem. The code that create the server socket is the following: int server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); struct sockaddr_in saddr; memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(1234); const int optionval = 1; setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &optionval, sizeof(optionval)); if (bind(server_socket, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { perror("bind"); return 0; } if (listen(server_socket, 1) < 0) { perror("listen"); return 0; } I wait for an incomming connection using the code below: static int WaitForConnection(int server_socket, struct timeval *timeout) { fd_set read_fds; FD_ZERO(&read_fds); int max_sd = server_socket; FD_SET(server_socket, &read_fds); // This select will result in 'EBADFD' in the error case. // Even though the server socket was not closed with 'close'. int res = select(max_sd + 1, &read_fds, NULL, NULL, timeout); if (res > 0) { struct sockaddr_in caddr; socklen_t clen = sizeof(caddr); return accept(server_socket, (struct sockaddr *) &caddr, &clen); } return -1; } edit: When the problem case happ
here for a quick overview of the site Help Center Detailed
Bad File Descriptor Error In Linux
answers to any questions you might have Meta Discuss the bad file descriptor socket accept workings and policies of this site About Us Learn more about Stack Overflow the company what is bad file descriptor Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the http://stackoverflow.com/questions/11735014/linux-server-socket-bad-file-descriptor Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Linux C Socket:“bad file descriptor” occurs when accept() invokes up vote 1 down vote favorite 1 I am writing a send/receive project http://stackoverflow.com/questions/11416396/linux-c-socketbad-file-descriptor-occurs-when-accept-invokes using linux/socket/tcp, I create some threads to send or recieve message,and use a global queue to store message Code: void EnQueue (M_queue queue,char * message,char * target_IP,char * target_IP_BAK) { char a; char * m=&a; strcpy(m,target_IP); M_element node=(M_element)malloc(sizeof(struct MessageNode )); node->message=message; node->target_IP=target_IP; node->next=NULL; if(IsQueueEmpty(queue)) { queue->rear=queue->front=node; } else { queue->rear->next=node; queue->rear=node; } } M_queue messages; void* receive_message(); void* send_message(void* args); int main() { pthread_t in_thread,out_thread; messages=(M_queue)malloc(sizeof(struct MessageQueue)); init(messages); int ret=pthread_create(&in_thread,NULL,receive_message,NULL); if(ret==-1) { perror("thread receive:"); } while(1) { if(!IsQueueEmpty(messages)) { M_element message=DeQueue(messages); pthread_create(&out_thread,NULL,send_message,(void*)message); } } pthread_join(in_thread,NULL); return 0; } void* send_message(void * args) { struct sockaddr_in sockaddr_out; M_element message=(M_element)args; int cfd=socket(AF_INET,SOCK_STREAM,0); if(cfd==-1) { perror("send_socket"); exit(1); } sockaddr_out.sin_family=AF_INET; sockaddr_out.sin_port=htons(9734); printf("%s\n",message->target_IP); sockaddr_out.sin_addr.s_addr=inet_addr(message->target_IP); int ret=connect(cfd,(struct sockaddr *)&sockaddr_out,sizeof(sockaddr_out)); if(ret==-1) { perror("connect"); offline_message(message->target_IP,message->message); exit(1); } if(write(cfd,message->message,strlen(message->message)+1)==-1) { perror("write"); exit(1); } close(cfd); } void* receive_message() { char buf[256]; char IP[256]; memset(IP,0,256); int first_socket=socket(AF_INET,SOCK_STREAM,0); if(first_socket==-1) { perror("socket"); exit(1); } struct sockaddr_in in_socketaddr; in_socketaddr.sin_family=AF_INET; in_socketaddr.sin_port=htons(9999); in_socke
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about http://stackoverflow.com/questions/12838041/method-to-handle-bad-file-descriptor-error Stack Overflow the company Business Learn more about hiring developers or posting ads with http://developerweb.net/viewtopic.php?id=5567 us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Method to handle bad file descriptor error up vote 2 down vote favorite 1 bad file Scenario: Connection is established between client and server.The client side connections are closed and client is destroyed. At the server side some of the connections closed are detected but some are not.So there are socket descriptor which are like dangling pointers.Select on these returns Bad file descriptor error but it is not possible to find the invalid fd . Question: In the above scenario, where the client connection does not bad file descriptor exist how should I handle these BAD FILE DESCRIPTORs .Can I call recv() on these ? c sockets recv share|improve this question edited Oct 11 '12 at 11:08 Jeyaram 5,37221643 asked Oct 11 '12 at 11:01 user1495948 2916 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote The signature for select() is: int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict errorfds, struct timeval *restrict timeout); Doesn't the errorfds set give you a list of descriptors which generated an error condition, such as EBADF? You can call recv() or any other function that takes a file descriptor with the bad file descriptor; you will likely get back an EBADF error from the function called (unless it detects a different error condition before detecting EBADF). share|improve this answer answered Oct 11 '12 at 11:45 Jonathan Leffler 439k62509823 recv() returns 0 in this case as if the connection with client is closed but select returns bad file descriptor for the same socket descriptor. –user1495948 Oct 11 '12 at 12:47 1 You may need to distinguish between what the system thinks of as a 'bad file descriptor' and what you think of as a bad file descriptor. The system thinks tha
AM Tommo Member Registered: 2007-09-03 Posts: 76 Re: Bad file descriptor Hi there.I am getting a small error in my program which I cannot spot.Let me explain what the program is doing at the point of the error:Firstly, we have two structs:struct client_message { short len; char command[200]; }; struct client_data { int socket; struct client_message msg; };Those of you who helped me in a previous thread may remember these.At the moment, to get things running, when the client connects, he/she will send a message to the server and the server will output it. The messages are being sent and received fine, there is just an error that pops up afterwards:Client:$./client 127.0.0.1 hello Length of packet: 8 Message: helloServer:New connection from 127.0.0.1 Length of packet: 8 Command: hello Why are we here? recv 2: Bad file descriptorSo when a client sends a message, a 'struct client_message' is filled: Eg)Message: helloLength of packet = 2 bytes to store length + 6 = 8 ('h' 'e' 'l' 'l' 'o' '\0')so define 'struct client_message msg';Then msg.len = 8 and msg.command='hello';So the server grabs the first two bytes first to get the length of the incoming packet, then we can ensure we have all the data by doing recv() until all 'len' bytes are received. Simple enough.So basically, somewhere along the way, the client socket is changed to -1 resulting in a recv() error. But I'm not sure why.This is the function which is giving the error:/* LENGTH_OF_PACKET = 2 */ void get_client_command( struct client_data *clnt, fd_set *master ) { /* pointer to our client_message data */ void *buf = (void *)&(clnt->msg); int nbytes; if( ( nbytes = recv( clnt->socket, buf, LENGTH_OF_PACKET, 0 ) ) <= 0 ) { if( nbytes == 0 ) { /* this executes for some reason after we've gotten the data */ printf("Why are we here?\n"); FD_CLR( clnt->socket, master ); close( clnt->socket ); clnt->socket