程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C#學習筆記之七(network I/O, Asynchonouse network I/O)

C#學習筆記之七(network I/O, Asynchonouse network I/O)

編輯:.NET實例教程


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; 

$False$


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; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved