1,105,371 Community Members

Cannot Write Over TCP Network Stream

Member Avatar
ryklon
Newbie Poster
24 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm encountering a problem when I try to write over a network stream.

When I press this button here, I can successfully write to the tcp stream and the other part of the program successfully receives the data.

Dim ClientSocket As TcpClient

Private Sub btnConnect_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnConnect.MouseClick

      ClientSocket = New TcpClient

      ClientSocket.Connect(frmNetworkSettings.txtServerIPAddress.Text, CInt(frmNetworkSettings.txtFileSharingPort.Text))

      Dim NetworkStream As NetworkStream = ClientSocket.GetStream()

      If NetworkStream.CanRead And NetworkStream.CanWrite Then

             Dim SentData As Byte()

             SentData = Encoding.ASCII.GetBytes(Username & Chr(32) & "is connected." & Environment.NewLine)

             NetworkStream.Write(SentData, 0, SentData.Length())

             NetworkStream.Flush()

      End If

End Sub

But when I press this button below, after pressing the button above, I cannot write on the tcp network stream and the other program on the network receives no data.

Private Sub btnDisconnect_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles btnDisconnect.MouseClick

     Dim NetworkStream As NetworkStream = ClientSocket.GetStream()

     If NetworkStream.CanRead And NetworkStream.CanWrite Then

            Dim SentData As Byte()

            SentData = Encoding.ASCII.GetBytes(Username & Chr(32) & "is disconnected." & Environment.NewLine)

            NetworkStream.Write(SentData, 0, SentData.Length())

            NetworkStream.Flush()

     End If

End Sub

Thanks in advance!

Member Avatar
tinstaafl
Postaholic
2,009 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 400 [?]
Skill Endorsements: 35 [?]
 
0
 

try declaring NetworkStream with class level scope the same as ClientSocket and use the same stream in both subs.

Member Avatar
ryklon
Newbie Poster
24 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok. I'll get you updated.

Member Avatar
ryklon
Newbie Poster
24 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I figured out the problem. Its because I'm reading the network stream only during a connection is made. I should modify my code to read the network stream after a connection is made. Anyway, thanks for your reply.

Question Answered as of 1 Year Ago by tinstaafl
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article