Error On Binding 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
Bad File Descriptor Error In C
site About Us Learn more about Stack Overflow the company Business Learn more write error bad file descriptor about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x socket error bad file descriptor 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 Bind failed
Bad File Descriptor Python
: Bad file descriptor up vote 1 down vote favorite I'm new to socket programming and here I'm trying to set up a simple server socket and listen on it. Here's my code. int server_socket, client_socket, sent; size_t len; struct sockaddr_in server, client; char msg[] = "Hello to the world of socket programming"; server_socket == socket(AF_INET, SOCK_STREAM, 0); if(server_socket == -1){ perror("Socket creation failed:
Bad File Descriptor Golang
"); exit(-1); } //define server server.sin_family = AF_INET; server.sin_port = htons(10000); server.sin_addr.s_addr = INADDR_ANY; memset(&server.sin_zero, '\0', 8); len = sizeof(struct sockaddr_in); if((bind(server_socket, (struct sockaddr*)&server, len)) == -1){ perror("Bind failed: "); exit(-1); } if(listen(server_socket, 5) == -1){ perror("Listen failed: "); exit(-1); } while(true){ if((client_socket = accept(server_socket, (struct sockaddr *)&client, (socklen_t *)&len)) == -1){ perror("Accept failed: "); exit(-1); } sent = send(client_socket, msg, strlen(msg), 0); printf("Sent %d byte to client: %s\n", sent, inet_ntoa(client.sin_addr)); close(client_socket); } I keep getting Bind Failed : Bad file descriptor However my file descriptor is exactly as per beej guide. Any help? c++ c sockets share|improve this question asked Sep 8 '15 at 15:37 user5132418 this line: memset(&server.sin_zero, '\0', 8); is very 'iffy'. much better to (before setting any fields in 'server' `memset(&server, '\0', sizeof( server) ); and remove the current call to memset() –user3629249 Sep 8 '15 at 20:44 Did you #include all the necessary header files? the posted code is missing that info. needs: stdio.h stdlib.h sys/types.h sys/socket.h netinet/in.h arpa/inet.h and perhaps a few others. Did you compile the code with all the warnings enabled? (for gcc, at a minimum use: -Wall -W
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 Overflow the company Business Learn more bad file descriptor mac about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Bad File Descriptor Perl
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping bad file descriptor bash each other. Join them; it only takes a minute: Sign up setsockopt: Bad file descriptor in C++ up vote 0 down vote favorite I have the famous error "address already in use" because I have no check for http://stackoverflow.com/questions/32461862/bind-failed-bad-file-descriptor the bind function. Here is my code: memset(&(this->serv_addr), 0, sizeof(this->serv_addr)); this->serv_addr.sin_family = AF_INET; this->serv_addr.sin_port = htons(port); this->serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); int yes = 1; if (setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { perror("setsockopt"); exit(1); } But running code I got this: setsockopt: Bad file descriptor The code is right, from the guide Beejnet. But why I got the error? Maybe the position of the code is wrong? The first the that sock_fd is called is in the function w_socket: int retv; http://stackoverflow.com/questions/7790773/setsockopt-bad-file-descriptor-in-c retv = socket(AF_INET, SOCK_STREAM, 0); if(retv == -1) { std::string err_msg(strerror(errno)); err_msg = "[socket] " + err_msg; throw err_msg; } else { int reuse_opt = 1; setsockopt(this->sock_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(int)); return retv; } } By default there's the sesockopt but no check. I've tried but it doesn't work. c++ bind setsockopt share|improve this question edited Oct 17 '11 at 8:35 asked Oct 17 '11 at 7:49 user840718 2 Please reward the people who have helped you in the past and accept answers to your older questions. –DarkDust Oct 17 '11 at 7:52 Sorry I don't know what I have to do. Do I have to check or click on anything? –user840718 Oct 17 '11 at 7:58 Where did you get the descriptor sock_fd? Would you please show the whole code? –Cortexiphan Oct 17 '11 at 8:04 Ok did it...sorry again. –user840718 Oct 17 '11 at 8:05 The code is this: antoniotancredi.altervista.org/2010/02/11/echo-server-in-c I have modified the code but the template is that. I just want to insert the sesockopt working right. –user840718 Oct 17 '11 at 8:09 add a comment| 1 Answer 1 active oldest votes up vote 0 down vote accepted You need to first create the socket via the socket call, like: sock_fd = socket(PF_INET, SOCK_STREAM, 0); (and check the return value; see man 2 socket for detail
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the http://stackoverflow.com/questions/22448422/bad-file-descriptor-error-while-sending-from-server-python workings 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 http://forums.otterhub.org/viewtopic.php?t=22849 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; bad file it only takes a minute: Sign up Bad file descriptor error while sending from server - python up vote 0 down vote favorite Hi I can't figure out why this error happens. I've put in print statements and did a test script to try and isolate the problem and I just can't figure out why it doesn't work. s bad file descriptor = None for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = res print ('Listening on', sa) try: s = socket.socket(af, socktype, proto) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except socket.error as msg: s = None continue try: s.bind(sa) s.listen(1) except socket.error as msg: s.close() s = None continue break if s is None: print ('could not open socket') sys.exit(1) def handler(signum, frame): s.close() sys.exit(1) signal.signal(signal.SIGINT, handler) while True: print('Waiting for connection...') conn, addr = s.accept() print ('Connection from', addr) while True: data = conn.recv(1024) if not data: break print ('data: ' + data.decode('utf-8')) filetoOpen = data.decode('utf-8') print('filetoOpen: ' + filetoOpen) with open (filetoOpen, 'r') as myfile: contents=myfile.read().replace('\n', '') print('about to send :\n' + contents) conn.send(contents.encode('utf-8')) myfile.close() conn.close() s.close() sys.exit() When I run it I get Traceback (most recent call last): File "server.py", line 67, in