CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Jan 2009

    Arrow C: 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
    // the definitions of server, mdl and msg shouldn't be necessary to understand the code
    	server srv = *(mdl->srv);
    	strcpy(srv.name, mdl->name);
    	struct hostent *he;
    	if ((he=gethostbyname("localhost")) == NULL) {
    		log_srv(&srv, "gethostbyname");
    	if ((srv.sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    		perror("Terminal: socket error");
    		log_srv(&srv, "socket error");
    	srv.remote_addr.sin_family = AF_INET; /* host byte order */
    	srv.remote_addr.sin_port = htons(port); /* short, network byte order */
    	srv.remote_addr.sin_addr = *((struct in_addr *)(he)->h_addr);
    	bzero(&(srv.remote_addr.sin_zero), 8); /* zero */
    	if (connect(srv.sockfd,( struct sockaddr *)&(srv.remote_addr), sizeof(struct sockaddr)) == -1) {
    		perror("Terminal: connect error");
    		log_srv(&srv,"connect error");
    	while(1) {
    		msg * damsg = create_msg("0000000001","Achat","0001000001","16");
    		damsg_str = msg_to_str(damsg);
    // FYI, damsg_str looks like "|0000000001|Achat|0001000001|16|"
    		log_msg("Terminal: sending...",damsg);
    		int lSent=0; char* damsg_str;
    		if ((lSent=send(srv.sockfd, damsg_str, strlen(damsg_str)+1, 0)) != 0) {
    			perror("Terminal: send error");
    			log_srv(&srv, "send error");
    //FYI, if the previous condition is "==-1" instead of "!= 0" perror doesn't show.
    // However, as said previously, perror does contain an error, which is "bad address"
    		log_smth("Terminal %d bytes sent for message %s",lSent,msg_to_str(damsg));
    Server part
    	server srv = *(mdl->srv);
    	strcpy(srv.name, mdl->name); srv.my_port=port;
    	srv.isAlive = 1;
    	if ((srv.sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    	srv.local_addr.sin_family = AF_INET; /* host byte order */
    	srv.local_addr.sin_port = htons(port); /* short, network byte order */
    	srv.local_addr.sin_addr.s_addr = INADDR_ANY; /* my IP */
    	bzero(&(srv.local_addr.sin_zero), 8); /* zero */
    	if (bind(srv.sockfd,( struct sockaddr *)&(srv.local_addr),sizeof(struct sockaddr))== -1) {
    	if (listen(srv.sockfd, 5) == -1) {
    	char tmp[128];
    	sprintf(tmp,"created. Binded to  %s:%d !!!???",inet_ntoa(srv.local_addr.sin_addr), srv.my_port);
    //FYI bug here. The previous line prints: "Binded to" instead of "Binded to"
    	if(!fork()) { /* first child */
    		while(1) { // while loop for accept()
    			srv.sin_size = sizeof(struct sockaddr_in);
    			int new_fd;
    			if ((new_fd = accept(srv.sockfd, (struct sockaddr *)&(srv.remote_addr),
    					&(srv.sin_size))) == -1) {
    			if(!srv.isAlive) {
    				close(new_fd); //if there's a connection when the program is dead, we stop it here
    				break; return;
    			char tmp[128];
    			sprintf(tmp, "new connection from %s:%d",inet_ntoa(srv.remote_addr.sin_addr),srv.remote_addr.sin_port);
    			if (!fork()) { /* multiclient */
    				if(!srv.isAlive) {
    					close(new_fd); //if there's a connection when the program is dead, we close it
    				char answer[MAXRECVDATA];
    				sprintf(answer,"Serveur %s.\n", srv.name);
    				if (send(new_fd, answer, strlen(answer), 0) == -1)
    				srv.recvdata = (msg*) malloc (sizeof (msg));
    				char* recvdata[MAXRECVDATA];
    				bzero(srv.recvdata,sizeof (msg));
    				printf("right before recv\n");
    //FYI: I think recv never returns because nothing happens after here
    				if ((srv.numbytes=recv(new_fd, recvdata, MAXRECVDATA, 0)) == -1) {
    					log_srv(&srv,"recv error!");
    				printf("received %s",*recvdata);
    				srv.recvdata = str_to_msg(*recvdata);
    				sprintf(tmp,"received by %s %s:%d",srv.name,	inet_ntoa(srv.remote_addr.sin_addr),srv.remote_addr.sin_port);
    				close(new_fd); // closing the connection once the message is received
    			close(new_fd); // le parent n'a pas besoin de new_fd
    			while(waitpid(-1,NULL,WNOHANG) > 0); // waiting for children
    		} // end while accept
    	} // end first child
    Any help is greatly appreciated.
    Thanks in advance.

  2. #2
    Join Date
    May 2001

    Re: C: send bad address error on localhost connection

    char* recvdata[MAXRECVDATA];
    you are creating an array of pointers to char and pass that to recv. While this might not screw up your server, it is definitely not correct.

    I suggest you start with a server that does not fork() so that you can debug it. Once that is working, extend it.


    BTW, you bind your server to INADRR_ANY and that's what "" indicates.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.