Skip to main content
GameDev.net gamedev.net
🔒 Locked

UDP Connection after proxy server

Started by Leaguefun Jan 9, 2003 at 4:17 AM 10 replies 350+ views
Original Post
Leaguefun
Leaguefun
How can I get connect from server to client thru UDP connection for which client located behind proxy ?
Soul
Soul
I assume the client send to the server first, otherwise it''s a no go.
But when a client sends a UDP packet through a proxy the proxy creates a path back to client, all you have to do is on the server, read the IP/Port from where the packet came and reply to it, don''t send the IP/port in a packet as the proxy can''t change this. To get the IP/Port use the sockaddr structure returned by recvfrom.
You need to make sure you send your UDP packets fairly frequently else the proxy will timeout the route.

This assumes the proxy is NAT based.

hope this helps
Leaguefun
Leaguefun
Thanks a lot !!!!
Leaguefun
Leaguefun
Sorry, do you mean that I need to sent back message by using the IP/port got from recvfrom and then the proxy server will resolve those IP/port into former outgoing address ?
Soul
Soul
Yeah that the way.

client sends data to server
server looks at sockaddr struct from recvfrom
server sends packet back to ip/port from sockaddr
proxy deals with sending it to the right place
Leaguefun
Leaguefun
Sorry , I have a question.

As my server got the client incoming IP/port, and sending message by it, is there anywhere need to be done if client cannot receive the server message ?
Soul
Soul
The client should be able to recv the data just by calling recvfrom
Leaguefun
Leaguefun
As my server already sent packet by using the IP/port from recvfrom, but client also cannot receive those message as behind proxy server. What''s missing ? Thanks.
Soul
Soul
Mail me your code and I''ll have a look

e-mail: stephen.lowe@zen.co.uk
Leaguefun
Leaguefun
I''d created a class to receive incoming message by thread. Within the thread procedure, I''d used the following to receive message :

recvfrom( listenSocket, inBuffer, 4096, 0, (SOCKADDR *)&fromAddress, &fromLen );

After that I''d also created a host list to maintain the IP/port, so after receive the incoming message, I''ll update the IP/port for the host. By using that informations, I''ll sent message to appropriate host as following :

SOCKADDR_IN remoteAddr;
unsigned short result;

memset( &remoteAddr, 0, sizeof( SOCKADDR_IN ) );
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_addr.s_addr = htonl( pHost->GetAddress() );
remoteAddr.sin_port = htons( pHost->GetPort() );

result = sendto( sendSocket,
outBuffer,
count,
0,
(SOCKADDR *)&remoteAddress,
sizeof( SOCKADDR ) );

Hope you can understand my words, thanks.
Leaguefun
Leaguefun
Sorry, I''d got something confused, as I''ll need to create a listening port for incoming message, so server can just simply send back message by the client IP and the pre-defined client listening port in normal case. If server sent back message to client by using IP/port read from recvfrom(), since the outgoing port is randomly selected, so the client will not be able to receive those message , is it right ? I
''d messed up my mind already .... below is the code for me to setting up sending port :

d_sendSocket = socket( AF_INET, SOCK_DGRAM, 0 );
if( d_sendSocket == INVALID_SOCKET )
{
// error handling
}

SOCKADDR_IN localAddr;
int result;

memset( &localAddr, 0, sizeof( SOCKADDR_IN ) );
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl( INADDR_ANY );
localAddr.sin_port = htons( 0 );

result = bind( d_sendSocket, (sockaddr *)&localAddr, sizeof( SOCKADDR_IN ) );
if( result == SOCKET_ERROR )
{
// error handling
}

unsigned long int val;

val = 1; // Anything non-zero.
result = ioctlsocket( d_sendSocket, FIONBIO, &val );
if( result )
{
// error handling
}
Prefect
Prefect
Umm, are you saying that you create a new socket for every client? Don''t do that, it won''t work because the new socket sends from a different port, and the NAT proxy will no longer be able to route the packets correctly.
Use one, and only one socket for sending and receiving packets to/from _all_ clients.

cu,
Prefect
Widelands - laid back, free software strategy

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.