单独与服务器或客户端都能正常通信,不知道这样的转发程序对不对?
通信意外断开后我想让它自动连接,次数为3次,请大虾也告诉下
服务器A-->转发站B-->客户端C,转发程序是放在转发站B上
转发程序代码:
Private Sub Command1_Click()
Command1.Enabled = False
Command2.Enabled = True
If ipclient.Text = "" Then
    ipclient.SetFocus
    MsgBox "服务器IP不能为空"
  
ElseIf Text2.Text = "" Then
    Text2.SetFocus
    MsgBox "服务器端口不能为空"
Else
    Winsockclient.Connect ipclient.Text, Text2.Text
End If
If Winsockclient.State = sckConnecting Then Label3.Caption = "与服务器正在连接"
If Winsockclient.State = sckClosed Then
Label3.Caption = "与服务器连接失败"
Command1.Enabled = True
Command2.Enabled = False
End If
End SubPrivate Sub Command2_Click()
Dim x As String
    If Winsockclient.State = sckClosed Then
    MsgBox "与服务器未连接"
    ElseIf Winsockclient.State = sckConnected Then
    Label3.Caption = "与服务器已断开"
    Winsockclient.Close
    Command1.Enabled = True
    Command2.Enabled = False
    End If
End SubPrivate Sub Form_Load()
'Winsockclient.RemotePort =1001
ipserver.Text = Winsockserver.LocalIP '本机IP地址
Winsockserver.LocalPort = 1002
Winsockserver.Listen
Text1.Text = Winsockserver.LocalPort
'中转站作为服务器时
Label3.Caption = "与服务器未连接"
Label6.Caption = "与客户端未连接"
Command1.Enabled = True
Command2.Enabled = False
End SubPrivate Sub textsend_Change()
Winsockserver.SendData textsend.Text
End SubPrivate Sub Winsockclient_Close()
Winsockclient.Close
MsgBox "与服务器断开连接"
Command1.Enabled = True
Command2.Enabled = False
Label3.Caption = "已断开"
End SubPrivate Sub Winsockclient_Connect()
Command1.Enabled = False
  Command2.Enabled = True
  Label3.Caption = "与服务器已连接"
End SubPrivate Sub Winsockclient_DataArrival(ByVal bytesTotal As Long)
Dim str As String
Dim at As String
Winsockclient.GetData str
Textget.Text = str
If Winsockserver.State = sckConnected Then
Winsockserver.SendData str
End If
End SubPrivate Sub Winsockserver_Close()
Winsockserver.Close
MsgBox "与客户端断开连接"
Winsockserver.LocalPort = Text1.Text
Winsockserver.Listen
End SubPrivate Sub Winsockserver_ConnectionRequest(ByVal requestID As Long)
If Winsockserver.State <> sckClosed Then Winsockserver.Close
Winsockserver.Accept requestID
Label6.Caption = "与客户端已连接"
End Sub

解决方案 »

  1.   

    没仔细看,但以下语句有误:
    If Winsockclient.State = sckConnecting Then Label3.Caption = "与服务器正在连接" 
    If Winsockclient.State = sckClosed Then 
    Label3.Caption = "与服务器连接失败" 
    由于Winsock是异步通信,所以在执行Connect方法后,是不能够立即将结果反映出来,所以需要在两个判断之间插入延时循环,而且状态还可能是sckError。比如:
    Lable3.Caption="与服务器正在连接"
    Do While WinsockClient.State <> sckConnecting
        Doevent
        If WinsockClient.State = sckClosed Or WinsockClient.State = sckError Then
            Label3.Caption="与服务器连接失败"
            Exit Do
        End If
    Loop
      

  2.   

    谢谢,怪不得我有时卡在"与服务器正在连接" 
    如果连接失败或因网络问题断开后,如何让它自动请求连接,次数为3次?
    TCP/IP转发程序程序是不是就是这样的?我网上找了好久都找不到实例和代码,请大哥再看下
      

  3.   

    1楼的代码有错误,运行到 Do While WinsockClient.State  <> sckConnecting  就运行不下去了
    我改成Do While WinsockClient.State = sckConnecting,程序在
     Doevent 
     If WinsockClient.State = sckClosed Or WinsockClient.State = sckError Then 
    来回循环
    请知道的高手指教
      

  4.   

    是的,我的代码写错了,条件应该是=。
    如果出现了死循环,说明socket一直处于连接状态,原因是服务程序没有运行或连接的参数(如IP、PORT)错误,因此,你可以加上一个超时判断。另外,判断是否连接成功的语句应该放在循环体外,比如:
    Dim lStartTime As Long
    lStartTime=GetTickCount()
    Do While WinsockClient.State  = sckConnecting
        Doevent
        If GetTickCount-lStartTime >2000 Then '2秒超时
            WinsockClient.Close
            Exit Do
        End If
    Loop 
    If WinsockClient.State = sckClosed Or WinsockClient.State = sckError Then
    End If