0

我正忙于服务器->客户端->服务器软件,客户端连接到服务器并请求一个字符串,服务器将其响应。

但是,当建立多个连接时,比如 4,我的电脑的 CPU 被提升到 100%

这是我的代码。

Imports System.Net ' for IPAddress
Imports System.Net.Sockets 'for TcpListener
Imports System.Text

Module Module1
Dim servePort As Integer = 28960
Dim requestCount As Integer
Dim bytesFrom(10024) As Byte
Dim dataFromClient As String
Dim sendBytes As [Byte]()
Dim serverResponse As String
Dim rCount As String
Sub Main()
    Dim serverSocket As New TcpListener(servePort)
    Dim clientSocket As TcpClient
    Dim counter As Integer
    Console.Title = ("Server")
    msg("Starting Server...")
    serverSocket.Start()
    msg("Server started on port " & servePort)
    msg("")
    counter = 0

    While (True)
        counter += 1
        clientSocket = serverSocket.AcceptTcpClient()
        If clientSocket.Client.LocalEndPoint.ToString = "127.0.0.1:28961" Then
            clientSocket.Close()
            Exit While
        End If

        msg("Client " + clientSocket.Client.LocalEndPoint.ToString + " connected.")
        Dim client As New handleClinet
        Dim networkStream As NetworkStream = _
                        clientSocket.GetStream()
        client.startClient(clientSocket, Convert.ToString(counter))

    End While

    clientSocket.Close()
    serverSocket.Stop()

    Console.ReadLine()
End Sub

Sub msg(ByVal mesg As String)
    mesg.Trim()
    Console.WriteLine(mesg)
End Sub

Public Class handleClinet
    Dim clientSocket As TcpClient
    Dim clNo As String
    Public Sub startClient(ByVal inClientSocket As TcpClient, _
    ByVal clineNo As String)
        Me.clientSocket = inClientSocket
        Me.clNo = clineNo
        Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
        ctThread.Start()
    End Sub
    Private Sub doChat()
        requestCount = 0

        While (True)
            Try
                requestCount = requestCount + 1
                Dim ip As String = clientSocket.Client.LocalEndPoint.ToString
                Dim networkStream As NetworkStream = _
                        clientSocket.GetStream()
                networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
                dataFromClient = _
                dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                msg("<< " & dataFromClient)
                rCount = Convert.ToString(requestCount)

                If dataFromClient.ToLower = "hydra_ver" Then
                    serverResponse = "0.94"
                Else
                   serverResponse = "Unknown Command"
                End If


                Else
                    serverResponse = "Unknown Command"
                End If
                sendBytes = Encoding.ASCII.GetBytes(serverResponse)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                networkStream.Flush()
                msg(">> " & serverResponse)
            Catch ex As Exception

            End Try

        End While

    End Sub
End Class
End Module
4

1 回答 1

0

哇。

你在一个永远循环中打开和关闭连接?

创建一个套接字侦听器,然后处理流事件。这是 c#,但我敢肯定你现在已经习惯了回溯到 VBNet。

public void portSocketListener() 
{
    Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
    IPEndPoint ep = new IPEndPoint(hostIP, 28961);
    listenSocket.Bind(ep);
    listenSocket.Listen(1);
}
于 2013-07-18T20:59:26.117 回答