Error In Accept Invalid Argument
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have c accept example Meta Discuss the workings and policies of this site About Us listen invalid argument Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with c accept function 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
Accept() In Socket Programming
like you, helping each other. Join them; it only takes a minute: Sign up Why am I having “Invalid argument” while trying to accept conections? up vote 0 down vote favorite In the next code, while I try to connect a client the server shows the following error: "invalid argument", I can't see the error. if((l_sock=socket(AF_INET,SOCK_STREAM,0))!=-1) listen in c { struct sockaddr_in srv_dir; srv_dir.sin_family=AF_INET; srv_dir.sin_port=8500; srv_dir.sin_addr.s_addr=INADDR_ANY; if((bind(l_sock,(struct sockaddr *)&srv_dir,sizeof(struct sockaddr_in)))!=-1) { if(!(listen(l_sock,5))) { signal(SIGINT,cerraje); int t_sock; struct sockaddr_in cli_dir; socklen_t tam; time_t tstmp; struct tm * res; res=(struct tm *)malloc(sizeof(struct tm)); while(!key) { if((t_sock=accept(l_sock,(struct sockaddr *)&cli_dir,&tam))!=-1) { tstmp=time(&tstmp); res=gmtime(&tstmp); send(t_sock,res,sizeof(struct tm),0); wr_hora(*res,cli_dir.sin_addr); } else perror("PeticiĆ³n no atendida");//The error is printed here. Thank you very much. c sockets network-programming share|improve this question edited Feb 3 '11 at 22:19 asked Feb 3 '11 at 21:57 Andoni Martín 9918 add a comment| 1 Answer 1 active oldest votes up vote 9 down vote accepted Read the documentation on accept(2): The addrlen argument is a value-result argument: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. When addr is NULL nothing is filled in. So you need to initialize the value of tam passed into accept with sizeof(cli_dir). You're fortunate that the socket library was able to catch your error, b
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta accept c++ Discuss the workings and policies of this site About Us Learn more
Accept() Man
about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack
Connect C
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 http://stackoverflow.com/questions/4892190/why-am-i-having-invalid-argument-while-trying-to-accept-conections each other. Join them; it only takes a minute: Sign up accept() returning invalid argument up vote 0 down vote favorite Learning about TCP connections. Can anyone see why my accept() call is erroring with an "invalid argument"? I can't see what I am doing wrong. Thanks! int main(int argc, char *argv[]) { int sockfd, newfd; struct http://stackoverflow.com/questions/20248288/accept-returning-invalid-argument sockaddr_in clientAddr; unsigned int recvLen; socklen_t addr_size; fd_set read_set; struct timeval tv; // initialize the fd set FD_ZERO(&read_set); // prepare the address struct for the first client bzero(&clientAddr,sizeof(clientAddr)); //zero the struct clientAddr.sin_family = AF_INET; //address family (ipv4) clientAddr.sin_port = htons(6001); //sets port to network byte order clientAddr.sin_addr.s_addr = INADDR_ANY; addr_size = sizeof(clientAddr); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stdout, "Cannot create socket for client 0.\n"); fprintf(stdout, "Terminating program\n\n"); exit(1); } else { fprintf(stdout, "Socket established for client 0\n"); } if (bind(sockfd, (struct sockaddr *)&clientAddr, sizeof(clientAddr)) < 0) { fprintf (stdout, "Binding failed for client 0\n\n"); perror("bind failed"); exit (1); } else { fprintf (stdout, "Binding successful for client 0\n"); } // ERROR HAPPENS ON THE NEXT LINE if ((newfd = accept(sockfd, (struct sockaddr *)&clientAddr, &addr_size)) < 0) { fprintf(stdout, "Error accepting inbound data from client 0\n"); perror(" accept() failed"); exit(EXIT_FAILURE); } else { fprintf(stdout, "\tSuccessfully accepted inbound connection from client 0\n"); } return 0; } c sockets tcp network-programming share|improve this question edited Nov 27 '13 at 17:08
Programming Boards Networking/Device Communication accept() invalid argument Getting started with C http://cboard.cprogramming.com/networking-device-communication/138416-accept-invalid-argument.html or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: accept() invalid argument https://bytes.com/topic/c/answers/246820-why-error-accept-invalid-argument Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 05-28-2011 #1 hauzer invalid argument View Profile View Forum Posts Registered User Join Date Aug 2008 Location Belgrade, Serbia Posts 163 accept() invalid argument server.c Code: #include
your question and get tips & solutions from a community of 418,513 IT Pros & Developers. It's quick & easy. why error: "accept: Invalid argument" P: n/a Yarco when doing fork in a loop: Expand|Select|Wrap|Line Numbers while(1){ tmp_sd=accept(sd,(structsockaddr*)&tmp_sin,&len); if(tmp_sd==-1){ perror("accept"); exit(0); } //checkclientip //fork pid=fork(); switch(pid){ case-1: perror("fork"); break; case0: _exit(0); } close(tmp_sd); } If there's no fork, the code done well. But if fork, when telnet, it has error: accept: Invalid argument Thanks a lot. Nov 16 '05 #1 Post Reply Share this Question 12 Replies P: n/a Michael Mair Yarco wrote: when doing fork in a loop: Expand|Select|Wrap|Line Numbers while(1){ tmp_sd=accept(sd,(structsockaddr*)&tmp_sin,&len); if(tmp_sd==-1){ perror("accept"); exit(0); } //checkclientip //fork pid=fork(); switch(pid){ case-1: perror("fork"); break; case0: _exit(0); } close(tmp_sd); } If there's no fork, the code done well. But if fork, when telnet, it has error: accept: Invalid argument Platform or compiler specific questions are best asked in a newsgroup where they are topical. There are no functions accept(), perror(), fork(), close(), and _exit() in standard C, so I won't say anything about their use. comp.unix.programmer might be a good place to ask. Your switch definitely lacks a default, though, to ward against unexpected pid values. printf() debug from there... Cheers Michael -- E-Mail: Mine is an /at/ gmx /dot/ de address. Nov 16 '05 #2 P: n/a Yarco Thanks Nov 16 '05 #3 P: n/a Gary Baydo On Wed, 16 Nov 2005 06:02:15 +0100, Michael Mair wrote: Yarco wrote: when doing fork in a loop: Expand|Select|Wrap|Line Numbers while(1){ tmp_sd=accept(sd,(structsockaddr*)&tmp_sin,&len); if(tmp_sd==-1){ perror("accept"); exit(0); } //checkclientip //fork pid=fork(); switch(pid){ case-1: perror("fork"); break; case0: _exit(0); } close(tmp_sd); } If there's no fork, the code done well. But if fork, when telnet, it has error: accept: Invalid argument Platform or compiler specific questions are best asked in a newsgroup where they are topical. There are no functions accept(), perror(), fork(), close(), and _exit() in standard C, so I won't say anything about their use. [snip] Isn't perror() part of the standard? It's declared in stdio.h. Regards, Gary Nov 16 '05 #4 P: n/a pete Gary Baydo wrote: Isn't perror() part of the standard? It's declared in stdio.h. perror() is in the standard library. -- pete Nov 16 '05 #5 P: n/a Michael Mair Gary Baydo wrote: On Wed, 16 Nov 2005 06:02:15 +0100, Michael Mair wrote:Yarco wrote:when doing fork in a loop: Expand|Select|Wrap|Line Numbers while(