Hmmm, ok this might throw a wrinkle into things a bit. It sounded like you
wanted to build something like a NAT proxy or something, but since you want
to run this process on the same machine that might take some doing.
(BEWARE - Below is off the cuff, might be broken, might have syntax errors
code examples)
It is simple to write a function that listens on a port:
Dim mSideAListenerPort As Int32
Dim mSideAListener As TcpListener
Dim mSideAClient As TcpClient
Dim mSideAIp As IPAddress
Dim mSideANetworkStream As NetworkStream
Dim mSideAReader As BinaryReader
Dim mSideAWriter As BinaryWriter
mSideAIp = IPAddress.Parse("192.168.0.1")
mSideAListener = New TcpListener(mSideAIp, 23)
mSideAListener.Start()
mSideAClient = New TcpClient
mSideAClient = mSideAListener.AcceptTcpClient 'Listens for a client to
connect to it
mSideANetworkStream = mSideAClient.GetStream
mSideAReader = New BinaryReader(mSideANetworkStream)
mSideAWriter = New BinaryWriter(mSideANetworkStream)
Then create another function to make an outbound connection:
Dim mSideBServerPort As Int32
Dim mSideBClient As TcpClient
Dim mSideBIp As IPAddress
Dim mSideBNetworkStream As NetworkStream
Dim mSideBWriter As BinaryWriter
Dim mSideBReader As BinaryReader
mSideBIp = IPAddress.Parse("192.168.0.2")
mSideBClient = New TcpClient
mSideBClient.Connect(mSideBIp, 8000)
mSideBNetworkStream = mSideBClient.GetStream
mSideBReader = New BinaryReader(mSideBNetworkStream)
mSideBWriter = New BinaryWriter(mSideBNetworkStream)
Then use the Binary Readers and Writers (in the correct order) to read from
the input and write that data to the output:
While Connected 'In one thread
mSideBWriter.Write(mSideAReader.Read)
Loop
While Connected 'In another thread
mSideAWriter.Write(mSideBReader.Read)
Loop
Of course you would need to do the cross reading and writing in different
threads because they need to happen at the same time and you will need to
make sure that you keep your readers and writers matched up otherwise you
will be crossing data.
The problem is that you want to run this on the same machine. I am not sure
how you would accomplish this to be honest. This might really take something
lower level to grab network packets before they get to the interface and
then send them on to the interface on a different port number. Something
that plugs into the IP stack. This is not something that I think you can
develop in .NET because I don't think it would be doable with managed code
at that level.
You could try this out on a second machine and see if you could get it
working then just figure out how to shoehorn it into the same machine. Keep
in mind that the one thing that keeps the processes of routers (relatively)
simple is that they are separate machines with their own destination
addresses and operate at a much lower level on the stack than this.
Also keep in mind that this example would only really cover TCP, no UDP
streams, they would have to be taken care of differently because they are a
different protocol.
Good luck. Hope I helped put this into perspective a bit.