关于我急需一个端口映射软件,本人不才,做不出来!!
功能很简单,只需要
比如 服务器 开放80端口
使用端口映射软件 映射所有访问 端口 123 的映射到 端口 80
结果就是 访问 等于
我需要源码,学习参考,谁能提供下!的问题
Imports System。Net
Imports System。Net。Sockets
Namespace SocketLib ‘类库文件
‘端口映射管理类
Public Class ClsPortMaper
Dim _thrReceiveMap As System。
Threading。Thread
Dim _tcpAcpSocket, _tcpMapSocket As Socket
Dim _strRemoteHost As String
Dim _intRemotePort As Integer
Dim _collectionPortMaper As ClsPortMaperCollection
Dim _mcsSendTimeout As Integer = 3000
Dim _mcsReceiveTimeout As Integer = 3000
Public TagName As String
Public Sub New(ByVal tcpAcpSocket As Socket, ByVal tcpMapSocket As Socket, ByVal collectionPortMaper As ClsPortMaperCollection)
Me。
_tcpAcpSocket = tcpAcpSocket
Me。_tcpMapSocket = tcpMapSocket
Me。
_collectionPortMaper = collectionPortMaper
_strRemoteHost = CType(_tcpAcpSocket。
RemoteEndPoint, IPEndPoint)。Address。ToString
_intRemotePort = CType(_tcpAcpSocket。RemoteEndPoint, IPEndPoint)。
Port
TagName = _strRemoteHost & “:” & _intRemotePort
End Sub
Public Sub StartService()
If Me。
_tcpAcpSocket Is Nothing Then
Throw New System。Exception(“请求连接Socket为空!”)
Return
End If
If Me。
_tcpMapSocket Is Nothing Then
Throw New System。Exception(“映射连接Socket为空!”)
Return
End If
Me。
_tcpAcpSocket。SetSocketOption(SocketOptionLevel。Socket, SocketOptionName。SendTimeout, _mcsSendTimeout)
Me。
_tcpAcpSocket。SetSocketOption(SocketOptionLevel。Socket, SocketOptionName。ReceiveTimeout, _mcsReceiveTimeout)
Me。
_tcpMapSocket。SetSocketOption(SocketOptionLevel。Socket, SocketOptionName。SendTimeout, _mcsSendTimeout)
Me。
_tcpMapSocket。SetSocketOption(SocketOptionLevel。Socket, SocketOptionName。ReceiveTimeout, _mcsReceiveTimeout)
_collectionPortMaper。
Add(Me)
_collectionPortMaper。RaiseConnectionRequest(Me。_strRemoteHost, Me。_intRemotePort, Me。
_collectionPortMaper。Count)
_thrReceiveMap = New System。Threading。Thread(AddressOf ReceiveMapService)
_thrReceiveMap。
Start()
End Sub
Private Sub ReceiveMapService()
While True
Try
If Me。
_tcpAcpSocket。Poll(0, SelectMode。SelectRead) Then
If Me。_tcpAcpSocket。
Available > 0 Then
Do
Dim bytData(1024) As Byte
Me。
_tcpAcpSocket。Receive(bytData)
Me。_tcpMapSocket。Send(bytData)
Loop While Me。
_tcpAcpSocket。Available > 0
Else
‘Client Closed
StopService(False)
Return
End If
End If
System。
Threading。Thread。Sleep(10)
If Me。_tcpMapSocket。Poll(0, SelectMode。
SelectRead) Then
If Me。_tcpMapSocket。Available > 0 Then
Do
Dim bytData(1024) As Byte
Me。
_tcpMapSocket。Receive(bytData)
Me。_tcpAcpSocket。Send(bytData)
Loop While Me。
_tcpMapSocket。Available > 0
Else
‘Client Closed
StopService(False)
Return
End If
End If
Catch ex As Exception
MessageBox。
Show(ex。Message)
Return
End Try
System。
Threading。Thread。Sleep(10)
End While
End Sub
Public Sub StopService(ByVal ForceStop As Boolean)
_tcpAcpSocket。
Shutdown(SocketShutdown。Both)
_tcpMapSocket。Shutdown(SocketShutdown。Both)
_tcpAcpSocket。
Close()
_tcpMapSocket。Close()
_collectionPortMaper。Remove(Me)
_collectionPortMaper。
RaiseConnectionClose(Me。_strRemoteHost, Me。_intRemotePort, Me。_collectionPortMaper。Count)
If ForceStop Then
_thrReceiveMap。
Abort()
End If
End Sub
End Class
‘PortMaper管理集
Public Class ClsPortMaperCollection
Inherits System。
Collections。CollectionBase
Public Event ConnectionRequest(ByVal strRemoteHost As String, ByVal intRemotePort As Integer, ByVal intTotalCount As Integer)
Public Event ConnectionClose(ByVal strRemoteHost As String, ByVal intRemotePort As Integer, ByVal intTotalCount As Integer)
Public Sub New()
End Sub
Public Function Add(ByVal objValue As ClsPortMaper) As Integer
Return List。
Add(objValue)
End Function
Public Property Item(ByVal index As Integer) As ClsPortMaper
Get
Return List(index)
End Get
Set(ByVal Value As ClsPortMaper)
List(index) = Value
End Set
End Property
Public ReadOnly Property Item(ByVal TagName As String) As ClsPortMaper
Get
For Each obj As ClsPortMaper In List
If obj。
TagName = TagName Then Return obj
Next
Return Nothing
End Get
End Property
Public Function GetIndex(ByVal TagName As String) As Integer
Dim i As Integer = 0
Do While i 0) And (mapPort >= 0) Then
Me。
_mapHost = mapHost
Me。_mapPort = mapPort
Me。
_mapEndPoint = New IPEndPoint(Dns。Resolve(_mapHost)。AddressList(0), _mapPort)
Else
Throw New Exception(“初始化参数错误!”)
Return
End If
End Sub
Public Sub Start()
_threadListen = New System。
Threading。Thread(AddressOf ListenService)
_threadListen。Start()
End Sub
Private Sub ListenService()
While True
Do While _listener。
Pending
Dim socAccept As Socket = _listener。AcceptSocket()
Dim socMap As New Socket(AddressFamily。
InterNetwork, SocketType。Stream, ProtocolType。Tcp)
Try
socMap。
Connect(_mapEndPoint)
Catch ex As Exception
MessageBox。
Show(“MapHost连接错误!” & ex。Message, “注意”, MessageBoxButtons。OK, MessageBoxIcon。Error)
Return
End Try
Dim objPortMaper As New ClsPortMaper(socAccept, socMap, _portMaperCollection)
objPortMaper。
StartService()
Loop
System。Threading。Thread。Sleep(100)
End While
End Sub
Private Sub _portMaperCollection_ConnectionRequest(ByVal strRemoteHost As String, ByVal intRemotePort As Integer, ByVal intTotalCount As Integer) Handles _portMaperCollection。
ConnectionRequest
RaiseEvent ConnectionRequest(strRemoteHost, intRemotePort, intTotalCount)
End Sub
Private Sub _portMaperCollection_ConnectionClose(ByVal strRemoteHost As String, ByVal intRemotePort As Integer, ByVal intTotalCount As Integer) Handles _portMaperCollection。
ConnectionClose
RaiseEvent ConnectionClose(strRemoteHost, intRemotePort, intTotalCount)
End Sub
Public Sub StopServer()
_threadListen。
Abort()
_listener。Stop()
While _portMaperCollection。Count > 0
_portMaperCollection。
Item(0)。
StopService(True)
End While
End Sub
End Class
End Namespace 。