none
求助一个socket的问题 RRS feed

  • 问题

  •   '客户端Socket
      Dim cliSocket As Socket
      '连接状态
      Dim IsConnected As Boolean = False
      '接收信息线程
      Dim myThread As Thread
      Dim ipHostInfo As IPHostEntry = System.Net.Dns.GetHostEntry("www.baibaiba.net")
      Dim remoteEP As New IPEndPoint(Net.IPAddress.Parse(ipHostInfo.AddressList(0).ToString), 9888)
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ConnectServer()
      End Sub
    
      Sub ConnectServer()
        cliSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Try
          cliSocket.Connect(remoteEP)
          MsgBox(cliSocket.Connected)
          IsConnected = True
          ListBox1.Items.Add("连接服务端成功!")
          Timer1.Stop()
          Timer1.Enabled = False
        Catch ex As Exception
          ListBox1.Items.Add(ex.Message)
          Timer1.Enabled = True
          Timer1.Start()
        End Try
      End Sub
    
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ConnectServer()
        'Timer1.Enabled = False
      End Sub
    

     

    问题描述:

    代码用意在于客户端启动后,如果不能够与SERVER建立连接,就不停地尝试与SERVER进行连接

    上述代码中,如果域名填写或者解析正确,即使服务器端的监听程序没有运行,客户端程序也能正常运行,但是,一旦域名错误或者不能正确解析IP地址,程序象假死一样,连窗体都不能够显示。我该怎么解决呢?

     


    学无止境
    2011年5月26日 10:38

答案

全部回复