Solved

Reading from the same port twice

Posted on 2004-08-10
3
186 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 250 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 250 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now