本人初次做局域网多客户端的一个网络程序,需要在客户端与服务器端相连时能够在服务器端实时显示IP地址和机器名,IP地址能够正常显示,但是机器名不行,请教各位高手能否耐心帮忙看下代码,红色是有问题的部分,应该如何修改可以获得客户端的机器名,谢谢了!Private Sub tcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim n As Integer
Dim k As Integer
n = FirstNonConnected()  '从已释放的连接中选取一个编号
If Index = 0 Then
      If n = 0 Then   '返回值为0时表明为新的连接
            intMax = intMax + 1  
            Load tcpServer(intMax)
            '检查控件的 State 属性是否为关闭的。
              '如果不是,
              '在接受新的连接之前先关闭此连接。
             If tcpServer(intMax).State <> sckClosed Then
            tcpServer(intMax).Close
             '接受具有 requestID 参数的连接。
              End If
            tcpServer(intMax).LocalPort = 0
            tcpServer(intMax).Accept requestID
             If txtInfo.Text = "" Then
               txtInfo.Text = "客户机" & intMax & "连接成功!"
             Else
               txtInfo.Text = txtInfo.Text & vbCrLf & "客户机" & intMax & "连接成功!"
             End If
             k = InClientInfo(tcpServer(intMax).RemoteHostIP)  '查找当前连接是否在已连接列表中
             If k <> 0 Then   ’不是0,说明在列表中
               SetMSH k, "", "", True  '设置对应行为连接状态
             Else
                ClientNum = ClientNum + 1
                ClientIP(ClientNum) = tcpServer(intMax).RemoteHostIP
                ClientName(ClientNum) = tcpServer(intMax).RemoteHostName  '就是这儿有问题,不知怎么改
                SetMSH ClientNum, ClientName(ClientNum), ClientIP(ClientNum), True
             End If
             txtInfo.Text = txtInfo.Text & Now() & "    " & tcpServer(intMax).RemoteHostIP & "登陆成功!" & vbCrLf
         Else  ’找到一个已释放的编号
            If tcpServer(n).State <> sckClosed Then
            tcpServer(n).Close
            '接受具有 requestID 参数的连接。
              End If
            tcpServer(n).LocalPort = 0
            tcpServer(n).Accept requestID
            If txtInfo.Text = "" Then
            txtInfo.Text = "客户机" & n & "连接成功!"
            Else
            txtInfo.Text = txtInfo.Text & vbCrLf & "客户机" & n & "连接成功!"
            End If
            k = InClientInfo(tcpServer(intMax).RemoteHostIP)
            If k <> 0 Then
            SetMSH k, "", "", True
            End If
            txtInfo.Text = txtInfo.Text & Now() & "    " & tcpServer(intMax).RemoteHostIP & "登陆成功!" & vbCrLf
              
       End If
       MSH1.Refresh
       lblConnect.Caption = "当前在线客户机总数:" & getConnectNum()
End If
End Sub

解决方案 »

  1.   

    机器名最好由客户端建立连接后,发送它的机器名给你,因为毕竟客户端可以取得机器名GetComputerName,然后发送到服务端.
      

  2.   

    我是客户端这样写的:
    Private Sub tcpClient_Connect()
     If txtOutput.Text = "" Then
       txtOutput.Text = "连接成功!"
       Else
       txtOutput.Text = txtOutput.Text & vbCrLf & "连接成功!"
       End If
       txtHost.Enabled = False
       cmdConnect.Enabled = False   ’禁止再连接
       cmdDisConnect.Enabled = True  '允许断开
       tcpClient.SendData "Name:" & tcpClient.LocalHostName
    End Sub在服务器这边过程tcpServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)处理的结果如何才能传回到前边的过程tcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)中?
      

  3.   

    机器名最好由客户端建立连接后,发送它的机器名给你,因为毕竟客户端可以取得机器名GetComputerName,然后发送到服务端