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

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

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

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

TcpClientHelper

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

namespace?Xpwy.Backup.PTcpHelper
{
????
public?class?TcpClientHelper:IDisposable
????{
????????TcpClient?client;
????????NetworkStream?netstream;
????????
string?_serverip?=?"127.0.0.1";
????????
int?_port?=?8080;

????????TcpCommon?tcpCommon?=?
new?TcpCommon();

????????
#region?TcpClientHelper?constructor
????????
public?TcpClientHelper(string?strServerIP,?int?serverPort)
????????{
????????????_serverip?=?strServerIP;
????????????_port?=?serverPort;

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

????????
public?void?Start()
????????{
????????????client?=?
new?TcpClient(_serverip,?_port);
????????????netstream?=?client.GetStream();
????????}

????????
public?void?Stop()
????????{
????????????
if?(netstream?!=?null)
????????????{
????????????????netstream.Close();
????????????}

????????????
if?(client?!=?null)
????????????{
????????????????client.Close();
????????????}
????????}

????????
#region?TcpCommon所有方法
????????
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()
????????{
????????????
if?(netstream?!=?null)
????????????{
????????????????netstream.Close();
????????????}

????????????
if?(client?!=?null)
????????????{
????????????????client.Close();
????????????}
????????}

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

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

[责任编辑:admin]


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