Christopher Johnson
Christopher Johnson

Reputation: 2629

create a "listener" for an incoming socket stream that will contain a byte[] array

I'm creating a socket server that needs to continuously listen for incoming messages from the connected clients. Those messages will be sent in a byte[] array. I had the server working great with a StreamReader but StreamReader only works with textual represenations of the data being sent...not byte[] arrays.

Here's what I had:

        StreamReader reader = new StreamReader(Client.GetStream());
        string line = "";
        while (true)
        {
            line = reader.ReadLine();
            if (!string.IsNullOrEmpty(line))
            {
                parentForm.ApplyText(line + "\r\n");
                SocketServer.SendBroadcast(line);
            }
        }

I need to now convert that into a raw stream somehow that will convert the stream contents into a byte[] array but I can't seem to get a handle on it.

I tried this:

        while (true)
        {
            var bytes = default(byte[]);
            using (var memstream = new MemoryStream())
            {
                var buffer = new byte[512];
                var bytesRead = default(int);
                while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
                    memstream.Write(buffer, 0, bytesRead);
                bytes = memstream.ToArray();
            }
            //parentForm.ApplyText(bytes.Length + "\r\n");
        }

but as you might guess, the while(true) loop doesn't quite work how I need it to. Can anyone help me with some code adjustment to make this work as I need it to. It needs to continuously listen for incoming messages, then when a message is received, it needs to do something with that message (the byte[] array) then go back to listening again.

TIA

Upvotes: 0

Views: 2933

Answers (2)

JamieSee
JamieSee

Reputation: 13030

There's no need to convert anything. GetStream() returns a NetworkStream. See the sample Microsoft includes in the NetworkStream.Read Method. All you have to do is replace the myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead)); line with an appropriate storage mechanism.

Upvotes: 0

Darshan Puranik
Darshan Puranik

Reputation: 1083

I guess "listening continuously" is not task of reader its a task of listener. I ran into same problem when i was writing server using TcpListener. I am not sure what you want to do but i am posting solution for your "listening continuous" and reading into byte[] problem. I guess this code might help you:

TcpListener t = new TcpListener(IPAddress.Loopback, _port);
        t.Start();

        Console.WriteLine("Server is started and waiting for client\n\n");

        byte[] buff = new byte[255];
        NetworkStream stream;
        TcpClient client;
        while(true)
        {
            client = t.AcceptTcpClient();
            if (!client.Connected)
               return;

            stream = client.GetStream();
            while ((stream.Read(buff, 0, buff.Length)) != 0)
            {
                break;
            }

            if (0 != buff.Length)
                break;
        }  

Upvotes: 3

Related Questions