- Код: Выделить всё
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Module Module1
Private Structure arrstream
Dim St1 As System.Net.Sockets.NetworkStream
Dim St2 As System.Net.Sockets.NetworkStream
End Structure
Sub Main()
Console.Write("Server starting... " & vbCrLf)
Dim Thread = New System.Threading.Thread(AddressOf tListener)
Thread.Start()
End Sub
Private Sub tListener()
Dim Client As Net.Sockets.TcpClient
Dim Stream As System.Net.Sockets.NetworkStream
Dim Stream2 As System.Net.Sockets.NetworkStream
Dim st As arrstream
'Dim Server = New TcpClient
Dim Listener = New TcpListener(Net.IPAddress.Any, 1234)
Listener.Start()
While True
Client = Listener.AcceptTcpClient()
Try
Console.Write("new client: " & Client.Client.RemoteEndPoint.ToString & vbCrLf)
Dim Server = New TcpClient("10.10.1.1", "8888")
Stream = Client.GetStream()
Stream2 = Server.GetStream()
st.St1 = Stream
st.St2 = Stream2
Dim Thread = New System.Threading.Thread(AddressOf srv1)
Thread.Start(st)
Thread.Join()
Dim Thread2 = New System.Threading.Thread(AddressOf srv2)
Thread2.Start(st)
Thread2.Join()
Catch ex As Exception
Console.Write(ex.Message & vbCrLf)
End Try
End While
End Sub
Private Sub srv1(ByVal st As arrstream)
Dim byte1(1024) As [Byte] 'Buffer
Dim message As String
'Dim st As arrstream
Dim Stream As System.Net.Sockets.NetworkStream
Dim Stream2 As System.Net.Sockets.NetworkStream
Stream = st.St1
Stream2 = st.St2
Try
While True
message = Stream.Read(byte1, 0, byte1.Length)
If Not message > 0 Then
Stream.Close()
Stream2.Close()
Exit Sub
End If
message = System.Text.Encoding.GetEncoding("windows-1251").GetString(byte1, 0, message)
Stream2.Write(byte1, 0, message.Length)
End While
Catch ex As Exception
Console.Write(ex.Message & vbCrLf)
End Try
End Sub
Private Sub srv2(ByVal st As arrstream)
Dim byte1(1024) As [Byte] 'Buffer
Dim message As String
Dim Stream As System.Net.Sockets.NetworkStream
Dim Stream2 As System.Net.Sockets.NetworkStream
Stream = st.St1
Stream2 = st.St2
Try
While True
message = Stream2.Read(byte1, 0, byte1.Length)
If Not message > 0 Then
Stream.Close()
Stream2.Close()
Exit Sub
End If
message = System.Text.Encoding.GetEncoding("windows-1251").GetString(byte1, 0, message)
Stream.Write(byte1, 0, message.Length)
End While
Catch ex As Exception
Console.Write(ex.Message & vbCrLf)
End Try
End Sub
End Module
Если коротко, то сервер сервер должен подключать сколько угодно клиентов и всё что идет на порт 1234, пересылать на 10.10.1.1:8888.
Проблемы. При подключении постоянно выделяется дополнительные 24-44 Кб.
И просьба спецам, оптимизировать и указать какие улучшения можно сделать.