|
 |
pro_vb_dotnet thread: vb.net sockets sending blank stream of data
Message #1 by "Adiel Gonzalez" <adiel_g@h...> on Fri, 22 Feb 2002 13:23:39
|
|
Hello everyone, I am trying to use the Sockets namespace in vb.net to
create a TCP client. My problem is that the socket keeps sending
an "empty" stream at the end of each "send". This only happens when
running the exe, during debug it does not send the empty stream. I have
a button setup to start the send. After the send, the socket will receive
a confirmation that "msg was received" during the receive:
Imports System.Net.Sockets
Imports System.Text
...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim tcpClient As New TcpClient()
Try
tcpClient.Connect("myServer", 6789)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
'HERE IS THE WRITE
' Does a simple write.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes
(txtInput.Text)
networkStream.Write(sendBytes, 0, sendBytes.Length)
'HERE IS THE READ
' Reads the NetworkStream into a byte buffer.
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt
(tcpClient.ReceiveBufferSize))
' Returns the data received from the host.
Dim returndata As String = Encoding.ASCII.GetString(bytes)
txtOutput.Text = returndata
' Close the network stream
'networkStream.Close()
' Close the connection
tcpClient.Close()
Else
If Not networkStream.CanRead Then
Console.WriteLine("You can not write data to this
stream")
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
Console.WriteLine("You can not read data from this
stream")
tcpClient.Close()
End If
End If
End If
Catch eX As Exception
Console.WriteLine(eX.ToString())
End Try
End Sub
Here is what I send:
"ABC" (Click on button first time with txtInput.Text equal to "ABC")
"DEF" (Click on button second time with txtInput.Text equal to "DEF")
Here is what the tcp server received when client is in debug mode:
"ABC"
"DEF"
Here is what the tcp server received when client is run from the exe:
"ABC"
"DEF"
Note: The spaces are random sometimes the client sends an extra space,
sometimes it doesnt. I tried using the .flush method with no help. I
believe this is a bug in the System.Net.Sockets. If anyone can prove me
wrong, that would be great!
Thanks before hand,
Adiel
|
|
 |