implements a NetworkStream client 2 : NetworkStream « Network « C# / C Sharp






implements a NetworkStream client 2

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

 /*
  Example15_12b.cs implements a NetworkStream client
*/

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

public class Example15_12b 
{

  public static void Main() 
  {

    // create a client socket
    TcpClient newSocket = new TcpClient("localhost", 50001);

    // create a NetworkStream to read from the host
    NetworkStream ns = newSocket.GetStream();

    // fill a byte array from the stream
    byte[] buf = new byte[100];
    ns.Read(buf, 0, 100);

    // convert to a char array and print
    char[] buf2 = new char[100];
    for(int i=0;i<100;i++)
      buf2[i]=(char)buf[i];
    Console.WriteLine(buf2);

    // clean up
    ns.Close();
    newSocket.Close();

  }

}



           
       








Related examples in the same category

1.Write to a NetworkStream
2.implements a NetworkStream server
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