Mega Code Archive

 
Categories / C# Tutorial / Network
 

Use a NetworkStream to read from a server

using System; using System.IO; using System.Net.Sockets ; class MainClass {   public static void Main()    {     TcpClient newSocket = new TcpClient("localhost", 50001);     NetworkStream ns = newSocket.GetStream();     byte[] buf = new byte[100];     ns.Read(buf, 0, 100);     char[] buf2 = new char[100];     for(int i=0;i<100;i++)       buf2[i]=(char)buf[i];     Console.WriteLine(buf2);     ns.Close();     newSocket.Close();   } }