Solved

binding error

Posted on 2011-02-25
21
487 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
Comment Utility
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
Comment Utility
Here's a nice detailed explanation for your perusal :

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

Author Comment

by:BeginToLearn
Comment Utility
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
Comment Utility
>> 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
Comment Utility
both  client and server ended when i run netstat hihi
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:BeginToLearn
Comment Utility
Sure.tks a lot
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
Tks a lot
0
 

Author Comment

by:BeginToLearn
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

8 Experts available now in Live!

Get 1:1 Help Now