You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, December 27th 2011, 3:18pm

Server using QUdpSocket

Hi,

i'm creating an udp server in my game using udp comunication. I woudl like to know if my approach is correct. Becouse of connectionless character of udp I use one thread that wait for new datagram and send it to working thread:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(1)
{
while (udpSocket->hasPendingDatagrams()) {
     	QByteArray datagram;
     	datagram.resize(udpSocket->pendingDatagramSize());
     	QHostAddress sender;
     	quint16 senderPort;

     	udpSocket->readDatagram(datagram.data(), datagram.size(),
                             	&sender, &senderPort);

     	sendDatagramToProcessInDifferentThread(datagram);
}
}


Is this good approach, or I should create many QUdpSocket with different addres in bind function?

2

Wednesday, December 28th 2011, 12:29pm

this way your thread will always be looping around and around. The bind method would let the thread be idle.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Thursday, December 29th 2011, 7:47am

this way your thread will always be looping around and around. The bind method would let the thread be idle.
Yes, but i will have a heavy load and big number of clients applications.

4

Thursday, December 29th 2011, 11:55am

it's a relative to CPU vs network speed and # clients.

why would you choose a method that doesn't want to share CPU vs one that would?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Friday, December 30th 2011, 10:39am

Program will be without GUI. One thread will continously wait and read data from socket, and another thread will do all computations.
it's a relative to CPU vs network speed and # clients.

why would you choose a method that doesn't want to share CPU vs one that would?

6

Friday, December 30th 2011, 12:43pm

why would you choose a method that doesn't want to share CPU vs one that would?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Monday, January 2nd 2012, 9:05am

Why you say that this method don't share CPU? Using threads share CPU.
why would you choose a method that doesn't want to share CPU vs one that would?

8

Monday, January 2nd 2012, 12:21pm

Source code

1
2
3
while(1)
{...
}


The above code with no allowance for sleeping/switching is not 'nice' code - It does not want to give up any cpu time. Just because you make threads, doesn't mean that each thread will 'play nice'.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar threads

Used tags

qudpsocket, server, udp