I've been searching a lot and found out that the server based on UDP should have only 1 socket that is doing all the job for every single client. And each client should have his own socket bound to unique port but can exist on the same ip? I'm trying to run a server and connect to it more than 1 client. 1 client connects normally but I can't bind the socket for the second client. Everything takes place on one the same PC and the server is localhost. The same code is used to create and bind a socket both for server and client(and comes from gafferongames.com):
bool Open( unsigned short port )
{
assert( !IsOpen() );
// create socket
socket = ::socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
if ( socket <= 0 )
{
printf( "failed to create socket\n" );
socket = 0;
return false;
}
// bind to port
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( (unsigned short) port );
if ( bind( socket, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 )
{
printf( "failed to bind socket\n" ); //!!!!!!!!!!!!! - this message pops up when trying to connect with second client
Close();
return false;
}
// set non-blocking io
#if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
int nonBlocking = 1;
if ( fcntl( socket, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 )
{
printf( "failed to set non-blocking socket\n" );
Close();
return false;
}
#elif PLATFORM == PLATFORM_WINDOWS
DWORD nonBlocking = 1;
if ( ioctlsocket( socket, FIONBIO, &nonBlocking ) != 0 )
{
printf( "failed to set non-blocking socket\n" );
Close();
return false;
}
#endif
return true;
}
What could be the issue? I'm a real beginner and really can't find the solution so I would be very grateful for any advice!