socket bind:Address already in use

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: socket bind:Address already in use

  1. #1
    Join Date
    Nov 2006

    socket bind:Address already in use

    Dear Experts,

    I have the following error when i try to connect to port 10000.When i use the port the first time, its okie, but when i try to re-use the port, i get socket bind:address already in use error.I rougly know how to use to prevent the socket from using the exisiting address, however, am not sure if my procedure is right.For my server.cpp i have the following codes.

    nt main(){
    int Sockfd, NewSockfd, ClntLen;
    sockaddr_in ClntAddr, ServAddr;
    int Port = SERV_TCP_PORT;
    char String[MAX_SIZE];
    int Len;
    // open a TCP socket (an Internet stream socket)
    Sockfd = Socket(AF_INET, SOCK_STREAM, 0); // socket() wrapper fn

    //to prevent the binding of the port from exisiting connection
    setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(void *)&opt, sizeof(opt));
    // bind the local address, so that the client can send to server
    memset((char*)&ServAddr, 0, sizeof(ServAddr));
    ServAddr.sin_family = AF_INET;
    ServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    ServAddr.sin_port = htons(Port);

    Bind(Sockfd, (sockaddr*) &ServAddr, sizeof(ServAddr));

    the above is part of my code of server.cpp and if you realise i have used the socksockopt() fucntion between calls from Socket() and Bind().I understand this code allows ports to be reused but i am still not able to overcome the problem.Please do help how i should troubleshoot.Thanks.

  2. #2
    Join Date
    May 2007
    Do you close/unbind the port when you are finished?

  3. #3
    Join Date
    Nov 2006

    Unbind and closing

    Thanks for replying.I did not close or unbind.I am using linux image in windows platform by a vmware player.I read abt the re-use of an existing address port which is initially used.But when i implement as i have show, i stillc cant overcome.If i need to close or unbind, how to i use it?

  4. #4
    Join Date
    May 2007
    I believe you can just call the closesocket(int sockid) function. then open it back up and listen again. I'm not 100% sure though, I used winsock 1.1 in vb when I was socket programming. Try it and let me know. Also try looking up a few more socket tutorials on google.

  5. #5
    Join Date
    Jan 2007
    Specify correct IP address and an unused port , it will always work.

Similar Threads

  1. Replies: 1
    Last Post: 06-03-2008, 06:55 AM
  2. Replies: 0
    Last Post: 03-20-2007, 12:13 PM
  3. Replies: 7
    Last Post: 11-24-2006, 10:27 AM
  4. Socket Code
    By Aliasgar in forum Java
    Replies: 0
    Last Post: 04-17-2001, 07:25 AM
  5. Socket to Socket communication?
    By Ruchi Dhar in forum Java
    Replies: 2
    Last Post: 10-24-2000, 02:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center