?
Solved

Reading from the same port twice

Posted on 2004-08-10
3
Medium Priority
?
197 Views
Last Modified: 2010-04-15
Hi,

I have an application where I would like to create two threads and inside the two threads create socket in each and listen on the same port number for UDP PDUs being sent through. What I would like to know is - is it possible to do this, ie can one receive PDU's from the same port twice? At the moment I am having trouble binding the socket on the same port number.

Cheers

Mat
0
Comment
Question by:pinmj001
3 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 1000 total points
ID: 11768662
I have faced this problem many times. In principle, it is not possible, even more in a portable way.
The solution, create a listening thread, and pass frames to both processing threads, or 3, or 4, in fact I do this with 7-10 threads.
0
 
LVL 22

Accepted Solution

by:
grg99 earned 1000 total points
ID: 11771570
This is a standard server-side setup.   The best way to do it is as jamie mentioned:

(1)  You have one thread that just loops, waiting for and reading UDP packets.  Whenever it gets a packet it spins off a worker thread to handle that packet and loops back again to waiting for a packet.

(2)  If you need higher performance, you can have a "pool" of spun-off worker threads that are pre-started, and they wait for the main thread to send them a packet to handle.  Then once the worker thread has handled the packet, it can do one of two things:

(1)  It can loop back and wait again for another packet from the main thread.  This requires that you're sure the worker thread cleans up after handling the first packet, and is in a good state to handle another one.

(2)  The worker thread could exit, then the main server could spin off another worker thread.

Note that you have to be really careful to use proper semaphore techniques to signal between the main thread and workers.  It can take quite a while to debug these complex interactions.

0
 

Author Comment

by:pinmj001
ID: 11779417
Thanks for the input guys.

I think I have solved the problem with this: setsockopt(RecvSocket,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes) which allows a new socket to bind on an address and port already in use by an existing socket.

Cheers

Mat
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question