Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

binding error

hi all,
 I try to run program to connect between client/server and use Net Activity Viewer to check to port activity. However, I have error "Address already in use" after running the second time or when i modified and recompile in Server even though i used setsockopt() already. Thanks for your help.
PS: i run on linux on vmware.
----------------------
//server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 3490

void error(const char *msg)
{
      perror(msg);
          exit(1);
}

int main(int argc, char *argv[])
{
           int sockfd, newsockfd, portno;
           socklen_t clilen;
           char buffer[256];
           struct sockaddr_in serv_addr, cli_addr;
      int n;
        int yes = 1;
   

      sockfd = socket(AF_INET, SOCK_STREAM, 0);
           if (sockfd < 0)
              error("ERROR opening socket");
           bzero((char *) &serv_addr, sizeof(serv_addr));
           

      portno = PORT;
      if(setsockopt( sockfd, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int))== -1) {
            perror("setsockopt");
            exit(1);
        }

           serv_addr.sin_family = AF_INET;
           serv_addr.sin_addr.s_addr = INADDR_ANY;

           serv_addr.sin_port = htons(portno);
           
      if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
              error("ERROR on binding!!!");
       
        listen(sockfd,5);
        clilen = sizeof(cli_addr);
     
      newsockfd = accept(sockfd,
                 (struct sockaddr *) &cli_addr,
                 &clilen);
           if (newsockfd < 0)
                error("ERROR on accept");
           bzero(buffer,256);
           n = read(newsockfd,buffer,255);
           if (n < 0) error("ERROR reading from socket");
           printf("Here is the message: %s\n",buffer);
           n = write(newsockfd,"I got your message",18);
           if (n < 0) error("ERROR writing to socket");

           close(newsockfd);
           close(sockfd);
           return 0;
}

-----------
//client
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

#define PORT 3490

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];


    portno = PORT;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
        error("ERROR opening socket");

    server = gethostbyname("ubuntu");

    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;

    bcopy((char *)server->h_addr,
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);

    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        error("ERROR connecting");
   
    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0)
         error("ERROR writing to socket");
    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0)
         error("ERROR reading from socket");
    printf("client: %s\n",buffer);
    close(sockfd);
    return 0;
}




client.c
server.c
0
BeginToLearn
Asked:
BeginToLearn
  • 12
  • 9
1 Solution
 
Infinity08Commented:
If you close down the socket gracefully, you shouldn't have this issue.

If you didn't close it down gracefully, the port should become available again after a few minutes.

If you want to force re-using the port (useful after a server crash eg.), use SO_REUSEADDR.

If that doesn't help, then it probably means that the socket is in some other state than TIME_WAIT. Use netstat to figure out what state the port is in.
0
 
Infinity08Commented:
Here's a nice detailed explanation for your perusal :

        http://hea-www.harvard.edu/~fine/Tech/addrinuse.html
0
 
BeginToLearnAuthor Commented:
after i use
   netstat | grep 3490
I can see that port is CLOSE_WAIT

I read that explanation and still get stuck.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Infinity08Commented:
>> I can see that port is CLOSE_WAIT

Aha, that's something else. And this is on the server side, right ?

Are both the client and server application ended (ie. they're no longer running) when you observe the socket in CLOSE_WAIT state ?
0
 
BeginToLearnAuthor Commented:
both  client and server ended when i run netstat hihi
0
 
BeginToLearnAuthor Commented:
could you please try them? I use command line to compile and run them . thanks.
0
 
Infinity08Commented:
I'll be offline for an hour or so now, but will be back after that ... sorry.
0
 
BeginToLearnAuthor Commented:
Sure.tks a lot
0
 
Infinity08Commented:
Ok. Sorry for that.

CLOSE_WAIT happens when for some reason, the close is not happening on the side that didn't initiate the closure.

In the code you posted however, the close seems to be correctly called. So, it looks like something is going wrong on the lower levels (the kernel, or maybe vmware).

Please check the return value of all close calls (as well as errno) to see if an error occurred for one of them, and if so which one.
0
 
BeginToLearnAuthor Commented:
I am offline for 8hrs.once I get home,i will try virtua box insfead vmware.if u can,pls check those return value.tks for ur time.i also check them once I get home
0
 
Infinity08Commented:
I'm not likely to get the same issue you get, because it's probably platform related. So, checking them on my end won't tell me anything about what's going on when you try it.

Take your time. I'll be monitoring this question as long as you wish.
0
 
BeginToLearnAuthor Commented:
Tks a lot
0
 
BeginToLearnAuthor Commented:
after add those lines,
 int t1, t2;
           if( t1= close(newsockfd) ==-1)
            error("ERROR in close newsockfd");
      printf("t1 is %d\n", t1);

            if( t2= close(sockfd) == -1)
            error("ERROR in close sockfd");
      printf("t2 is %d\n", t2);

it didn't print any t1,t2.
0
 
BeginToLearnAuthor Commented:
I am installing ubuntu on virtualbox too. so i can test it there to see what's wrong with close :)
0
 
Infinity08Commented:
Note that due to operator precedence :

>>            if( t1= close(newsockfd) ==-1)

should be :

                 if( (t1= close(newsockfd)) ==-1)


>> it didn't print any t1,t2.

That's strange, because given that code, it should always print the lines starting with "t1 is" and "t2 is", because those lines aren't inside the if block.


Please also do the same on the client side.



Btw, what's the base system that your VM is installed on ?
0
 
BeginToLearnAuthor Commented:
VMware is installed on Windows 7. I just finished installation ubuntu on virtualbox. hihi . i have a small internet so it run overnight. I am trying to figure out hhow to install Install Guest Additions. now.
0
 
BeginToLearnAuthor Commented:
After i add
    int t;
      if(( t= close(sockfd))== -1)
            printf("t is %d\n",t);
i dont see any display.
0
 
BeginToLearnAuthor Commented:
ok. here is the final conclusion. After running on VirtualBox, it's ok. No more Address in use :) So what is the reason?
0
 
Infinity08Commented:
It might be that VMWare doesn't properly forward the socket calls to Windows.

One thing I suspect eg., is that shutdown is not called.

If VirtualBox is working fine, I'd stick with that :) (it's my favorite out of the two anyway heh).
0
 
BeginToLearnAuthor Commented:
no double i must say bye bye to vmware while i work on this program. So i gonna close this question or can i ask another question here?
0
 
Infinity08Commented:
If it's directly related to this one, I'll be happy to help you out further.

If not, please open another question - keeping separate issues separated is important to keep the PAQ database of high quality :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now