We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,408 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Cannot Write Over TCP Network Stream

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!

2
Contributors
3
Replies
1 Day
Discussion Span
4 Months Ago
Last Updated
4
Views
Question
Answered
ryklon
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

tinstaafl
Nearly a Posting Virtuoso
1,336 posts since Jun 2010
Reputation Points: 360
Solved Threads: 235
Skill Endorsements: 14

Ok. I'll get you updated.

ryklon
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

ryklon
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 4 Months Ago by tinstaafl

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0914 seconds using 2.68MB