通过vb6中的代理将客户端请求转发给另一台服务器

时间:2014-01-22 14:54:34

标签: vb6

我有两个vb6应用,一个是客户端应用&另一个是服务器应用程序,都使用winsock控件进行通信我想要的是:

我需要做的,或尝试使服务器的应用程序自动“端口转发”从客户端到另一个服务器的传入连接。

实施例: 客户端的IP为0.0.0.0 我的IP是1.1.1.1 计划的IP是2.2.2.2

客户端与程序之间的正常程序连接是客户端将正常连接到程序,并将客户端的IP编程为0.0.0.0

使用此Serverapp,当Client(0.0.0.0)连接到My IP(1.1.1.1)时,My IP(Server app)将接受来自Client的连接,并将客户端连接到Program IP(2.2.2.2)。因此,当程序读取客户端IP时,它将读作1.1.1.1。

那么如何实现这个..请发布示例代码

我尝试在服务器应用程序中添加一个winsock控件&连接到实际的服务器,但如何通过服务器应用程序将客户端请求转发到实际服务器!请发布示例代码..将不胜感激。enter image description here

2 个答案:

答案 0 :(得分:0)

抱歉迟到的回复......

一个非常简单明了的解决方案可能如下:

一个简单的服务器项目:

'1 form with:
'  1 winsock control: name=Winsock1
'  1 textbox control: name=Text1 multiline=true scrollbars=3
Option Explicit

Private Sub Form_Load()
  With Winsock1
    .LocalPort = 12345
    .Listen
  End With
End Sub

Private Sub Form_Resize()
  Text1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub Winsock1_Close()
  With Winsock1
    Do
      If .State <> sckClosed Then
        .Close
      End If
    Loop Until .State = sckClosed
    .Listen
  End With 'Winsock1
  Text1.SelText = "Waiting for new client" & vbCrLf
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  With Winsock1
    If .State <> sckClosed Then
      .Close
    End If
    .Accept requestID
    Text1.SelText = "New connection from " & .RemoteHostIP & vbCrLf
  End With 'Winsock1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData
  Text1.SelText = "received: " & strData & vbCrLf
End Sub

一个简单的转发项目:

'1 form with:
'  2 winsock controls: name=Winsock1 and name=Winsock2
'  1 textbox control: name=Text1 multiline=true scrollbars=3
Option Explicit

Private Sub Form_Load()
  With Winsock1
    .LocalPort = 23456
    .Listen
  End With
  With Winsock2
    .Connect "laptop07", 12345
  End With 'Winsock2
End Sub

Private Sub Form_Resize()
  Text1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub Winsock1_Close()
  With Winsock1
    Do
      If .State <> sckClosed Then
        .Close
      End If
    Loop Until .State = sckClosed
    .Listen
  End With 'Winsock1
  Text1.SelText = "Waiting for new client" & vbCrLf
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  With Winsock1
    If .State <> sckClosed Then
      .Close
    End If
    .Accept requestID
    Text1.SelText = "New connection from " & .RemoteHostIP & vbCrLf
  End With 'Winsock1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData
  Winsock2.SendData strData
  Text1.SelText = "forwarded: " & strData & vbCrLf
End Sub

首先使用启动服务器,然后启动转发器

在我的示例中,服务器在我的笔记本电脑上运行,在我的网络中称为laptop07

在转发项目的代码中替换&#34; laptop07&#34;使用运行服务器的计算机的IP地址

服务器侦听端口12345,转发器侦听端口23456 ..确保它们在防火墙中被允许

测试它:

  • 启动服务器
  • 启动转发器
  • 让您的客户端连接到端口23456上的转发器
  • 从您的客户端发送数据
  • 看到它在转发器中转发
  • 看到它被服务器接收

请记住,这是一个非常简单的项目,没有任何错误捕获

它只是告诉你如何做到这一点

答案 1 :(得分:0)

服务器项目的新代码:

'1 form with:
'  1 winsock control: name=Winsock1
'  1 textbox control: name=Text1 multiline=true scrollbars=3
Option Explicit

Private Sub Form_Load()
  With Winsock1
    .LocalPort = 12345
    .Listen
  End With
End Sub

Private Sub Form_Resize()
  Text1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub Winsock1_Close()
  With Winsock1
    Do
      If .State <> sckClosed Then
        .Close
      End If
    Loop Until .State = sckClosed
    .Listen
  End With 'Winsock1
  Text1.SelText = "Waiting for new client" & vbCrLf
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  With Winsock1
    If .State <> sckClosed Then
      .Close
    End If
    .Accept requestID
    Text1.SelText = "New connection from " & .RemoteHostIP & vbCrLf
  End With 'Winsock1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData
  If CheckHeader(strData) = False Then
    Text1.SelText = "received: " & strData & vbCrLf
  End If
End Sub

Private Function CheckHeader(strData As String) As Boolean
  Dim blnHeader As Boolean
  Dim strHeaderStart As String, strHeaderEnd As String
  Dim strHeader As String
  blnHeader = False
  strHeaderStart = "@@@"
  strHeaderEnd = "###"
  If Left$(strData, Len(strHeaderStart)) = strHeaderStart Then
    If Right$(strData, Len(strHeaderEnd)) = strHeaderEnd Then
      strHeader = Mid$(strData, Len(strHeaderStart) + 1, Len(strData) - Len(strHeaderStart) - Len(strHeaderEnd))
      Text1.SelText = "Header: " & strHeader & vbCrLf
      blnHeader = True
    End If
  End If
  CheckHeader = blnHeader
End Function

转发器项目的新代码

'1 form with:
'  2 winsock controls: name=Winsock1 and name=Winsock2
'  1 textbox control: name=Text1 multiline=true scrollbars=3
Option Explicit

Private Sub Form_Load()
  With Winsock1
    .LocalPort = 23456
    .Listen
  End With
  With Winsock2
    .Connect "laptop07", 12345
  End With 'Winsock2
End Sub

Private Sub Form_Resize()
  Text1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub Winsock1_Close()
  With Winsock1
    Do
      If .State <> sckClosed Then
        .Close
      End If
    Loop Until .State = sckClosed
    .Listen
  End With 'Winsock1
  Text1.SelText = "Waiting for new client" & vbCrLf
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  With Winsock1
    If .State <> sckClosed Then
      .Close
    End If
    .Accept requestID
    Text1.SelText = "New connection from " & .RemoteHostIP & vbCrLf
    SendIP .RemoteHostIP
  End With 'Winsock1
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim strData As String
  Winsock1.GetData strData
  Winsock2.SendData strData
  Text1.SelText = "forwarded: " & strData & vbCrLf
End Sub

Private Sub SendIP(strIP As String)
  Dim strHeaderStart As String, strHeaderEnd As String
  strHeaderStart = "@@@"
  strHeaderEnd = "###"
  Winsock2.SendData strHeaderStart & "IP Client:" & strIP & strHeaderEnd
End Sub