Error Writing To Socket Bad File Descriptor
Contents |
here for a quick overview of the site Help Center Detailed answers python socket error bad file descriptor to any questions you might have Meta Discuss the workings socket error errno 9 bad file descriptor and policies of this site About Us Learn more about Stack Overflow the company Business
Raise Error Ebadf Bad File Descriptor
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack
Socket Bad File Descriptor Python
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 Bad File Descriptor with Linux Socket write() Bad File Descriptor C up vote -1 down vote favorite I have an interesting problem with write(2) what is bad file descriptor function. PrepareResponseForSetCoordinates function causes bad file descriptor error on write. Here is the line of error: perror("ERROR writing to socket"); total output: ERROR writing to socket: Bad file descriptor I am sure that I have established the connection because PrepareResponseForConnectionTest works like a charm. Can you have any idea about the reason of the error? When I use gcc as compiler there was no problem. After that because of using multiple new cpp sources I am using g++ as compiler and I have this error. Regards Here below my code: #define MAX_PMS_MESSAGE_LEN (4096) unsigned char baCommBuffer[MAX_PMS_MESSAGE_LEN]; unsigned char PrepareResponseForSetCoordinates(void) { unsigned char baTempBuff[255]={0}; unsigned short bCnt=0,i=0,bCsum=0,bCnt2=0; time_t lEpochTime; time_t lSessionTime; memset(baTempBuff,0,sizeof(baTempBuff)); memset(baCommBuffer,0,sizeof(baCommBuffer)); bzero(baCommBuffer,MAX_PMS_MESSAGE_LEN); bzero(baTempBuff,sizeof(baTempBuff)); lEpochTime = time(NULL); baCommBuffer[bCnt++] = START_CHAR; baCommBuffer[bCnt++] = START_CHAR; baCommBuffer[bCnt++] = PACKET_SEPERATOR; sprintf((char*)baTempBuff,"%ld",(unsigned long)lEpochTime); memcpy(baCommBuffer+bCnt,baTempBuff,10); bzero(baTempBuff,sizeof(baTempBuff)); bCnt+=10; baCommBuffer[bCnt++] = PACKET_SEPERATOR; // lSessionTime = time(NULL); if(SPMSMessage.lSessionID) lSessionTime = SPMSMessage.lSessionID; else lSessionTime=lEpochTime; sprintf((char*)baTempBuff,"%ld",(unsigned long)lSessionTime); memcpy(baCommBuffer+bCnt,baTempBuff,10); bzero(baTempBuff,sizeof(baTempBuff));
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 Stack
Bad File Descriptor Socket Accept
Overflow the company Business Learn more about hiring developers or posting ads with us bad file descriptor linux Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a bad file descriptor c 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 favorite I have http://stackoverflow.com/questions/11258781/bad-file-descriptor-with-linux-socket-write-bad-file-descriptor-c 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 happen? Is it normal that http://stackoverflow.com/questions/11735014/linux-server-socket-bad-file-descriptor 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 happens i currently simply restart the server but I don't understan
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings http://stackoverflow.com/questions/13287359/socket-bad-file-descriptor and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation http://cboard.cprogramming.com/c-programming/125259-basic-socket-programming.html 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 bad file only takes a minute: Sign up socket bad file descriptor up vote 0 down vote favorite I want to write a multi clients socket program, but I get Bad file descriptor when the stage of accept. How can I correct my code? Thanks! Here is my code http://codepad.org/q0N1jTgz Thanks! Here is my part of code! while(1) { struct sockaddr_in client_addr; bad file descriptor int addrlen = sizeof(client_addr); /*Accept*/ if(clientfd = accept(sockfd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen) < 0) { perror("Accpet Error"); close(sockfd); exit(-1); } /*Fork process*/ if(child = fork() < 0) { perror("Fork Error"); close(sockfd); exit(-1); } else if(child == 0) { int my_client = clientfd; close(sockfd); send(my_client, welcome, sizeof(welcome), 0); while ((res = recv(my_client, buffer1, sizeof(buffer1), 0)) > 0) { string command(buffer1); cout << "receive from client:" << command << ", " << res << " bytes\n"; memset(buffer1, '\0', sizeof(buffer1)); } } close(clientfd); } sockets file-descriptor share|improve this question edited Nov 8 '12 at 13:18 asked Nov 8 '12 at 10:39 Andy 1431116 Please include the relevant parts of your code in your question by editing it. The codepad will go away any time. –user647772 Nov 8 '12 at 10:59 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted there are a few bugs in your code first you need to use parentheses around the assignments for child and clientfd. line 68 should be changed to if((clientfd = accept(sockfd, (struct sockaddr *)&client_addr, (sock
Programming Boards C Programming Basic socket programming Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: Basic socket programming Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 03-28-2010 #1 cnewbie1 View Profile View Forum Posts Registered User Join Date Nov 2009 Posts 111 Basic socket programming Sorry for pasting something as basic as this, but I don't seem to be able to figure this out. I'm using this example, pasted from an online tutorial, for a basic socket server application: Code: void dostuff(int); void error(char *); int main(int argc, char *argv[]) { int unsigned sockfd, newsockfd, portno, clilen, pid; struct sockaddr_in serv_addr, cli_addr; if (argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { error("ERROR opening socket"); } bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { error("ERROR on binding"); } listen(sockfd,5); clilen = sizeof(cli_addr); while (1) { newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) { error("ERROR on accept"); } pid = fork(); if (pid < 0) { error("ERROR on fork"); } if (pid == 0) { close(sockfd); dostuff(newsockfd); exit(0); <------ line XXX } else close(newsockfd); } return 0; /* we never get here */ } void error(char *msg) { perror(msg); exit(1); } void dostuff(int sock) { int n; char buffer[256]; bzero(buffer,256); n = read(sock,buffer,255); if (n < 0) { error("ERROR reading from socket"); } printf("Here is the message: %s\n",buffer); n = write(sock,"I got your message",18); if (n < 0) { erro