Solved

Reading from the same port twice

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 and use switch statements in the C programming language.

770 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