UDP文件传输问题

Harrison_2009 2009-08-30 10:23:18
要实现UDP点对点聊天中带有大文件传输,能识别是哪个人传送过来的!
新人这方面N多不懂,最好给点实例(大文件传输,能识别是哪个人传送过来的!)。
...全文
388 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
whhp_bdqn_good 2012-10-26
  • 打赏
  • 举报
回复
这个帖子对我的影响太大了,LZ和芥子老大的一问一答让我受益良多,多谢两位和楼上的各位了
beibeisay88 2010-03-16
  • 打赏
  • 举报
回复
谢谢分享~~~~~正在学习中~
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复
那接收时怎么接起来呢??
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复

发送消息的代码是:
IPEndPoint m_endPoint = new IPEndPoint(IPAddress.Parse(toIP), 8082);
UdpClient client = new UdpClient();
byte[] sendByte = System.Text.Encoding.UTF8.GetBytes(sendMsg.ToCharArray());
while (true)
{//若发送三次都失败,则提示消息发送失败。
try
{
client.Send(sendByte, sendByte.Length, m_endPoint);
break;
}
catch
{
if (retry < 3)
{
retry++;
continue;
}
else
{
break;
}
}
}
client.Close();
}

接收的代码:
bool UdpStatus = true;
byte[] myByte = null;
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8082);
UdpClient server = new UdpClient(endPoint);

while (UdpStatus)
{
try
{
myByte = server.Receive(ref endPoint);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
//消息处理
。。。。。。
}


文件传输时也差不多,如果是使用这种方法应该如何识别是哪个人传送过来的!
dancingbit 2009-08-31
  • 打赏
  • 举报
回复

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
int retry = 0;
FileStream myFile = new FileStream(ofd.FileName, FileMode.Open);
MessageBox.Show(ofd.FileName);
byte[] bty = new byte[4096];

IPEndPoint m_endPoint = new IPEndPoint(IPAddress.Parse(toIP), 8082);
UdpClient mclient = new UdpClient();
while(myFile.Read(bty, 0, 4096)>0)
//myFile.Flush();
//myFile.Close();
//while (true)
{//若发送三次都失败,则提示消息发送失败。
try
{
mclient.Send(bty, bty.Length, m_endPoint);
break;
}
catch(Exception f)
{
MessageBox.Show(f.Message);
if (retry < 3)
{
// retry++;
//continue;
}
else
{
MessageBox.Show("文件传送失败!");
break;
}
}
}
mclient.Close();
}


直接拿你的代码改的。
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复
芥子大虾,给点例子!刚学很不熟悉。。
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
1.定义一个1024或2048之类长度的byte[]。
2.循环读文件,每次读1024个字节。
3.发送。文件读完了循环终止。否则转2。
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复
就是不会分片。
byshome 2009-08-31
  • 打赏
  • 举报
回复
RUDP中有现成的示例与说明。
也可以参考Csharp_QQ CSDN的download里就有
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
不出错才怪,你也不管文件多大,一次性地发过去?

1.缓冲区大小是有限制的。
2.不考虑缓冲区大小也要考虑网络速度。
3.不考虑网络速度也要考虑接收方的缓冲区。
4.接收方读取数据的速度。

大文件,一般还是使用TCP发送好了,分片是肯定需要的了。
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
看看我回答的其他关于网络文件传送的帖子。
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
换TCP。
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复
获取是哪个人发来的我知道怎么解决了,endPoint就是!

继续原来的问题:UDP大文件传输。。
我发送的代码如下:

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
int retry = 0;
FileStream myFile = new FileStream(ofd.FileName, FileMode.Open);
MessageBox.Show(ofd.FileName);
byte[] bty = new byte[myFile.Length+2000];
myFile.Read(bty, 0, Convert.ToInt32(myFile.Length));
myFile.Flush();
myFile.Close();
IPEndPoint m_endPoint = new IPEndPoint(IPAddress.Parse(toIP), 8082);
UdpClient mclient = new UdpClient();
while (true)
{//若发送三次都失败,则提示消息发送失败。
try
{
mclient.Send(bty, bty.Length, m_endPoint);
break;
}
catch(Exception f)
{
MessageBox.Show(f.Message);
if (retry < 3)
{
retry++;
continue;
}
else
{
MessageBox.Show("文件传送失败!");
break;
}
}
}
mclient.Close();
}


发送小于1M的文件是没问题的,但大于1M就会出错!
错误信息是: 一个在数据报套接字上发送的消息大于内部消息缓冲器或其他一些网络限制,或该用户用于接收数据报的缓冲器比数据报小。
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复
我是以纯FileIsEND结束的。发送完了文件才发这个结束符!
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
1.你总不会就用了"END"作为结束符吧?要是你文件中有这个呢?
2.传送文件最好还是使用TCP,UDP丢包是平常的事。
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复

byte[] bty = new byte[4096];
while (myFile.Read(bty, 0, 4096) > 0)
{
mclient.Send(bty, bty.Length, m_endPoint);
}

因为发送的东西分了70+个片段,所以就发了70+次了吧!发送好像是没错了的。。可能是接收方面有问题!
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
你怎么知道发了70+个消息?
Harrison_2009 2009-08-31
  • 打赏
  • 举报
回复
这个不用按顺序拼??直接用接收到的就拼?
ms.Write(myByte, 0, myByte.Length);
怎么我发现发了70+个消息,侦听到的就那么几个的??
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
接收完消息后,取UdpClient的基础Socket,取RemoteEndPoint属性。
dancingbit 2009-08-31
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 jutuo2009cs 的回复:]
不用发一个序列使它按指定序列拼起来?
[/Quote]
什么意思?
加载更多回复(15)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧