S
Sivakumar282000
Guest
Hi All,
Problem statement - Remote monitoring on virtual machine(virtual box) not working and on physical machine taking time to get the output or sometimes not responding.
Setup
Monitor process,service,HD
connection on port 8888
It is one to one connection between server and client and there is no other device in the network.
Code
Client
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Try
If (btnConnect.Text = "Connect") Then
btnConnect.Text = "Disconnect"
clientSocket = New System.Net.Sockets.TcpClient()
Label1.Text = "Client Socket Program - Connecting to Server ..."
clientSocket.Connect(txtIP.Text, txtPort.Text)
Label1.Text = "Client Socket Program - Server Connected ..."
Else
btnConnect.Text = "Connect"
Call SendData("Disconnect$")
'clientSocket.GetStream.Close()
clientSocket.Client.Disconnect(True)
clientSocket.Close()
End If
Catch ex As Exception
msg("Connect: " & Err.Description)
End Try
End Sub
Private Sub SendData(ByVal strData As String)
Try
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(strData)
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
'Dim inStream(10024) As Byte
Dim inStream(65536) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String =
System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " + returndata)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
Server
Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String)
Try
Me.clientSocket = inClientSocket
Me.clNo = clineNo
ctThread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
Private Sub doChat()
Try
Dim requestCount As Integer
Dim bytesFrom(65536) As Byte
'Dim dataFromClient As String
Dim sendBytes As [Byte]()
Dim serverResponse As String
Dim rCount As String
requestCount = 0
Dim bolLoop As Boolean = True
While (bolLoop)
Try
requestCount = requestCount + 1
Dim dataFromClient As String
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("From client-" + clNo + dataFromClient)
rCount = Convert.ToString(requestCount)
serverResponse = "Server to clinet(" + clNo + ") " + rCount
sendBytes = Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
If (dataFromClient.Trim = "Disconnect") Then
bolLoop = False
clientSocket.GetStream.Close()
'ctThread.Abort()
'Exit While
End If
Array.Clear(bytesFrom, 0, bytesFrom.Length)
Catch ex As Exception
'MsgBox(ex.ToString)
bolLoop = False
'clientSocket.GetStream.Close()
End Try
End While
'Dim intID As Integer = ctThread.ManagedThreadId
'ctThread.Abort(intID)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
End Class
Please help as i feel connection is not reliable.
Regards,
Siva Kumar
Siva
Continue reading...
Problem statement - Remote monitoring on virtual machine(virtual box) not working and on physical machine taking time to get the output or sometimes not responding.
Setup
Monitor process,service,HD
connection on port 8888
It is one to one connection between server and client and there is no other device in the network.
Code
Client
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
Try
If (btnConnect.Text = "Connect") Then
btnConnect.Text = "Disconnect"
clientSocket = New System.Net.Sockets.TcpClient()
Label1.Text = "Client Socket Program - Connecting to Server ..."
clientSocket.Connect(txtIP.Text, txtPort.Text)
Label1.Text = "Client Socket Program - Server Connected ..."
Else
btnConnect.Text = "Connect"
Call SendData("Disconnect$")
'clientSocket.GetStream.Close()
clientSocket.Client.Disconnect(True)
clientSocket.Close()
End If
Catch ex As Exception
msg("Connect: " & Err.Description)
End Try
End Sub
Private Sub SendData(ByVal strData As String)
Try
serverStream = clientSocket.GetStream()
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(strData)
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
'Dim inStream(10024) As Byte
Dim inStream(65536) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String =
System.Text.Encoding.ASCII.GetString(inStream)
msg("Data from Server : " + returndata)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
Server
Public Sub startClient(ByVal inClientSocket As TcpClient, ByVal clineNo As String)
Try
Me.clientSocket = inClientSocket
Me.clNo = clineNo
ctThread = New Threading.Thread(AddressOf doChat)
ctThread.Start()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
Private Sub doChat()
Try
Dim requestCount As Integer
Dim bytesFrom(65536) As Byte
'Dim dataFromClient As String
Dim sendBytes As [Byte]()
Dim serverResponse As String
Dim rCount As String
requestCount = 0
Dim bolLoop As Boolean = True
While (bolLoop)
Try
requestCount = requestCount + 1
Dim dataFromClient As String
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("From client-" + clNo + dataFromClient)
rCount = Convert.ToString(requestCount)
serverResponse = "Server to clinet(" + clNo + ") " + rCount
sendBytes = Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
msg(serverResponse)
If (dataFromClient.Trim = "Disconnect") Then
bolLoop = False
clientSocket.GetStream.Close()
'ctThread.Abort()
'Exit While
End If
Array.Clear(bytesFrom, 0, bytesFrom.Length)
Catch ex As Exception
'MsgBox(ex.ToString)
bolLoop = False
'clientSocket.GetStream.Close()
End Try
End While
'Dim intID As Integer = ctThread.ManagedThreadId
'ctThread.Abort(intID)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
End Class
Please help as i feel connection is not reliable.
Regards,
Siva Kumar
Siva
Continue reading...