Client-Server
-
hey, guys i was wondering if anyone can help me debug this code as i have been trying for days and still have not find out the mistake i make. The portion which i have posted is one of the sub in my server application. I edited the code i found so that the server is able to reply to its client and after editing server do make the reply but with some bug. For the 1st request from the client, the reply is perfectly fine but subsequent request, the browser omit 2 front letters of the sentence and i don't understand why.Struck with this error with quite some time already. Help will be greatly appreciated. Private Sub doChat() Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte Dim dataFromClient, DataToClient As String Dim sendBytes As [Byte]() Dim serverResponse As String Dim rCount As String requestCount = 0 For infiniteCounter = 1 To 2 infiniteCounter = 1 Try requestCount = requestCount + 1 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 = "ok" sendBytes = System.Text.Encoding.ASCII.GetBytes(serverResponse) networkStream.Write(bytesFrom, 0, serverResponse.Length) msg("From Server - " + serverResponse) DataToClient = networkStream.Read(sendBytes, 0, sendBytes.Length) broadcast(dataFromClient, clNo, True) broadcast(serverResponse, "Server", True) Catch ex As Exception MsgBox(ex.ToString) End Try Next End Sub
-
hey, guys i was wondering if anyone can help me debug this code as i have been trying for days and still have not find out the mistake i make. The portion which i have posted is one of the sub in my server application. I edited the code i found so that the server is able to reply to its client and after editing server do make the reply but with some bug. For the 1st request from the client, the reply is perfectly fine but subsequent request, the browser omit 2 front letters of the sentence and i don't understand why.Struck with this error with quite some time already. Help will be greatly appreciated. Private Sub doChat() Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte Dim dataFromClient, DataToClient As String Dim sendBytes As [Byte]() Dim serverResponse As String Dim rCount As String requestCount = 0 For infiniteCounter = 1 To 2 infiniteCounter = 1 Try requestCount = requestCount + 1 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 = "ok" sendBytes = System.Text.Encoding.ASCII.GetBytes(serverResponse) networkStream.Write(bytesFrom, 0, serverResponse.Length) msg("From Server - " + serverResponse) DataToClient = networkStream.Read(sendBytes, 0, sendBytes.Length) broadcast(dataFromClient, clNo, True) broadcast(serverResponse, "Server", True) Catch ex As Exception MsgBox(ex.ToString) End Try Next End Sub
Hi, your code is hard to read without proper formatting; please use the PRE tags for code blocks that span several lines, they provide a non-proportional font, and pteserve formatting (indentation). Anyhow,
networkStream.Write(bytesFrom, 0, serverResponse.Length)
looks suspicious to me: why would serverResponse determine how many bytes from "bytesFrom" should be written? :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
Hi, your code is hard to read without proper formatting; please use the PRE tags for code blocks that span several lines, they provide a non-proportional font, and pteserve formatting (indentation). Anyhow,
networkStream.Write(bytesFrom, 0, serverResponse.Length)
looks suspicious to me: why would serverResponse determine how many bytes from "bytesFrom" should be written? :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
Is tis ok? Private Sub doChat() Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte Dim dataFromClient, DataToClient As String Dim sendBytes As [Byte]() Dim serverResponse As String Dim rCount As String requestCount = 0 For infiniteCounter = 1 To 2 infiniteCounter = 1 Try requestCount = requestCount + 1
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 = "ok"
sendBytes = System.Text.Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(bytesFrom, 0, serverResponse.Length)
msg("From Server - " + serverResponse)
DataToClient = networkStream.Read(sendBytes, 0, sendBytes.Length)
broadcast(dataFromClient, clNo, True)
broadcast(serverResponse, "Server", True)Catch ex As Exception MsgBox(ex.ToString) End Try Next End Sub
-
Is tis ok? Private Sub doChat() Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte Dim dataFromClient, DataToClient As String Dim sendBytes As [Byte]() Dim serverResponse As String Dim rCount As String requestCount = 0 For infiniteCounter = 1 To 2 infiniteCounter = 1 Try requestCount = requestCount + 1
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 = "ok"
sendBytes = System.Text.Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(bytesFrom, 0, serverResponse.Length)
msg("From Server - " + serverResponse)
DataToClient = networkStream.Read(sendBytes, 0, sendBytes.Length)
broadcast(dataFromClient, clNo, True)
broadcast(serverResponse, "Server", True)Catch ex As Exception MsgBox(ex.ToString) End Try Next End Sub
:confused::confused::confused: That is more code than before, some of it in PRE tags?? And I still don't like the line
networkStream.Write(bytesFrom, 0, serverResponse.Length)
As serverResponse contains "ok" its length will always be 2! is that what you intend? :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
:confused::confused::confused: That is more code than before, some of it in PRE tags?? And I still don't like the line
networkStream.Write(bytesFrom, 0, serverResponse.Length)
As serverResponse contains "ok" its length will always be 2! is that what you intend? :)Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
It is the same amount of code i posted earlier. Those in the PRE tags are those code whereby i think my mistake is at. As for the serverRespone.Length, 2 is not the intended length but i already change it but it seen like there is not much different. The reply i got are all rubbish.