By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,530 Members | 2,181 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,530 IT Pros & Developers. It's quick & easy.

Sockets: Send bad address error on localhost connection

P: 1
Hello everybody,

The error I have seems very easy to solve, but for some odd reason I can't seem to solve it. Anyways, here's my "setup".

I created a server running on localhost:1200 (telnet localhost 1200 proves that it is up and running) and I coded a client which is supposed to connect to that server and send it data.
The odd part is that the client connects to the server (via connect()) without any problem, but three lines later, it cannot send it any data. The oddest part is that "send()" does not return "-1" (i.e. error) but does return the number of bytes supposedly sent. That said, if I show the error occurred (via perror), it prints "send: bad address".
Another odd thing is that the server never receives anything. I checked with Wireshack if the packets were send, and they are.

So anyways, either I have a problem with the recv() in my server (but I really wouldn't know why), either their a problem with the send() part in the client.

I have been trying tons of different things for about four hours now and I really don't understand why it doesn't work.
Oh, yes, I have also tried using very simple servers and I have the same problem. Another thing is that a couple days ago the code seemed to be working fine since there was a connection between the client and the server. Sadly, even when I replace my code with the old one from the SVN (thank gosh I have a svn server), I still get the same problem...

Anyways, here's the code. The comments starting with FYI are (of course) for your information.

Client part
Expand|Select|Wrap|Line Numbers
  1. // the definitions of server, mdl and msg shouldn't be necessary to understand the code
  2.     server srv = *(mdl->srv);
  3.     strcpy(srv.name, mdl->name);
  4.     struct hostent *he;
  5.     if ((he=gethostbyname("localhost")) == NULL) {
  6.         herror("gethostbyname");
  7.         log_srv(&srv, "gethostbyname");
  8.     }
  9.  
  10.     if ((srv.sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  11.         perror("Terminal: socket error");
  12.         log_srv(&srv, "socket error");
  13.     }
  14.  
  15.     srv.remote_addr.sin_family = AF_INET; /* host byte order */
  16.     srv.remote_addr.sin_port = htons(port); /* short, network byte order */
  17.     srv.remote_addr.sin_addr = *((struct in_addr *)(he)->h_addr);
  18.     bzero(&(srv.remote_addr.sin_zero), 8); /* zero */
  19.  
  20.     log_srv(&srv,"created");
  21.     if (connect(srv.sockfd,( struct sockaddr *)&(srv.remote_addr), sizeof(struct sockaddr)) == -1) {
  22.         perror("Terminal: connect error");
  23.         log_srv(&srv,"connect error");
  24.         exit(-1);
  25.     }
  26.     while(1) {
  27.         msg * damsg = create_msg("0000000001","Achat","0001000001","16");
  28.         damsg_str = msg_to_str(damsg);
  29. // FYI, damsg_str looks like "|0000000001|Achat|0001000001|16|"
  30.  
  31.         log_msg("Terminal: sending...",damsg);
  32.         int lSent=0; char* damsg_str;
  33.  
  34.         if ((lSent=send(srv.sockfd, damsg_str, strlen(damsg_str)+1, 0)) != 0) {
  35.             perror("Terminal: send error");
  36.             log_srv(&srv, "send error");
  37.         }
  38. //FYI, if the previous condition is "==-1" instead of "!= 0" perror doesn't show.
  39. // However, as said previously, perror does contain an error, which is "bad address"
  40.         log_smth("Terminal %d bytes sent for message %s",lSent,msg_to_str(damsg));
  41.         sleep(1);
  42.  
  43.     }
  44.  
Server part
Expand|Select|Wrap|Line Numbers
  1.     server srv = *(mdl->srv);
  2.     strcpy(srv.name, mdl->name); srv.my_port=port;
  3.     srv.isAlive = 1;
  4.     if ((srv.sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  5.         perror("socket");
  6.         exit(1);
  7.     }
  8.     srv.local_addr.sin_family = AF_INET; /* host byte order */
  9.     srv.local_addr.sin_port = htons(port); /* short, network byte order */
  10.     srv.local_addr.sin_addr.s_addr = INADDR_ANY; /* my IP */
  11.     bzero(&(srv.local_addr.sin_zero), 8); /* zero */
  12.  
  13.     if (bind(srv.sockfd,( struct sockaddr *)&(srv.local_addr),sizeof(struct sockaddr))== -1) {
  14.         perror("bind");
  15.         exit(1);
  16.     }
  17.  
  18.     if (listen(srv.sockfd, 5) == -1) {
  19.         perror("listen");
  20.         exit(1);
  21.     }
  22.     char tmp[128];
  23.     sprintf(tmp,"created. Binded to  %s:%d !!!???",inet_ntoa(srv.local_addr.sin_addr), srv.my_port);
  24. //FYI bug here. The previous line prints: "Binded to 0.0.0.0:1200" instead of "Binded to 127.0.0.1:1200"
  25.     log_srv(&srv,tmp);
  26.     if(!fork()) { /* first child */
  27.         while(1) { // while loop for accept()
  28.             srv.sin_size = sizeof(struct sockaddr_in);
  29.             int new_fd;
  30.             if ((new_fd = accept(srv.sockfd, (struct sockaddr *)&(srv.remote_addr),
  31.                     &(srv.sin_size))) == -1) {
  32.                 perror("accept");
  33.                 continue;
  34.             }
  35.             if(!srv.isAlive) {
  36.                 close(new_fd); //if there's a connection when the program is dead, we stop it here
  37.                 break; return;
  38.             }
  39.             char tmp[128];
  40.             sprintf(tmp, "new connection from %s:%d",inet_ntoa(srv.remote_addr.sin_addr),srv.remote_addr.sin_port);
  41.             log_srv(&srv,tmp);
  42.             if (!fork()) { /* multiclient */
  43.                 if(!srv.isAlive) {
  44.                     close(new_fd); //if there's a connection when the program is dead, we close it
  45.                     return;
  46.                 }
  47.                 char answer[MAXRECVDATA];
  48.                 sprintf(answer,"Serveur %s.\n", srv.name);
  49.                 if (send(new_fd, answer, strlen(answer), 0) == -1)
  50.                     perror("send");
  51.  
  52.                 srv.recvdata = (msg*) malloc (sizeof (msg));
  53.                 char* recvdata[MAXRECVDATA];
  54.                 bzero(srv.recvdata,sizeof (msg));
  55.                 printf("right before recv\n");
  56. //FYI: I think recv never returns because nothing happens after here
  57.                 if ((srv.numbytes=recv(new_fd, recvdata, MAXRECVDATA, 0)) == -1) {
  58.                     perror("recv");
  59.                     log_srv(&srv,"recv error!");
  60.                     exit(1);
  61.                 }
  62.                 printf("received %s",*recvdata);
  63.                 srv.recvdata = str_to_msg(*recvdata);
  64.  
  65.                 sprintf(tmp,"received by %s %s:%d",srv.name,    inet_ntoa(srv.remote_addr.sin_addr),srv.remote_addr.sin_port);
  66.                 log_msg(tmp,srv.recvdata);
  67.                 close(new_fd); // closing the connection once the message is received
  68.             }
  69.             close(new_fd); // le parent n'a pas besoin de new_fd
  70.  
  71.             while(waitpid(-1,NULL,WNOHANG) > 0); // waiting for children
  72.  
  73.         } // end while accept
  74.     } // end first child
  75.  
Any help is greatly appreciated.
Thanks in advance.
Jan 30 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.