socket bind:Address already in use
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.
int Sockfd, NewSockfd, ClntLen;
sockaddr_in ClntAddr, ServAddr;
int Port = SERV_TCP_PORT;
// 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.
Do you close/unbind the port when you are finished?
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?
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.
Specify correct IP address and an unused port , it will always work.
By Varsha1717 in forum Java
Last Post: 06-03-2008, 07:55 AM
Last Post: 03-20-2007, 01:13 PM
Last Post: 11-24-2006, 11:27 AM
By Aliasgar in forum Java
Last Post: 04-17-2001, 08:25 AM
By Ruchi Dhar in forum Java
Last Post: 10-24-2000, 03:00 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center