SDL with UDP socket

Hello everyone i’m developing OpenGL visualization application under Linux that can get data by UDP. I’ve create the new thread by SDL_CreateThread func, then i’ve the socket in this thread and sending the data via UDP from python script with frequency 25 Hz. Sometimes it causes freezing main thread or everytime some mainthread slowdown. How can i solve this problem? There is code of socket part:

Code:
void UDPServerSocket::Init()
{
bufsize=2048;
buf=new unsigned char[bufsize];
server_sockd= socket(AF_INET,SOCK_DGRAM,IPPROTO_IP);
server_address.sin_family=AF_INET;
server_address.sin_addr.s_addr=inet_addr(“127.0.0.1”);
server_address.sin_port=htons(12000);
server_len=sizeof(server_address);

bind(server_sockd,(struct sockaddr *)&server_address,server_len);
client_len=sizeof(client_address);

}
int UDPServerSocket::Process()
{
Init();
memset(buf,0,bufsize);
int readed;
fd_set rset;

while(1)
{

FD_ZERO(&rset);
FD_SET(server_sockd,&rset);
pselect(server_sockd+1,&rset,NULL,NULL,NULL,NULL);

int numBytes = 0;


readed=recvfrom(server_sockd,buf,bufsize,0,(sockaddr *)&client_address,&client_len);

		if (readed>0)
			{
			if (strcmp((const char *)buf,"ping")==0)
			{

				break;
			}
			if (qu.size()<10)
			qu.push(string((const char *)buf));
			memset(buf,0,bufsize);

			}
}
return 0;

}

Hi.

I’m not sure why you think that this is a SDL issue. When you have general
programming problems, there are general purpose sites, forums or mailing
lists you can use.

In the spirit of helping, I see the following potential problems:

  • recvfrom() is a blocking call. You are using select(), but not testing
    the values to see if it is safe to read from the socket without blocking.

  • There is a security problem in your code. What do you think would
    happen if I sent you a packed 2048 bytes long, with no NUL terminator?

  • I’m assuming your main thread reads from “qu” and this auxiliary thread
    writes to it, If so, unless you are protecting “qu”, the behaviour of your
    program is undefined.

  • You drop packets when the queue is full, without any logging. Someone
    could trivially DOS your server by sending many tiny packets, keeping the
    queue full of spam.

  • You are quite light on error checking too. Errors can sometimes
    compound one another, so you should have robust error detection, handling
    and logging to ensure that a simple early error doesn’t cascade.

Regards,
– Brian.On 29 September 2011 06:03, qmor <qmor.qmor at gmail.com> wrote:

**
Hello everyone i’m developing OpenGL visualization application under Linux
that can get data by UDP. I’ve create the new thread by SDL_CreateThread
func, then i’ve the socket in this thread and sending the data via UDP from
python script with frequency 25 Hz. Sometimes it causes freezing main thread
or everytime some mainthread slowdown. How can i solve this problem? There
is code of socket part: