54SA.COM|专注于系统亚博体育官方网址管理,为中国SA提供动力!
Getting online shouldn't be tough. $7.99 .COMs
系统管理员之家Banner
当前位置: 主页 > 编程开发 > C#开发 >

C# TCP发送消息和传输文件(四)

时间:2011-07-05 16:54来源:未知 编辑:admin

TcpListenerHelper

Code
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Net.Sockets;
using?System.Net;
using?System.Threading;

namespace?Xpwy.Backup.PTcpHelper
{
????
public?class?TcpListenerHelper
????{
????????
private?string?_strServerIP?=?"";
????????
private?int?_serverPort?=?0;

????????TcpListener?server;
????????TcpClient?client;
????????NetworkStream?netstream;
????????IAsyncResult?asyncResult;
????????TcpCommon?tcpCommon?=?
new?TcpCommon();

????????ManualResetEvent?listenConnected?=?
new?ManualResetEvent(false);

????????
bool?_active?=?false;

????????
public?TcpListenerHelper(string?strServerIP,?int?serverPort)
????????{
????????????_strServerIP?=?strServerIP;
????????????_serverPort?=?serverPort;
????????????server?=?
new?TcpListener(IPAddress.Parse(strServerIP),?serverPort);
????????????server.Server.ReceiveTimeout?=?
6000;
????????????server.Server.SendTimeout?=?
6000;
????????}

????????
///?

????????///?启动
????????
///?
????????public?void?Start()
????????{
????????????
try
????????????{
????????????????_active?=?
true;
????????????????server.Start();
????????????}
????????????
catch?(Exception?ex)
????????????{
????????????????
throw?ex;
????????????}
????????}

????????
///?
????????
///?停止
????????
///?
????????public?void?Stop()
????????{
????????????
try
????????????{
????????????????_active?=?
false;
????????????????
if?(client?!=?null)
????????????????{
????????????????????client.Close();
????????????????}

????????????????
if?(netstream?!=?null)
????????????????{
????????????????????netstream.Close();
????????????????}
????????????????server.Stop();
????????????????
????????????}
????????????
catch?(Exception?ex)
????????????{
????????????????
throw?ex;
????????????}
????????}

????????
public?void?Listen()
????????{
????????????listenConnected.Reset();
????????????asyncResult?=?server.BeginAcceptTcpClient(
new?AsyncCallback(AsyncCall),?server);
????????}

????????
public?void?AsyncCall(IAsyncResult?ar)
????????{
????????????
try
????????????{
????????????????TcpListener?tlistener?=?(TcpListener)ar.AsyncState;

????????????????
if?(_active)
????????????????{
????????????????????client?=?tlistener.EndAcceptTcpClient(ar);
????????????????????netstream?=?client.GetStream();
????????????????}
????????????????
else
????????????????{
????????????????????client?=?
null;
????????????????????netstream?=?
null;
????????????????}
????????????????listenConnected.Set();
????????????}
????????????
catch?(Exception?ex)
????????????{
????????????????
throw?ex;
????????????}
????????}????????
public?bool?WaitForConnect()
????????{
????????????listenConnected.WaitOne();

????????????
if?(client?!=?null?&&?netstream?!=?null)
????????????{
????????????????
return?true;
????????????}
????????????
else
????????????{
????????????????
return?false;
????????????}
????????}
????????

????????
#region?TcpCommon所有方法
????????
///?
????????
///?计算文件的hash?
????????
///?
????????public?string?CalcFileHash(string?FilePath)
????????{
????????????
return?tcpCommon.CalcFileHash(FilePath);
????????}

????????
///?
????????
///?发送文件
????????
///?
????????
///?
????????
///?
????????public?bool?SendFile(string?filePath)
????????{
????????????
return?tcpCommon.SendFile(filePath,?netstream);
????????}

????????
///?
????????
///?接收文件
????????
///?
????????
///?
????????
///?
????????public?bool?ReceiveFile(string?filePath)
????????{
????????????
return?tcpCommon.ReceiveFile(filePath,?netstream);
????????}

????????
///?
????????
///?发送消息
????????
///?
????????
///?
????????
///?
????????public?bool?SendMessage(string?message)
????????{
????????????
return?tcpCommon.SendMessage(message,?netstream);
????????}

????????
///?
????????
///?接收消息
????????
///?
????????
///?
????????public?string?ReadMessage()
????????{
????????????
return?tcpCommon.ReadMessage(netstream);
????????}
????????
#endregion

????????
#region?IDisposable?成员

????????
public?void?Dispose()
????????{
????????????Stop();
????????}

????????
#endregion
????}
}


?

?

C# TCP发送消息和传输文件(一)开发背景
C# TCP发送消息和传输文件(二)TcpCommon
C# TCP发送消息和传输文件(三)TcpClientHelper
C# TCP发送消息和传输文件(四)TcpListenerHelper
C# TCP发送消息和传输文件(五)server端
C# TCP发送消息和传输文件(六)client端

[责任编辑:admin]


------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
用户名: 匿名?
最新评论 进入详细评论页>>