您的位置:首页 > 服装鞋帽 > 男装 > Stream ----NetworkStream

Stream ----NetworkStream

luyued 发布于 2011-03-15 22:56   浏览 N 次  

1.构建 客户端-服务器 应用程序

=======================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
class Program
{
const int PORT_NO = 5000;
const string SERVER_IP = "127.0.0.1";
static void Main(string[] args)
{
//监听指定IP地址和端口
IPAddress localAdd = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAdd, PORT_NO);
Console.WriteLine("Listening...");
listener.Start();
//进来的客户端连接
TcpClient client = listener.AcceptTcpClient();
//通过流获取进来的数据
NetworkStream nwStream = client.GetStream();
byte[] buffer = new byte[client.ReceiveBufferSize];
//读取进来的流
int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
//接收的数据转换成字符串
string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received :" + dataReceived);
//给客户端写回文本
Console.WriteLine("Sending back : " + dataReceived);
nwStream.Write(buffer, 0, bytesRead);

client.Close();
listener.Stop();
Console.ReadLine();
}
}
}
=========================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
const int PORT_NO = 5000;
const string SERVER_IP = "127.0.0.1";

static void Main(string[] args)
{
//字符串发送到服务器
string textToSend = DateTime.Now.ToString();
//在IP和端口上,建立一个TCPClient对象
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
//发送文本
Console.WriteLine("Sending : " + textToSend);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//读回文本
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);

Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
Console.ReadLine();

client.Close();
}
}
}
=================================================================================================

2.构建多用户的服务器应用程序

-------------------------------------

服务器

-------------------------Client.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Server
{
class Client
{
//创建一个TCPlient对象
TcpClient _client = null;
//用于发送和接收数据
byte[] buffer;
//当客户端连接时,唤醒
public Client(TcpClient client)
{
_client = client;
//从开始客户端异步读取数据
buffer = new byte[_client.ReceiveBufferSize];
_client.GetStream().BeginRead(buffer, 0, _client.ReceiveBufferSize,receiveMessage, null);
}
public void receiveMessage(IAsyncResult ar)
{
int bytesRead;
try
{
lock (_client.GetStream())
{
//从客户端读
bytesRead = _client.GetStream().EndRead(ar);
}
if (bytesRead < 1)
return;
else
{
//获取发送来的信息
string messageReceived = ASCIIEncoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Received : " + messageReceived);
//给客户端写回文本
Console.WriteLine("Sending back : " + messageReceived);
byte[] dataToSend = ASCIIEncoding.ASCII.GetBytes(messageReceived);
_client.GetStream().Write(dataToSend, 0, dataToSend.Length);
}
//继续从客户端读
lock (_client.GetStream())
{
_client.GetStream().BeginRead(buffer, 0, _client.ReceiveBufferSize, receiveMessage, null);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
--------------------Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
class Program
{
const int PORT_NO = 5000;
const string SERVER_IP = "127.0.0.1";
static void Main(string[] args)
{
////监听指定IP地址和端口
//IPAddress localAdd = IPAddress.Parse(SERVER_IP);
//TcpListener listener = new TcpListener(localAdd, PORT_NO);
//Console.WriteLine("Listening...");
//listener.Start();
////进来的客户端连接
//TcpClient client = listener.AcceptTcpClient();
////通过流获取进来的数据
//NetworkStream nwStream = client.GetStream();
//byte[] buffer = new byte[client.ReceiveBufferSize];
////读取进来的流
//int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
////接收的数据转换成字符串
//string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);
//Console.WriteLine("Received :" + dataReceived);
////给客户端写回文本
//Console.WriteLine("Sending back : " + dataReceived);
//nwStream.Write(buffer, 0, bytesRead);

//client.Close();
//listener.Stop();
//Console.ReadLine();
//监听指定的IP地址和端口
IPAddress localAddress = IPAddress.Parse(SERVER_IP);
TcpListener listener = new TcpListener(localAddress, PORT_NO);
Console.WriteLine("Listening...");
listener.Start();

while (true)
{
//引入客户端连接
Client user = new Client(listener.AcceptTcpClient());
}
}
}
}
---------------------------------

客户端

--------------------------Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace Client
{
class Program
{
const int PORT_NO = 5000;
const string SERVER_IP = "127.0.0.1";

static void Main(string[] args)
{
//字符串发送到服务器
string textToSend = DateTime.Now.ToString();
//在IP和端口上,建立一个TCPClient对象
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
NetworkStream nwStream = client.GetStream();
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(textToSend);
//发送文本
Console.WriteLine("Sending : " + textToSend);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
//读回文本
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);

Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
Console.ReadLine();

client.Close();
}
}
}


MSN空间完美搬家到新浪博客!

上一篇:古希腊女神 下一篇:一些软件安装命令
广告赞助商