473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sockets: Send bad address error on localhost connection

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
0 5074

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

Similar topics

1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
2
by: dariophoenix | last post by:
Hi, I am trying to encapsulate Linux sockets and POSIX threads in C++ classes (I work in Knoppix, using KDevelop). Since sockets and threads are new to me, I searched for example code and found...
10
by: 7stud | last post by:
Hi, I'm experimenting with a basic socket program(from a book), and both the client and server programs are on my computer. In both programs, I call socket.gethostname(), but I discovered that...
4
by: sd1978 | last post by:
Hi, I have placed a webservice in the webserver. When I access it from a webpage, default.aspx on a click of a button i get the following error: No connection could be made because the target...
0
by: crawfordr | last post by:
Hello, I have created a perl script that connects to a specific socket (Ip address/port) using protocall of TCP. It is the server socket script. There is also coding to manage multiple handles by...
1
by: kyosti | last post by:
Hello, I'm writing in c++ on linux. I'm trying to send several JPG-files through sockets one after another and I fail( I'm doing it in this way: first client sends the server "FBEGIN" message,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.