Discussion:
Lag when receive data from Broadcast UPD ...
(too old to reply)
Thomas Couronné
2005-01-05 14:59:39 UTC
Permalink
Hi list,

I've made a server that broadcasts and a client socket that receives
the UDP Datagrams but I have some lag when I receive data...

Here the initialisation code for the server and client socket :

// Init Winsock
WSADATA wsaData;
WSAStartup(MAKEWORD(1, 1), &wsaData) ;

// Create Sending socket
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ;

// Set socket options
BOOL Val = TRUE;
setsockopt(Socket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*>(&Val), sizeof(BOOL) ;

setsockopt(Socket, SOL_SOCKET, SO_BROADCAST,
reinterpret_cast<char*>(&Val), sizeof(BOOL) ;

setsockopt(Socket, SOL_SOCKET, SO_DONTROUTE,
reinterpret_cast<char*>(&Val), sizeof(BOOL) ;

// Socket address
AddrInfo.sin_family = AF_INET ;
AddrInfo.sin_port = htons(Port) ;
AddrInfo.sin_addr.s_addr = INADDR_BROADCAST ;

// non-blocking mode
WSAAsyncSelect(Socket, NULL, 0, FD_WRITE | FD_CLOSE) ;


//---------------------------------------
// Create listening socket
ListeningSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ;

setsockopt(ListeningSocket, SOL_SOCKET, SO_REUSEADDR,
reinterpret_cast<char*>(&Val), sizeof(BOOL) ;

setsockopt(ListeningSocket, SOL_SOCKET, SO_BROADCAST,
reinterpret_cast<char*>(&Val), sizeof(BOOL) ;

setsockopt(ListeningSocket, SOL_SOCKET, SO_DONTROUTE,
reinterpret_cast<char*>(&Val), sizeof(BOOL) ;

// Socket address
AddrInfoL.sin_family = AF_INET ;
AddrInfoL.sin_port = htons(Port) ;
AddrInfoL.sin_addr.s_addr = INADDR_ANY ;

// Bind the socket
bind(ListeningSocket, (LPSOCKADDR) &AddrInfoL, sizeof(AddrInfoL)) ;

WSAAsyncSelect(ListeningSocket, NULL, 0, FD_READ | FD_CLOSE) ;


Errors were checked all the way and there were none


Here is the code that broadcasts :

sendto(Socket, reinterpret_cast<const char*>(static_cast<const
float*>(RenderCam->GetGlobalTransform())), sizeof(float)*16, 0,
(LPSOCKADDR) &AddrInfo, sizeof(AddrInfo)) ;


And here is the code that receives data :

char buffer [sizeof(float)*16] ;
recv(ListeningSocket, buffer, sizeof(buffer), 0) ;


1 - Is there something wrong in that code ??
2 - Is there any reason for that lag ( a lot when it start to emmit,
few after ) ?

Regards
Thomasor
David Schwartz
2005-01-05 17:37:55 UTC
Permalink
Post by Thomas Couronné
1 - Is there something wrong in that code ??
Lack of error checking. Most likely, lack of a protocol defined at the
byte level. All of these are bad, but none of them probably explain what you
are seeing, I think. It's hard to be sure, since you don't describe what you
expect and what actually happens.
Post by Thomas Couronné
2 - Is there any reason for that lag ( a lot when it start to emmit,
few after ) ?
What do you mean by "lag"? Is it possible you're just seeing packet
loss? How much time are you talking about exactly?

DS

Loading...