c - Accept() socket call - invalid argument -


i'm not quite sure why code returning invalid argument accept() call. i'm binding , listening without problems, should able accept() @ point. if there's wrong inside acceptsocket() call i'm not seeing it. can point me in right direction? code is:

#include <stdio.h> #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <errno.h> void setsocket(struct sockaddr_in* s_){     s_->sin_family=af_inet;     s_->sin_port=htons(9999);     s_->sin_addr.s_addr=inaddr_any;     memset(&(s_->sin_zero), '\0', 8);     printf("s_->sin_family = %i\n", s_->sin_family);         printf("s_->sin_port = %i\n", s_->sin_port);     printf("s_->sin_addr.s_addr= %i\n", s_->sin_addr.s_addr); }  void createsocket(int *sock){     if ((*sock = socket(af_inet, sock_stream, 0)) == -1){         fprintf(stderr, "socket creation error: %s\n", strerror(errno));         exit(1);     }     printf("sock = %i\n", *sock);     fflush(stdout); }  void bindsocket(int sock, struct sockaddr_in *s_){     printf("s_->sin_family = %i\n",s_->sin_family);     printf("s_->sin_port = %i\n",s_->sin_port);     printf("s_->sin_addr.s_addr = %i\n",s_->sin_addr.s_addr);     if((bind(sock, (struct sockaddr*)s_, sizeof(*s_))) == -1)         fprintf(stderr, "socket bind error: %s\n", strerror(errno));     printf("sizeof(s_) = %lu\n", sizeof(*s_)); }  void acceptsocket(int sfd, struct sockaddr_in *s_){     int st = sizeof(*s_);     printf("sizeof(*s_) = %lu\n", sizeof(*s_)); //  printf("(socklen_t*)&st_) = %i\n",(socklen_t*)&st);     fflush(stdout);     if(accept(sfd, (struct sockaddr*)s_, (socklen_t *)&st) == -1)         fprintf(stderr, "socket accept error: %s\n", strerror(errno)); }  void closesocket(int sockfd){     if(close(sockfd) == 1)         fprintf(stderr, "socket close error: %s\n", strerror(errno)); }  void dialogue(int sockfd){     char s[1000];     int bytesread = read(sockfd, s, 1000);     if(bytesread == 1)         fprintf(stderr, "socket read error: %s\n", strerror(errno));     if(write(sockfd, "i read %i bytes!\n", *s, 100) == -1)         fprintf(stderr, "socket write error: %s\n", strerror(errno));     if(write(sockfd, *s, bytesread) == -1)         fprintf(stderr, "socket write error: %s\n", strerror(errno)); }  void listensocket(int sfd){     if(listen(sfd, 100) == -1)         fprintf("socket listen error: %s\n", strerror(errno)); }  int main(int argc, char* argv[]){     int sockfd;     struct sockaddr_in socket_;     createsocket(&sockfd);     setsocket(&socket_);     printf("sockfd = %i\n", sockfd);     fflush(stdout);     bindsocket(sockfd, &socket_);     listensocket(sockfd);     while(1){         acceptsocket(sockfd, &socket_);         dialogue(sockfd);     }      exit(0); } 

please study manual of accept function. typically returns -1 on error, on success returns valid descriptor of socket through can communicate.

basically use following code instead of current function:

int acceptsocket(int sfd, struct sockaddr_in *s_){     int st = sizeof(*s_);     printf("sizeof(*s_) = %lu\n", sizeof(*s_)); //  printf("(socklen_t*)&st_) = %i\n",(socklen_t*)&st);     fflush(stdout);      int out = accept(sfd, (struct sockaddr*)s_, (socklen_t *)&st);      if(out == -1) {         fprintf(stderr, "socket accept error: %s\n", strerror(errno));     }     return out; } 

then proceed use return value argument dialogue function. can't vouch fix code entirely, start towards tcp network communication.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -