Solved

binding error

Posted on 2011-02-25
21
489 Views
Last Modified: 2012-06-27
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
Comment
Question by:BeginToLearn
  • 12
  • 9
21 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34981055
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 34981072
Here's a nice detailed explanation for your perusal :

        http://hea-www.harvard.edu/~fine/Tech/addrinuse.html
0
 

Author Comment

by:BeginToLearn
ID: 34981195
after i use
   netstat | grep 3490
I can see that port is CLOSE_WAIT

I read that explanation and still get stuck.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34981385
>> 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
 

Author Comment

by:BeginToLearn
ID: 34981409
both  client and server ended when i run netstat hihi
0
 

Author Comment

by:BeginToLearn
ID: 34981423
could you please try them? I use command line to compile and run them . thanks.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34981434
I'll be offline for an hour or so now, but will be back after that ... sorry.
0
 

Author Comment

by:BeginToLearn
ID: 34981761
Sure.tks a lot
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34982603
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
 

Author Comment

by:BeginToLearn
ID: 34983188
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 53

Expert Comment

by:Infinity08
ID: 34983660
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
 

Author Comment

by:BeginToLearn
ID: 34984041
Tks a lot
0
 

Author Comment

by:BeginToLearn
ID: 34985641
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
 

Author Comment

by:BeginToLearn
ID: 34986177
I am installing ubuntu on virtualbox too. so i can test it there to see what's wrong with close :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34986317
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
 

Author Comment

by:BeginToLearn
ID: 34987682
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
 

Author Comment

by:BeginToLearn
ID: 34987776
After i add
    int t;
      if(( t= close(sockfd))== -1)
            printf("t is %d\n",t);
i dont see any display.
0
 

Author Comment

by:BeginToLearn
ID: 34987964
ok. here is the final conclusion. After running on VirtualBox, it's ok. No more Address in use :) So what is the reason?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34988141
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
 

Author Comment

by:BeginToLearn
ID: 34988199
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 34988233
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 46
Better understanding on C++ Class serialization and formats 9 61
wordmultiple challenge 12 121
Why is compiler in oracle server ? 9 45
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now