Csharp/C Sharp/Network/NetworkStream
Содержание
Acts as a client program to demonstrate the use of the NetworkStream class
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// NStrCli.cs -- Acts as a client program to demonstrate the use of
// the NetworkStream class.
//
// Compile this program with the following command line.
// C:>csc NStrCli.cs
//
// To use this program with the companion server program, first start
// the server, then connect to it using the client program (this program)
// in a separate console window.
// As you enter lines from the client, they will appear in the server
// console window. To end the session, press <Enter> on a blank line.
//
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace nsStreams
{
public class NStrCli
{
static public void Main ()
{
IPAddress hostadd = Dns.Resolve("localhost").AddressList[0];
Console.WriteLine ("Host is " + hostadd.ToString());
IPEndPoint EPhost = new IPEndPoint(hostadd, 2048);
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp );
string str = "Hello, World!";
byte [] b;
StringToByte (out b, str);
s.Connect (EPhost);
NetworkStream strm = new NetworkStream (s, FileAccess.ReadWrite);
if (!s.Connected)
{
Console.WriteLine ("Unable to connect to host");
return;
}
strm.Write (b, 0, b.Length);
while (b[0] != 4)
{
string text = Console.ReadLine();
if (text.Length == 0)
{
b[0] = 4;
strm.Write (b, 0, 1);
break;
}
StringToByte (out b, text);
strm.Write (b, 0, text.Length);
}
strm.Close ();
s.Close ();
}
//
// Convert a buffer of type byte to a string
static string ByteToString (byte [] b, int start)
{
string str = "";
for (int x = start; x < b.Length; ++x)
{
str += (char) b [x];
}
return (str);
}
//
// Convert a buffer of type string to byte
static void StringToByte (out byte [] b, string str)
{
b = new byte [str.Length];
for (int x = 0; x < str.Length; ++x)
{
b[x] = (byte) str [x];
}
}
}
}
Acts as a server program to demonstrate the use of the NetworkStream class
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// NStrSvr.cs -- Acts as a server program to demonstrate the use of
// the NetworkStream class.
//
// Compile this program with the following command line.
// C:>csc NStrSvr.cs
//
// To use this program with the companion client program, first start
// the server (this program), then connect to it using the client program
// in a separate console window.
// As you enter lines from the client, they will appear in the server
// console window. To end the session, press <Enter> on a blank line.
//
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace nsStreams
{
public class NStrSvr
{
static public void Main ()
{
IPAddress hostadd = Dns.Resolve("localhost").AddressList[0];
Console.WriteLine ("Host is " + hostadd.ToString());
IPEndPoint EPhost = new IPEndPoint(hostadd, 2048);
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp );
s.Bind (EPhost);
s.Listen (0);
Socket sock = s.Accept();
NetworkStream strm = new NetworkStream (sock, FileAccess.ReadWrite);
byte [] b = new byte [256];
while (true)
{
for (int x = 0; x < b.Length; ++x)
b[x] = 0;
strm.Read (b, 0, b.Length);
if (b[0] == 4)
break;
string str = ByteToString (b, 0);
Console.WriteLine (str);
}
strm.Close ();
sock.Close ();
s.Close ();
}
// Convert a buffer of type byte to a string
static string ByteToString (byte [] b, int start)
{
string str = "";
for (int x = start; x < b.Length; ++x)
{
if (b[x] == 0)
break;
str += (char) b [x];
}
return (str);
}
}
}
implements a NetworkStream client 2
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_12b.cs implements a NetworkStream client
*/
using System;
using System.IO;
using System.Net.Sockets ;
public class Example15_12b
{
public static void Main()
{
// create a client socket
TcpClient newSocket = new TcpClient("localhost", 50001);
// create a NetworkStream to read from the host
NetworkStream ns = newSocket.GetStream();
// fill a byte array from the stream
byte[] buf = new byte[100];
ns.Read(buf, 0, 100);
// convert to a char array and print
char[] buf2 = new char[100];
for(int i=0;i<100;i++)
buf2[i]=(char)buf[i];
Console.WriteLine(buf2);
// clean up
ns.Close();
newSocket.Close();
}
}
implements a NetworkStream server
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_12a.cs implements a NetworkStream server
*/
using System;
using System.IO;
using System.Net.Sockets ;
public class Example15_12a
{
// Listen waits for connections
private void Listen()
{
// listen on port 50001
TcpListener tcpl = new TcpListener(50001);
tcpl.Start();
// wait for clients
for (;;)
{
// Block here waiting for client connections
Socket newSocket = tcpl.AcceptSocket();
if (newSocket.Connected)
{
// create a NetworkStream on the socket
NetworkStream ns = new NetworkStream(newSocket);
// send some data
byte[] buf = {(byte)"H", (byte)"e", (byte)"l", (byte)"l",
(byte)"o", (byte)" ", (byte)"N", (byte)"e", (byte)"t"};
ns.Write(buf, 0, 9);
// cleanup
ns.Flush();
ns.Close();
}
// clean up and quit
newSocket.Close();
break;
}
}
public static void Main()
{
// launch a listening thread
Example15_12a listener = new Example15_12a();
listener.Listen();
}
}
Write to a NetworkStream
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
class Program {
static void Main(string[] args) {
TcpClient client = new TcpClient();
IPHostEntry host = Dns.GetHostByName("www.google.ru");
client.Connect(host.AddressList[0], 8000);
NetworkStream clientStream = client.GetStream();
string request = "LIST";
byte[] requestBuffer = Encoding.ASCII.GetBytes(request);
clientStream.Write(requestBuffer, 0, requestBuffer.Length);
byte[] responseBuffer = new byte[256];
MemoryStream memStream = new MemoryStream();
int bytesRead = 0;
do {
bytesRead = clientStream.Read(responseBuffer, 0, 256);
memStream.Write(responseBuffer, 0, bytesRead);
} while (bytesRead > 0);
}
}