Solved

Reading from the same port twice

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 77
Linux context switch  - loop takes long time to process 6 109
why "." vs "->" 23 119
Problem to show menu 5 87
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

919 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

18 Experts available now in Live!

Get 1:1 Help Now