none
vb2005 socket问题 RRS feed

  • 问题

  • 我在vb.net2005里面tcp连接遇到这样一个问题。不知道为什么总是测试几天后再次测试就说目标机器积极拒绝,无法连接,这样提示。但是以前好好的东西用了几天就不行了。重装系统可以打开。但又过大概十几天后还是不行。这是为什么?

    Try
                Call getMySettings()
                'clientSocket = New Sockets.TcpClient(mySettings.myServerAddr, 12345)
                clientSocket = New Sockets.TcpClient("127.0.0.1", 12345)
                stream = clientSocket.GetStream  '得到流
                inf.Serialize(stream, myself) '发送验证信息
                stream.Flush()
    
                If getReplay() = "Accepted" Then
    
                    'getReplay() '获得返回信息
                    sendCmd("login")
                    Call ifLoginFail(getReplay()) '获得返回信息,getReplay在common里面定义
                End If
            Catch ex As Exception
                Call disposeHandle()
                MsgBox("建立连接出错,请重新设置服务器ip地址!")
                MsgBox(ex.Message, , "")
                'setIPDialog.ShowDialog()
            End Try
     Dim ipAddr As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList(0)  '获取服务器ip
            Try
                serverSocket = New TcpListener(ipAddr, 12345)        '生成侦听服务端
                serverSocket.Start()
            Catch ex As Exception
                MsgBox(ex.Message)
                End
            End Try
            Dim thread As Thread = New Thread(AddressOf Run)
            thread.Start()
    Public Sub Run()
            ' listening()
            While (True)
                Try
                    Dim acceptSocket As TcpClient = serverSocket.AcceptTcpClient() '唤醒挂起Client,返回服务套接字
                    numOfClient += 1
                    'Console.Write("第 " + CStr(numOfClient) + " 号用户来了!")
                    stream = acceptSocket.GetStream()
                    Dim buffer(1024) As Byte '缓冲区
                    stream.Read(buffer, 0, 1024)
                    mContent = System.Text.Encoding.GetEncoding("UTF-8").GetString(buffer)
                    read = New XmlTextReader(New IO.StringReader(mContent))
                    Dim user As UserInf = New UserInf
                    While (read.Read())
                        If (read.NodeType = XmlNodeType.Element And read.Name = "userAccount") Then
                            user.userAccount = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userPsw") Then
                            user.userPsw = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userName") Then '获取用户昵称
                            user.userName = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userHead") Then '获取头像地址
                            user.userHead = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userWord") Then '获取个性签名
                            user.userWord = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userSex") Then '获取性别
                            user.userSex = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userAge") Then '获取年龄
                            user.userAge = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userEmail") Then '获取Email 
                            user.userEmail = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userTel") Then '获取电话
                            user.userTel = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userState") Then '获取状态
                            user.userState = read.ReadElementContentAsString
                        End If
                    End While
                    'Console.WriteLine(" 欢迎" + user.userAccount + " 进入本系统!")
    
                    Dim newThread As ThreadClass
                    Dim certainThread As Thread
                    newThread = New ThreadClass(user, acceptSocket)
                    certainThread = New Thread(AddressOf newThread.newComerHandle)
                    Try
                        certainThread.Start()
                    Catch ex As Exception
                        If Not certainThread Is Nothing Then
                            certainThread.Abort()
                            certainThread = Nothing
                        End If
                    End Try
                Catch ex As Exception
                    If Not stream Is Nothing Then
                        stream.Close()
                    End If
                    If Not read Is Nothing Then
                        read.Close()
                    End If
                    'Console.WriteLine("新用户登录失败,服务器断开连接!")
                End Try
            End While
                    'While (True)
                    ' Do what you need
                    ' End While
        End Sub
    这是客户端和服务器端的代码。大家看一下。。。这到底为什么。。。
    muradil
    2009年5月30日 15:59

答案

  • 你好,你监听的只是127.0.0.1这个ip当客户端用你的公网ip访问的时候是无法监听到了,当然如果你是在你在自己本机上同时运行客户端和服务器端时可以的
    你可以查看下我这篇文章
    http://hi.baidu.com/1987raymond/blog/item/0aa6e83f96e215ca7d1e71c9.html

     listener = new TcpListener(IPAddress.Any, port)
     

    同时请注意下你电脑上的防火墙是否禁用了TCP 12345端口,如果是禁用了则有肯能发生这种目标机器积极拒绝的错误

    一般防火墙或者安全套装都提供的是主动的模式,即防火墙自动将这些端口禁用了,你可以选择为交互模式这样你自己可以控制端口的开启和禁用,一般式定制规则、策略这些



    努力着就不会放弃!.net技术交流群 33083877http://hi.baidu.com/1987raymond
    2009年5月31日 1:26
    版主