Hello, i was learning about sending messages to the server -> client -> server, however after every client that gets connected it adds about 25% CPU. So after 4 clients the process is using 100% of CPU usage.
I didn't figure out what the problem could be?
VB.NET:
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
I didn't figure out what the problem could be?