bsaxberg
New member
From reading through this thread I was able to get a client server app working successfully. Thank you!
My question is this, if I pass a string of text from the client to the server what is filled in the stream after the text?
Here is the main code in the client. You can see it is getting the byte length to send from the Encoding.ASCII.GetBytes call.
On the Server end I am catching it and it is saying that my Buffer Length is 8193 when what was passed was only an 8 character long string. What is the extra data in the stream and how can I trim it off so I only see the string that was sent?
Here is the server code.
I openly admit I am new to Sockets.
Thank you,
Brian
My question is this, if I pass a string of text from the client to the server what is filled in the stream after the text?
Here is the main code in the client. You can see it is getting the byte length to send from the Encoding.ASCII.GetBytes call.
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If tbSend.Text.Length > 0 Then
Dim tcp As New TcpClient
tcp.Connect("127.0.0.1", 8765)
Dim networkStream As NetworkStream = tcp.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(tbSend.Text)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Dim bytes(tcp.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcp.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
tbReceive.Text = returndata
Else
If Not networkStream.CanRead Then
tbStatus.Text = "cannot not read data to this stream"
tcp.Close()
Else
If Not networkStream.CanWrite Then
tbStatus.Text = "cannot write data from this stream"
tcp.Close()
End If
End If
End If
Else
MsgBox("Enter Some data to send", MsgBoxStyle.OkOnly)
End If
End Sub
On the Server end I am catching it and it is saying that my Buffer Length is 8193 when what was passed was only an 8 character long string. What is the extra data in the stream and how can I trim it off so I only see the string that was sent?
Here is the server code.
VB.NET:
Private Sub accepting(ByVal ar As IAsyncResult)
'receives filename, filelength, filebytes
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
Dim netstream As NetworkStream = clientSocket.GetStream()
Dim bytesFrom(clientSocket.ReceiveBufferSize) As Byte
Dim NewUser As String = ""
netstream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
Dim serverResponse As String = ""
MsgBox(dataFromClient.Length.ToString)
If dataFromClient.ToString = "OpenGame" Then
MsgBox("True")
End If
If dataFromClient.ToString.Substring(0, 8) = "OpenGame" Then
serverResponse = GameInPlay.GameID.ToString
ElseIf dataFromClient.StartsWith("DisplayName=") Then
NewUser = dataFromClient.Substring(0, 11)
For Each con In ConList
If NewUser = con.DisplayName Then
Dim n As Integer = dgvContestants.Rows.Add()
dgvConnections.Columns.Item(0).Name = "Contestant"
dgvContestants.Rows(n).Cells(0).Value = con.DisplayName
serverResponse = "Contestant Added"
Exit For
End If
Next
Else
serverResponse = "Unknown Parameter"
End If
Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(serverResponse)
netstream.Write(sendBytes, 0, sendBytes.Length)
netstream.Flush()
clientSocket.Close()
End Sub
I openly admit I am new to Sockets.
Thank you,
Brian