PC Review


Reply
Thread Tools Rate Thread

Asynchronous UDP - Can multiple ports be used?

 
 
=?Utf-8?B?RGF2aWQgRXZhbnM=?=
Guest
Posts: n/a
 
      20th Apr 2005
I am creating an application that I am attempting to use Async UDP to relay
UDP messages from one network to another. I have 2 PCs A and B on two
separate networks and a central server C that is on both networks. PC A
sends data on port 1234 and receives on 1235, PC B is the inverse. Server C
needs to listen on 1234 for messages from PC A and then send to PC B on port
1235 and likewise needs to listen on 1235 for messages from PC B and send
messages to PC A on port 1234. So, when C receives a message on 1234, it
needs to forward it on 1234 to PC B.

I have created an application that binds port 1234 and 1235 (via a socket)
with an asynchronous handler. The first message that is sent (in either)
direction is received and forwarded properly. However, any future response,
regardless of incoming port, drops into the handler for the first message
received. So, if the first message is received on 1234, all subsequent
packets fall into the handler for communication on 1234.

Any ideas on why this happens? Is there an alternate approach to forward
UDP packets across subnets?

Listen:
' Establish the local endpoint for the socket.
' Dns.GetHostName returns the name of the host running the
application.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddr As IPAddress = ipHostInfo.AddressList(0)
Dim EPSSE2Listen As New IPEndPoint(ipAddr, 1234)

' Create a UDP socket.
Dim sockSSE2 As New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)

' Bind the socket to the local endpoint and listen for incoming
connections.
Try
sockSSE2.Bind(EPSSE2Listen)

Dim stateSSE2 As New StateObject
'Dim IPEPAll As New IPEndPoint(IPAddress.Any, 1234)
'Dim EPAll As EndPoint = CType(IPEPAll, EndPoint)

'Populate the state object with the socket that we have bound
stateSSE2.workSocket = sockSSE2

'Set the asynchronous receive
sockSSE2.BeginReceive(stateSSE2.buffer, 0, stateSSE2.BufferSize,
SocketFlags.None, New AsyncCallback(AddressOf UDPReceive), stateSSE2)

Catch e As Exception
Console.WriteLine(e.ToString())
End Try

Handler:
Dim stateReceive As StateObject = CType(ar.AsyncState, StateObject)
'Get the socket that we have been listening on
Dim sockManager As Socket = stateReceive.workSocket
Dim sDataIn As String

' Read data from client socket.
Dim bytesRead As Integer = sockManager.EndReceive(ar)

'COnvert the text
sDataIn = Encoding.ASCII.GetString(stateReceive.buffer, 0, bytesRead)

'Write the data to the console window
Console.WriteLine(sDataIn)

'Replace the IP address so the client responds to us
sDataIn = Replace(sDataIn, Left(sDataIn, InStr(sDataIn, "|") - 1),
sIPAddress)

Console.WriteLine(sDataIn)

'Forward the request to the destination client
SendUDPMessage("192.168.1.52", 1234, sDataIn,
bytesRead,sockmanager)
'Wait for the next response
sockManager.BeginReceive(stateReceive.buffer, 0,
stateReceive.BufferSize, SocketFlags.None, New AsyncCallback(AddressOf
UDPReceive), stateReceive)

Send:
'Set the destination IP address
Dim IPEPSendTo As New IPEndPoint(IPAddress.Parse(sIPAddress), iPort)
'Create the end point for the IP address
Dim EPSend As EndPoint = CType(IPEPSendTo, EndPoint)
'Create a new UDP socket to send
'Dim sockSend As New Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp)
Dim byteBuffer As Byte() = Encoding.ASCII.GetBytes(sBuffer)

'Send the buffer data
sockSend.SendTo(byteBuffer, 0, iByteLen - 1, SocketFlags.None, EPSend)

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Asynchronous I/O completion ports vs multi-threading Carl Daniel [VC++ MVP] Microsoft Dot NET 1 24th Nov 2007 05:50 AM
Multiple asynchronous sockets Ryan Microsoft Dot NET Framework 9 24th Jan 2007 09:28 PM
Multiple asynchronous sockets Ryan Microsoft Dot NET Framework 0 24th Jan 2007 02:30 PM
Asynchronous I/O or multiple threads? Quiet Man Microsoft C# .NET 6 2nd Dec 2004 04:18 AM
I/O Completion Ports vs. Asynchronous I/O Josh Microsoft Dot NET 3 10th Jul 2003 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.