Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello evenyone:
I have this problem for several hours.Let's have a look at my code below:

C++
BOOL bReuseAddr=TRUE;
setsockopt(m_sConnServer,SOL_SOCKET,SO_REUSEADDR,(char*)&bReuseAddr,sizeof(BOOL));
bind(m_sConnServer,(sockaddr*)&bindAddr,sizeof(sockaddr));

setsockopt(m_sListen,SOL_SOCKET,SO_REUSEADDR,(char*)&bReuseAddr,sizeof(BOOL));
bind(m_sListen,(sockaddr*)&bindAddr,sizeof(sockaddr));

setsockopt(m_sHole,SOL_SOCKET,SO_REUSEADDR,(char*)&bReuseAddr,sizeof(BOOL));
bind(m_sHole,(sockaddr*)&bindAddr,sizeof(sockaddr));

if(connect(m_sConnServer,(sockaddr*)&m_serverAddr,sizeof(sockaddr))==0)
      send(m_sConnServer,(char*)peer,sizeof(PEER_INFO),0);

listen(m_sListen,20);

Sleep(4000); //让服务器有时间处理之前的连接
for(int i=0;i!=10;i++)
{
   if(connect(m_sHole,(sockaddr*)&m_serverAddr,sizeof(sockaddr))==0) //When it comes here,it always notice "WSAEADDRINUSE" error by WSAGetLastError()
   {
       cout<<"send "<<send(m_sHole,(char*)peer,sizeof(PEER_INFO),0)<<endl;
       break;
   }
}

It works as i want it to be until reach the line i add comment.

I have read msdn for many times,but i don't understand "When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf." well.

I want to know what's the meaning of the archives' explaination,and How to avoid such
Error.Thank you very much~
Posted
Updated 11-Sep-11 4:09am
v2

1 solution

http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver[^]Please go thru the basics of client and server side sockets!


seems, You try to use the application as both server and a client!

Please have a separate application for a client and for a serer.
And you find u get no error!


Thats the casue u get the error.

to Create a stream socket server:

1. create socket
2. bind the socket
3. listen for any connections
4. accepts the any connection received.

to create a stream socket client:
1. create a socket
2. connect to the server
3. communicate with the server
 
Share this answer
 
v2
Comments
Caladium 12-Sep-11 6:10am    
HI Arun.Yes you are right,i want to create an application as both server and client because i am learning about ptp communications.In order to connect with another client,one have to communicate with server.But others can connect it to download resources,so it must be listening for coming connect.What confused me is that when i change the three setsockopt(...)'s situation,thing's changed.Sometimes it can use the two connect socket correctly,but sometimes a connect socket and the listen socket.Funny~

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900