c - socket not bind() ing - Invalid argument -


good evening all, kicks , giggles i'm trying hand @ *nix sockets , tcp/ip. now, off ground i'm trying create socket on 2 endpoints , basic text chat program , forth. now, before i'm , running i'm hit bind 'invalid argument':

user@user-virtualbox:~/sockets$ ./socket sock=3 s_->sin_family = 2 s_->sin_port = 3879 s_->sin_addr.s_addr = 0 sockfd = 3 s_->sin_family = 2 s_->sin_port = 3879 s_->sin_addr.s_addr = 0 socket bind error: invalid argument sizeof(s_) = 8 

code below. so, inaddr_any should 255.255.225.255 = 0, understand; af_inet 2; , sin_port, well, i've looked @ binary backward , forward , not sure understand how 9000 represented in host order @ 3879 9000, assume it's non-issue. additionally, since 1 stdout , 2 stderr, assume above dynamically allocated , 3 should fine socket file descriptor.

#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_, (socklen_t)sizeof(s_))) == -1){         fprintf(stderr, "socket bind error: %s\n", strerror(errno));     }     printf("sizeof(s_) = %lu\n", sizeof(s_)); }  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_);     exit(0); } 

i believe problem

sizeof() inside bind()... 's_' pointer, sizeof (probably) 4... need dereference it:

if((bind(sock, (struct sockaddr*)s_, (socklen_t)sizeof(*s_))) == -1){     fprintf(stderr, "socket bind error: %s\n", strerror(errno)); } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -