资源简介
C#UDP通信+文件传输。文件传输有点问题,不能选择想保存文件的地址,是保存在了程序中写死了的地址。让我比较郁闷!
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace BroadcastExample
{
public class ConnectionClient
{
Socket sokMsg;
DGShowMsg dgShowMsg;//负责 向主窗体文本框显示消息的方法委托
DGShowMsg dgRemoveConnection;// 负责 从主窗体 中移除 当前连接
Thread threadMsg;
#region 构造函数
///
///
///
/// 通信套接字
/// 向主窗体文本框显示消息的方法委托
public ConnectionClient(Socket sokMsg DGShowMsg dgShowMsg DGShowMsg dgRemoveConnection)
{
this.sokMsg = sokMsg;
this.dgShowMsg = dgShowMsg;
this.dgRemoveConnection = dgRemoveConnection;
this.threadMsg = new Thread(RecMsg);
this.threadMsg.IsBackground = true;
this.threadMsg.Start();
}
#endregion
bool isRec = true;
#region 02负责监听客户端发送来的消息
void RecMsg()
{
while (isRec)
{
try
{
byte[] arrMsg = new byte[1024 * 1024 * 2];
//接收 对应 客户端发来的消息
int length = sokMsg.Receive(arrMsg);
//将接收到的消息数组里真实消息转成字符串
string strMsg = System.Text.Encoding.UTF8.GetString(arrMsg 0 length);
//通过委托 显示消息到 窗体的文本框
dgShowMsg(strMsg);
}
catch (Exception ex)
{
isRec = false;
//从主窗体中 移除 下拉框中对应的客户端选择项,同时 移除 集合中对应的 ConnectionClient对象
dgRemoveConnection(sokMsg.RemoteEndPoint.ToString());
}
}
}
#endregion
#region 03向客户端发送消息
///
/// 向客户端发送消息
///
///
public void Send(string strMsg)
{
byte[] arrMsg = System.Text.Encoding.UTF8.GetBytes(strMsg);
byte[] arrMsgFinal = new byte[arrMsg.Length + 1];
arrMsgFinal[0] = 0;//设置 数据标识位等于0,代表 发送的是 文字
arrMsg.CopyTo(arrMsgFinal 1);
sokMsg.Send(arrMsgFinal);
}
#endregion
#region 04向客户端发送文件数据 +void SendFile(string strPath)
///
/// 04向客户端发送文件数据
///
/// 文件路径
public void SendFile(string strPath)
{
//通过文件流 读取文件内容
using (FileStream fs = new FileStream(strPath FileMode.OpenOrCreate))
{
byte[] arrFile = new byte[1024 * 1024 * 2];
//读取文件内容到字节数组,并 获得 实际文件大小
int length = fs.Read(arrFile 0 arrFile.Length);
//定义一个 新数组,长度为文件实际长度 +1
byte[] arrFileFina
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 17920 2011-10-20 16:23 BroadcastExample\BroadcastExample\bin\Debug\BroadcastExample.exe
文件 42496 2011-10-20 16:23 BroadcastExample\BroadcastExample\bin\Debug\BroadcastExample.pdb
文件 14328 2011-10-20 16:05 BroadcastExample\BroadcastExample\bin\Debug\BroadcastExample.vshost.exe
文件 490 2007-07-21 01:33 BroadcastExample\BroadcastExample\bin\Debug\BroadcastExample.vshost.exe.manifest
文件 3534 2011-10-19 15:25 BroadcastExample\BroadcastExample\BroadcastExample.csproj
文件 4623 2011-10-19 15:25 BroadcastExample\BroadcastExample\ConnectionClient.cs
文件 18495 2011-10-20 16:14 BroadcastExample\BroadcastExample\Form1.cs
文件 6662 2011-10-20 14:37 BroadcastExample\BroadcastExample\Form1.Designer.cs
文件 5814 2011-10-20 14:37 BroadcastExample\BroadcastExample\Form1.resx
文件 1502 2010-05-06 16:15 BroadcastExample\BroadcastExample\obj\BroadcastExample.csproj.FileListAbsolute.txt
文件 2254 2011-10-20 16:05 BroadcastExample\BroadcastExample\obj\Debug\BroadcastExample.csproj.FileListAbsolute.txt
文件 847 2011-10-20 14:37 BroadcastExample\BroadcastExample\obj\Debug\BroadcastExample.csproj.GenerateResource.Cache
文件 17920 2011-10-20 16:23 BroadcastExample\BroadcastExample\obj\Debug\BroadcastExample.exe
文件 180 2011-10-20 14:37 BroadcastExample\BroadcastExample\obj\Debug\BroadcastExample.Form1.resources
文件 42496 2011-10-20 16:23 BroadcastExample\BroadcastExample\obj\Debug\BroadcastExample.pdb
文件 180 2011-10-20 10:24 BroadcastExample\BroadcastExample\obj\Debug\BroadcastExample.Properties.Resources.resources
文件 4608 2011-09-27 16:45 BroadcastExample\BroadcastExample\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
文件 491 2011-10-20 14:14 BroadcastExample\BroadcastExample\Program.cs
文件 1204 2010-04-26 22:31 BroadcastExample\BroadcastExample\Properties\AssemblyInfo.cs
文件 2856 2011-05-09 16:03 BroadcastExample\BroadcastExample\Properties\Resources.Designer.cs
文件 5612 2010-04-26 22:31 BroadcastExample\BroadcastExample\Properties\Resources.resx
文件 1114 2011-05-09 16:03 BroadcastExample\BroadcastExample\Properties\Settings.Designer.cs
文件 249 2010-04-26 22:31 BroadcastExample\BroadcastExample\Properties\Settings.settings
文件 938 2011-05-09 16:03 BroadcastExample\BroadcastExample.sln
..A..H. 18944 2011-10-19 16:54 BroadcastExample\BroadcastExample.suo
文件 1189 2011-05-09 16:03 BroadcastExample\UpgradeLog.xm
文件 3348 2011-05-09 16:03 BroadcastExample\_UpgradeReport_Files\UpgradeReport.css
文件 12505 2008-01-11 01:25 BroadcastExample\_UpgradeReport_Files\UpgradeReport.xslt
文件 69 2011-05-09 16:03 BroadcastExample\_UpgradeReport_Files\UpgradeReport_Minus.gif
文件 71 2011-05-09 16:03 BroadcastExample\_UpgradeReport_Files\UpgradeReport_Plus.gif
............此处省略14个文件信息
评论
共有 条评论