Write to a NetworkStream : NetworkStream « Network « C# / C Sharp






Write to a NetworkStream

 
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;


class Program {
    static void Main(string[] args) {
        TcpClient client = new TcpClient();
        IPHostEntry host = Dns.GetHostByName("www.google.com");
        client.Connect(host.AddressList[0], 8000);

        NetworkStream clientStream = client.GetStream();

        string request = "LIST";
        byte[] requestBuffer = Encoding.ASCII.GetBytes(request);
        clientStream.Write(requestBuffer, 0, requestBuffer.Length);

        byte[] responseBuffer = new byte[256];
        MemoryStream memStream = new MemoryStream();
        int bytesRead = 0;
        do {
            bytesRead = clientStream.Read(responseBuffer, 0, 256);
            memStream.Write(responseBuffer, 0, bytesRead);
        } while (bytesRead > 0);



    }
}

 








Related examples in the same category

1.implements a NetworkStream server
2.implements a NetworkStream client 2
3.Acts as a server program to demonstrate the use of the NetworkStream classActs as a server program to demonstrate the use of the NetworkStream class
4.Acts as a client program to demonstrate the use of the NetworkStream classActs as a client program to demonstrate the use of the NetworkStream class