XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

NetworkStream help?

Last post 9/25/2009 5:58 PM by cpyburn. 2 replies.
  • 9/25/2009 3:12 PM

    NetworkStream help?

    How do you clear out the the networkstream if that is really what I even need to do?  i am running into a problem while running a for loop that the data is still in the stream from the previous loop.  let me be more clear.

    private TcpClient client;

    private NetworkStream clientStream;

    clientStream = client.GetStream();

    foreach (DataRow item in ds.Tables["ServerAddress"].Rows)
    {
    tempServers = item["name"];
    memoryStream.Write(
    Encoding.ASCII.GetBytes(tempServers), 0, Encoding.ASCII.GetBytes(tempServers).Length);
    // Finally send the buffer
    clientStream.BeginWrite(memoryStream.ToArray(), 0, memoryStream.ToArray().Length,
        
    new AsyncCallback(asyncCallBackDelegate), clientStream);
    clientStream.EndWrite(endResult);

    clientStream.Flush();
    }

    Now lets say that row one is chad and row two is joe, on the second loop the clientstream will be joed instead of joe because the client stream isnt clear but the stream has been sent.  What do i do to get joe sent instead of joed.

     

     

  • 9/25/2009 5:03 PM In reply to

    Re: NetworkStream help?

    The problem is not with clientStream, but rather with memoryStream. You are always writing to offset 0 in the memory stream without cleaning it out. Try adding:

    memoryStream.Seek(0,SeekOrigin.Begin);
    memoryStream.SetLength(0);

    prior to writing to the memory stream.

    However, one might ask why you're writing to the memory stream and then immediately writing that data to your network stream. Why not write directly to the network stream? Or why not write all of your data to the memory stream in a loop, then write the memory stream to the network stream after the loop?
  • 9/25/2009 5:58 PM In reply to

    Re: NetworkStream help?

    About writing to the memorystream and then back to the networkstream... I initially designed this for seralized classes so I am still removing a lot of the binary formatting code that was in it.  And thank you, I fixed the problem.
Page 1 of 1 (3 items) Previous Next