Create a NetworkStream from a Socket : NetworkStream « Network « C# / CSharp Tutorial






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

class MainClass
{
  public static void Main() 
  {
    TcpListener tcpl = new TcpListener(9999);
    tcpl.Start();

    for (;;)
    {
      Socket newSocket = tcpl.AcceptSocket();
      if (newSocket.Connected)
      {
        NetworkStream ns = new NetworkStream(newSocket);

        byte[] buf = {1,2,3,4,5,6,7,8,9};
        ns.Write(buf, 0, 9);

        ns.Flush();
        ns.Close();

      }
      newSocket.Close();
      break;

    }
  }

}








33.16.NetworkStream
33.16.1.Create a NetworkStream from a Socket
33.16.2.NetworkStream TcpClient
33.16.3.Use NetworkStream to read and write to a server
33.16.4.Tcp client: Use StreamReader and StreamWriter to read and write to a server
33.16.5.Network Streaming Client
33.16.6.Network Streaming Server