Click here to Skip to main content
14,982,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an sample application which send a TCP request to a server and the server returns a byte array. This is then saved to a memory stream for processing. The client appears to successfully save the byte array to the memory stream as when a break point is positioned int he code before it leaves the function the memory stream has data in it.

enum type

class NetworkObject
    public TcpClient tcp;
    public type ConnectionType;
    public string URL;
    public MemoryStream AudioMemoryStream;

private TcpClient tcpclientAudio = new TcpClient();
private NetworkObject audioObj = null;

private void button1_Click(object sender, EventArgs e)
    if (tcpclientAudio.Connected)
    tcpclientAudio = new TcpClient();

    // id is the call id of the audio you want to get

    // Image Test
    string audioId = recordIdTextBox.Text.Trim();
    string audioType = type.Audio.ToString();

    string message = "GET /fred?id=" + audioId + "&live=false HTTP/1.1\r\n" + "Sec-WebSocket-Key: 4eW1rRpCzr60VdvY3lZZMw==\r\n";

    audioObj = new NetworkObject();
    audioObj.tcp = tcpclientAudio;
    audioObj.ConnectionType = type.Audio;
    audioObj.URL = message;
    audioObj.AudioMemoryStream = new MemoryStream();

    tcpclientAudio.BeginConnect(IPAddress.Parse(""), 8008, new AsyncCallback(CallBackConnectMethod), audioObj);

void CallBackConnectMethod(IAsyncResult result)
    NetworkObject obj = (NetworkObject)result.AsyncState;

    TcpClient tcp = obj.tcp;

    NetworkStream ns = tcp.GetStream();

    byte[] buffer = new byte[2048];
    int read = 0;
    string message = obj.URL;
    byte[] request = Encoding.ASCII.GetBytes(message);
    ns.Write(request, 0, request.Length);
    read = ns.Read(buffer, 0, 1000);

    Console.WriteLine(Encoding.ASCII.GetString(buffer, 00, read));

    obj.AudioMemoryStream = new MemoryStream();
    // check for switching protocols
            read = ns.Read(buffer, 0, 1);
            if (buffer[0] == 0x82)
                read = ns.Read(buffer, 0, 1);
                int len = 0;
                switch (buffer[0])
                    case 126:
                        read = ns.Read(buffer, 0, 2);
                        len = (buffer[0] << 8) + buffer[1];
                        len = buffer[0];

                 read = ns.Read(buffer, 0, len);
                 if (obj.ConnectionType == type.Update)
                     Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, read));
                     obj.AudioMemoryStream.Write(buffer, 0, read);
                 read = 0;
         catch (Exception)
             read = 0; // connection is lost
     while (read > 0);

     Debug.WriteLine("Just leaving");

What I have tried:

Initially the memory stream was not in the NetworkObject class but this was added as it is defined outside the thread and passed in. As it is initialised and populated with the thread I hoped it would still be available on the main thread once it had been populated.
Gerry Schmitz 5-Nov-20 10:13am
On the "main thread" for what? It just looks like you're doing a bunch of "locally" scoped stuff.
Richard Deeming 5-Nov-20 10:18am
You haven't shown the code where you're checking the stream. At a guess, you're trying to access it before the callback has finished executing.
aksl14 5-Nov-20 10:36am
I'm running this as a forms app within dev studio. I click the button and it fires the tcp request. I then use a break point on the "just leaving" debug.writeline to check what is in the memory stream. Once that has happened I "continue" in the debugger and click another button which has a break point. After stepping through I found my bug as the if the stream wasn't null I was closing it. Removing check now means it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900