C#学习笔记之七(network I/O, Asynchonouse network I/O)
2024-07-21 02:19:43
供稿:网友
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;