Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have this problem when I am writing and reading network streams. If I send another packet, then it will also send the last packet again, so I end up sending more and more information. Somehow I mustn't be flushing the buffer out correctly or something. This is my output if I click a button 3 times - you can see by the third time it has sent the packet 3 times, and hence the server replies 3 times.

 

Recieved packet: hs#USHOEOZSSUNW

Connection verified.

Recieved packet: hs#YTZJCYVGGWRX

Recieved packet: hs#YTZJCYVGGWRX

Connection verified.

Connection verified.

Recieved packet: hs#THPQVRMIHKML

Recieved packet: hs#THPQVRMIHKML

Recieved packet: hs#THPQVRMIHKML

Connection verified.

Connection verified.

Connection verified.

 

To send a packet I am using:

 

       Dim ns As NetworkStream = Client.GetStream
       If ns.CanWrite Then
           Dim bytes() As Byte = Nothing
           bytes = System.Text.Encoding.UTF8.GetBytes(Msg)
           ns.Write(bytes, 0, bytes.Length)
       End If

 

To receive a packet I am using:

 

                   Dim ns As NetworkStream = cli.GetStream
                   If ns.CanRead And ns.DataAvailable Then
                       Dim bytes(cli.ReceiveBufferSize) As Byte
                       ns.Read(bytes, 0, cli.ReceiveBufferSize)
                       RaiseEvent NewPacket(cli, System.Text.Encoding.UTF8.GetString(bytes))
                   Else
                       cli.Close()
                       Connections.Remove(cli)
                       RaiseEvent ClientMessage("Client connected from " & cli.Client.RemoteEndPoint.ToString.Split(":")(0) & " has disconnected.")
                   End If

 

I have tried closing the stream after I have finished writing, but that seems to destroy the socket. I have tried Flush() too, but not too sure how to use that one.

 

Cheerio,

 

Pinky

Posted

Assuming you're using tcp......

 

1) Try disabling the nagle algorithm using setsocketoptions method. THe nagle

algorithm basically prevents data from being sent immediately (think chatty clients).

 

2) When you finished writing your custom packet, flush the data by calling the Flush method.

 

3) Closing a socket will drop the connection. I believe with networkstreams though, there's a boolean indicating if it owns the underlying socket or not. If it doesn't, it won't close the underlying socket.

Posted

Firstly, thanks for your quick replies HJB417.

 

Yep, I'm using TCP. I did a bit more research on the 'Flush()' method, and I found that it is 'Reserved for later use.'

 

I actually fixed my problem though. I was brute force testing a multiple connection server with a little app I made to generate lots of clients. Each time I clicked the button it generated a client, AND added a handler for each client! So when I had added 3 clients, I was getting 3 lots of messages ;-)

 

Oh well, the best problems are the ones with the simplest solutions I guess.

 

Thanks again,

 

Pinky

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...