Impossible to receive datas using UDP (sdl! Please help me

Hello

I’m trying to use SDLnet.
I have a program with a client and a server side, so a client is a server too.

The program is in 2 parts: connection in TCP to get IP,
and then data send/recv are UDP.
First, a client connects to a server using TCP.
Then, after a short exchange of data, the server and the client initialize an UDP engine:

I use a short structure for that:

Code:
struct Communication{
//A communication:
//2 parts:
//Client send something
Uint16 outPortIn;//a port to receive datas in response
UDPsocket outSocketIn;//a socket to receive datas in response
Uint16 outPortOut;//a port to send datas
UDPsocket outSocketOut;//a socket to send datas
//Server send something
Uint16 inPortIn;//a port to receive datas
UDPsocket inSocketIn;//a socket to receive datas
Uint16 inPortOut;//a port to send datas in response
UDPsocket inSocketOut;//a socket to send datas in response

SDL_Thread* serverThread;//Server thread for the client
Uint32 ip;//Ip of the client

};

Note: below, j is the starting port number, so if I use j=1, the ports 1,2,3 and 4 will be used.

Server side:

Code:

    IPaddress* ipaddress = SDLNet_TCP_GetPeerAddress(tempSocket);
    Uint32 ipp = ipaddress->host;
    SDLNet_TCP_Close(tempSocket);

    IPaddress srvadd;
    char ipe[16];
    sprintf(ipe,"%d.%d.%d.%d",ipp%256,(ipp/256)%256,(ipp/256/256)%256,ipp/256/256/256);

    //Cr?ation d'une communication
    communications[i] = new Communication;
    int j=port;
    communications[i]->inPortIn=j+2;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->inPortIn);
    communications[i]->inPortIn = srvadd.port;
    communications[i]->inSocketIn = SDLNet_UDP_Open(j+2);
    if(communications[i]->inSocketIn == NULL) exit(15);
    communications[i]->inPortOut=j+3;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->inPortOut);
    communications[i]->inPortOut = srvadd.port;
    communications[i]->inSocketOut = SDLNet_UDP_Open(j+3);
    if(communications[i]->inSocketOut == NULL) exit(15);
    communications[i]->outPortIn=j;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->outPortIn);
    communications[i]->outPortIn = srvadd.port;
    communications[i]->outSocketIn = SDLNet_UDP_Open(j);
    if(communications[i]->outSocketIn == NULL) exit(15);
    communications[i]->outPortOut=j+1;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->outPortOut);
    communications[i]->outPortOut = srvadd.port;
    communications[i]->outSocketOut = SDLNet_UDP_Open(j+1);
    if(communications[i]->outSocketOut == NULL) exit(15);

    communications[i]->ip = ipp;

Client side:

Code:

IPaddress* ipaddress = SDLNet_TCP_GetPeerAddress(tempSocket);
SDLNet_TCP_Close(tempSocket);

    IPaddress srvadd;
    char ipe[16];
    communications[i] = new Communication;
    communications[i]->ip = ipaddress->host;
    sprintf(ipe,"%d.%d.%d.%d",communications[i]->ip%256,(communications[i]->ip/256)%256,(communications[i]->ip/256/256)%256,communications[i]->ip/256/256/256);

    communications[i]->inPortIn=j+1;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->inPortIn);
    communications[i]->inPortIn = srvadd.port;
    communications[i]->inSocketIn = SDLNet_UDP_Open(j+1);
    communications[i]->inPortOut=j;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->inPortOut);
    communications[i]->inPortOut = srvadd.port;
    communications[i]->inSocketOut = SDLNet_UDP_Open(j);
    if(communications[i]->inSocketOut == NULL) exit(15);

    communications[i]->outPortIn=j+3;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->outPortIn);
    communications[i]->outPortIn = srvadd.port;
    communications[i]->outSocketIn = SDLNet_UDP_Open(j+3);
    if(communications[i]->outSocketIn == NULL) exit(15);
    communications[i]->outPortOut=j+2;
    SDLNet_ResolveHost(&srvadd,ipe,communications[i]->outPortOut);
    communications[i]->outPortOut = srvadd.port;
    communications[i]->outSocketOut = SDLNet_UDP_Open(j+2);
    if(communications[i]->outSocketOut == NULL) exit(15);
    communications[i]->ip = srvadd.host;

The TCP part works perfectly.
Only the udp part is the problem.
Datas are NEVER received by any computer!

Send datas function:

Code:
UDPpacket* packet = SDLNet_AllocPacket(size);
packet->address.host = communications[comm]->ip;
if(sortant) packet->address.port =communications[comm]->outPortOut;
else packet->address.port =communications[comm]->inPortOut;
packet->channel = -1;
packet->len = size;
int i;
if(sortant) i= SDLNet_UDP_Send(communications[comm]->outSocketOut,-1, packet);
else i = SDLNet_UDP_Send(communications[comm]->outSocketIn,-1, packet);
delete packet;
return i;

Receive datas function:

Code:
UDPpacket* packet = SDLNet_AllocPacket(1024);
packet->address.host = communications[comm]->ip;
if(sortant) packet->address.port = communications[comm]->outPortIn;
else packet->address.port = communications[comm]->inPortIn;
packet->channel = -1;
while(packet->len!=1){
printf(".");
//SDL_Delay(1);
if(sortant) SDLNet_UDP_Recv(communications[comm]->outSocketIn, packet);
else SDLNet_UDP_Recv(communications[comm]->inSocketIn, packet);
}
for(int i=0;ilen;i++) buffer[i] = packet->data[i];
delete packet;
return packet->len;

What am I doing wrong?
I know that it is possible to use UDP without BIND() function.
Please help me!!
Thank you for any help you could provide :)!

Hi

Can you give us a high level overview of what you are trying to achieve? Why
are you using TCP during setup if you want to use UDP for the data
transmission? You are aware that there are libraries which implement
so-called “reliable” UDP, which allow you to specify data which must be
received? Its generally a bad idea to write IP addresses and ports into
packets to specific a return connection, that is a great way to make your
program fail in the presence of NAT.

Your code contains bugs. Your UDP receive code will only terminate the
receive loop if it gets a data packet with exactly 1 byte in it. If this
ever does happen, it will proceed into an infinite loop that will eventually
crash or corrupt memory due to a bounds overrun on the buffer array. You
should use the matching “SDLNet_FreePacket” function rather than “delete”,
or you will proably get heap corruption errors.

Your should check the return value of all SDL and SDL_net functions. For
example, the SDLNet_UDP_Recv() documentation* specifies that the return
value will indicate the presence or absence of errors and packets, or if
nothing occurred at all.

Finally, once you’ve fix the things I’ve mentioned above, try using a
debugger. Had you traced through the code with a debugger you might have
spotted the infinite loops.

Hello

I’m trying to use SDLnet.
I have a program with a client and a server side, so a client is a server
too.

The program is in 2 parts: connection in TCP to get IP,
and then data send/recv are UDP.
First, a client connects to a server using TCP.
Then, after a short exchange of data, the server and the client initialize
an UDP engine: