C#学习笔记之七(network I/O, Asynchonouse network I/O)

类别:.NET开发 点击:0 评论:0 推荐:

Network IO; 
 //Server1 programm
 using System.Net.Sockets;
 TcpListener tcpListener = new TcpListener(65000);
 tcpListener.Start();
 for(;;)
 {
  Socket clientSocket = tcpListener.AcceptSocket()
  if (clientSocket.Connected)
  {
   StreamFile(clientSocket);
   clientSocket.Close();
   break;
  }
 }
 void StreamFile(Socket clientSocket)
 {
  NetworkStream networkStream =
    new NetworkStream(clientSocket);
  System.IO.StreamWriter streamWriter =
    new System.IO.StreamWriter(networkStream);
  System.IO.StreamReader streamReader =
    new System.IO.StreamReader(@"C:\a.txt");
  string strStream;
  do {
   strStream = streamReader.ReadLine();
   if(strStream != null )
   {
    streamWriter.WriteLine(strStream);
    streamWriter.Flush();
   }
  } while (strStream != null);
  streamReader.Close();
  networkStream.Close();
  streamWriter.Close();         
 }
 
 //Client1 Code
 using System.Net.Sockets;
 TcpClient serverSocket;
 try {
  serverSocket = new TcpClient("localhost", 65000);
 } catch() {}
 NetworkStream networkStream =
   severSocket.GetStream();
 System.IO.StreamReader streamReader =
   new System.IO.StreamReader(networkStream);
 try {
  string strOut;
  do {
   if (strOut != null)
   {
    streamReader.ReadLine(strOut);
    if (strOut != null )
    {
     Console.WriteLine(strOut);
    }
   }
  } while (strOut != null );
 } catch() {}

Aysnch Network IO:
 //Server2
 ...
 TcpListener = tcpListener = new TcpListener(65000);
 tcpListener.Start();
 for(;;)
 {
  Socket socketForClient = tcpListener.AcceptSock();
  if (SocketForClient.Connected)
  {
   ClientHandler handler =
    new ClientHandler(socketForClient);
   handler.StartRead();
  }
 }
 
 class ClientHandler
 {
  private byte[] buff;
  private Socket socket;
  private NetworkStream networkSteam;
  private AsyncCallback cbRead;
  private AsyncCallback  cbWrite;
  
  public ClientHandler(Socket socketForClient)
  {
   socket = socketForClient;
   buff = new byte[256];
   networkStream =
    new NetworkStream(socketForClient);
   cbRead = new AsyncCallback(this.OnReadComplete);
   cbWrite = new AsyncCallback(this.OnWriteComplete);
  }
  
  public void StartRead()
  {
   networkStream.BeginRead(
    buff,
    0,
    buff.Length,
    cbRead,
    null );
  }
  private void OnReadComplete( IAsyncResult ar)
  {
   int bytesRead = networkStream.EndRead(ar);
   if ( butesRead > 0)
   {
    string s = System.Text.Encoding.ASCII.GetString(
     buff, 0 buytesRead);
    Console.Write("Received {0} bytes from client: {1},
     bytesRead, s);
    networkStream.BeginWrite(
     buff, 0, bytesRead, cbWrite, null);
   }
   else
   {
    Console.WriteLine("Read connection dropped");
    networkStream.Close();
    socket.Close();
    networkStream = null;
    socket = null;
   }
  } //OnReadComplete
  private void OnWriteComplete(IAsyncResult ar)
  {
   networkStream.EndWrite(ar)
   Console.WriteLine("Write complete");
   networkStream.BeginRead(
    buff, 0, buff.Length,
    cbRead, null );
   
  } // OnWriteComplete
 }
 
 
 // Client2 Code
 Network NetworkStream streamToServer;
 TcpClient tcpSocket =
  new TcpClient("localhost", 65000);
 streamToServer = tcpSocket.GetStream();
 string message = "This is a string sent from the client!";
 StreamWriter writer =
  new StreamWriter(streamToServer);
 writer.WriteLine(message)
 writer.Flush();
 StreamReader reader =
  new StreamReader(streamToServer);
 string strResponse = reader.ReadLine();
 Console.WriteLine("Received: {0}", strResonse);
 streamToServer.Close();
 return 0;
 

本文地址:http://com.8s8s.com/it/it45069.htm