Csharp/C Sharp by API/System.Net.Sockets/NetworkStream

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

NetworkStream.Flush()

<source lang="csharp"> /* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765

  • /

using System; using System.Net; using System.Net.Sockets; using System.Text; public class TcpClientSample {

  public static void Main()
  {
     byte[] data = new byte[1024];
     string input, stringData;
     TcpClient server;

     try
     {
        server = new TcpClient("127.0.0.1", 9050);
     } catch (SocketException)
     {
        Console.WriteLine("Unable to connect to server");
        return;
     }
     NetworkStream ns = server.GetStream();
     int recv = ns.Read(data, 0, data.Length);
     stringData = Encoding.ASCII.GetString(data, 0, recv);
     Console.WriteLine(stringData);
     while(true)
     {
        input = Console.ReadLine();
        if (input == "exit")
           break;
        ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
        ns.Flush();
        data = new byte[1024];
        recv = ns.Read(data, 0, data.Length);
        stringData = Encoding.ASCII.GetString(data, 0, recv);
        Console.WriteLine(stringData);
     }
     Console.WriteLine("Disconnecting from server...");
     ns.Close();
     server.Close();
  }

}


 </source>


NetworkStream.Read

<source lang="csharp"> using System; using System.Net; using System.Net.Sockets; using System.Text; public class NetworkStreamTcpClient {

  public static void Main()
  {
     byte[] data = new byte[1024];
     string input, stringData;
     int recv;
     IPEndPoint ipep = new IPEndPoint(
                     IPAddress.Parse("127.0.0.1"), 9050);
     Socket server = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
     server.Connect(ipep);
     NetworkStream ns = new NetworkStream(server);
     if (ns.CanRead)
     {
        recv = ns.Read(data, 0, data.Length);
        stringData = Encoding.ASCII.GetString(data, 0, recv);
        Console.WriteLine(stringData);
     }
     else
     {
        Console.WriteLine("Error: Can"t read from this socket");
        ns.Close();
        server.Close();
        return;
     }
     while(true)
     {
        input = "asdf";
        if (input == "exit")
           break;
        if (ns.CanWrite)
        {
           ns.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);
           ns.Flush();
        }
        recv = ns.Read(data, 0, data.Length);
        stringData = Encoding.ASCII.GetString(data, 0, recv);
        Console.WriteLine(stringData);
     }
     Console.WriteLine("Disconnecting from server...");
     ns.Close();
     server.Shutdown(SocketShutdown.Both);
     server.Close();
  }

}


 </source>


NetworkStream.Write

<source lang="csharp"> using System; using System.Text; using System.IO; using System.Net.Sockets;

public class TryTcp {

 public static void Main(String [] args) {
   TcpClient client = new TcpClient("www.nfex.ru", 80);
   NetworkStream stream = client.GetStream();
   byte[] send = Encoding.ASCII.GetBytes("GET HTTP/1.0 \r\n\r\n");
   stream.Write(send, 0, send.Length);
   byte[] bytes = new byte[client.ReceiveBufferSize];
   int count = stream.Read(bytes, 0, (int)client.ReceiveBufferSize);
   String data = Encoding.ASCII.GetString(bytes);
   char[] unused = {(char)data[count]};
   Console.WriteLine(data.TrimEnd(unused));
   stream.Close();
   client.Close();
 }

}


 </source>


new NetworkStream

<source lang="csharp">

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;
   }
 }

}


 </source>