Asynchronous UDP - Can multiple ports be used?

G

Guest

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top