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空间完美搬家到新浪博客!
- 07-01· 北凉国的那些事(组诗)
- 07-01· 《乱世佛音》 第七章 巨变
- 07-01· 南匈奴始末(3)
- 06-30· 流浪在波希米尼亚
- 06-30· 希尼亚从大洋西岸放飞新
- 06-28· 瑪利亞之城 - 家庭日 "光
- 06-28· 至青年营弟兄姐妹的一封
- 06-26· 《三国群英大富翁》追忆
- 06-24· 东莞血汗工厂实录(281:沙田
- 06-22· 第一次看戏
- 06-22· 经典复刻,独一无二:试
- 06-22· 蓝旗营教学中心9月份盛大
- 06-22· 品牌折扣女装嫣然品牌折
- 06-21· IQVopdnkvdz 1100
- 06-21· kriyoylto8fyds'p;tyijyfuifiogoi
- 06-21· 巴黎春天缤纷圣诞 喜迎新
- 06-21· 晒JS宝宝贝贝些 咯
- 06-21· 司马氏的谥法和葬仪
- 06-21· [转载]司马氏的谥法和葬仪
- 06-21· 闲来蓟县看秋山