当客户端关闭服务器崩溃时

时间:2015-08-23 14:22:45

标签: vb.net sockets tcp server client

当我从客户端连接到服务器时出现问题,它连接没有任何问题,但是当我关闭客户端窗口时,服务器显示以下错误消息:

The length can not be less than zero. Parameter name: length

然后当我尝试重新连接客户端时,它也会崩溃

这是服务器源代码:

    serverSocket.Start()
    AddInfo("Server Started", ConsoleColor.Cyan)

    AddInfo("---------------- ACCOUNTS ----------------", ConsoleColor.Blue)
    Dim AN As Integer = 0
    For Each f In Directory.GetFiles("saved\accounts\")
        Dim acc As String = Path.GetFileNameWithoutExtension(f).Split(CChar("%"))(0)
        Dim pass As String = Path.GetFileNameWithoutExtension(f).Split(CChar("%"))(1)
        AddInfo(acc & " - " & pass, ConsoleColor.Magenta)
        AN += 1
    Next
    AddInfo(AN & " Accounts exists .", ConsoleColor.DarkCyan)
    AddInfo("-----------------------------------------------", ConsoleColor.Blue)

    clientSocket = serverSocket.AcceptTcpClient()
    AddInfo("New Client Connected", ConsoleColor.Yellow)
    clients_number += 1
    AddInfo("Clients Num : " & clients_number, ConsoleColor.DarkYellow)
    requestCount = 0

    While (True)
        'RECEIVING
        requestCount = requestCount + 1
        serverStream = clientSocket.GetStream()
        Dim bytesFrom(10024) As Byte
        serverStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
        Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
        AddInfo("Data from client -  " + dataFromClient, ConsoleColor.DarkYellow)

        '### CHEKING ACCOUNT ###
        For Each f In Directory.GetFiles("saved\accounts\")
            If dataFromClient = Path.GetFileNameWithoutExtension(f) Then
                account_avaible = True
            End If
        Next
        If account_avaible = True Then
            Dim serverResponse As String = ""
            Dim l As New List(Of String)
            l.AddRange(File.ReadAllLines("saved\accounts\" & dataFromClient & ".inf"))
            For i = 0 To l.Count - 1
                serverResponse += l.Item(i).ToString.Split(CChar("="))(1) & "|"
            Next
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
            serverStream.Write(sendBytes, 0, sendBytes.Length)
            serverStream.Flush()
            AddInfo(serverResponse, ConsoleColor.Green)
        Else
            Dim serverResponse As String = "connection_false"
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
            serverStream.Write(sendBytes, 0, sendBytes.Length)
            serverStream.Flush()
            AddInfo(serverResponse, ConsoleColor.Green)
            account_avaible = False
        End If
    End While

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。谢谢@WDS。  我删除了循环,然后我返回clientSocket = serverSocket.AcceptTcpClient()

这是来源:

`serverSocket.Start():AddInfo(" Server Started",ConsoleColor.Cyan)

    AddInfo("---------------- ACCOUNTS ----------------", ConsoleColor.Blue)
    Dim AN As Integer = 0
    For Each f In Directory.GetFiles("saved\accounts\")
        Dim acc As String = Path.GetFileNameWithoutExtension(f).Split(CChar("%"))(0)
        Dim pass As String = Path.GetFileNameWithoutExtension(f).Split(CChar("%"))(1)
        AddInfo(acc & " - " & pass, ConsoleColor.Magenta)
        AN += 1
    Next
    AddInfo(AN & " Accounts exists .", ConsoleColor.DarkCyan)
    AddInfo("-----------------------------------------------", ConsoleColor.Blue)

突突:

    account_avaible = False
    clientSocket = serverSocket.AcceptTcpClient()
    AddInfo("New Client Connected", ConsoleColor.Yellow)
    clients_number += 1
    AddInfo("Clients Num : " & clients_number, ConsoleColor.DarkYellow)
    requestCount = 0

        'RECEIVING
        requestCount = requestCount + 1
        serverStream = clientSocket.GetStream()
        Dim bytesFrom(10024) As Byte
        serverStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize)
        Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
        AddInfo("Data from client -  " + dataFromClient, ConsoleColor.DarkYellow)

        '### CHEKING ACCOUNT ###
        For Each f In Directory.GetFiles("saved\accounts\")
            If dataFromClient = Path.GetFileNameWithoutExtension(f) Then
                account_avaible = True
            End If
        Next
        If account_avaible = True Then
            Dim serverResponse As String = ""
            Dim l As New List(Of String)
            l.AddRange(File.ReadAllLines("saved\accounts\" & dataFromClient & ".inf"))
            For i = 0 To l.Count - 1
                serverResponse += l.Item(i).ToString.Split(CChar("="))(1) & "|"
            Next
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
            serverStream.Write(sendBytes, 0, sendBytes.Length)
            serverStream.Flush()
            AddInfo(serverResponse, ConsoleColor.Green)
        Else
            Dim serverResponse As String = "connection_false"
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
            serverStream.Write(sendBytes, 0, sendBytes.Length)
            serverStream.Flush()
            AddInfo(serverResponse, ConsoleColor.Green)
            account_avaible = False
    End If

    GoTo Dodo

    'clientSocket.Close()
    'serverSocket.Stop()`

但现在我有新问题= /当我输入错误的帐户msg显示("不正确的帐户")然后当我尝试输入其他帐户时客户端bugg(冻结)