高分求UDP广播分包组包

破碎的脸 2011-05-04 08:15:41
通过UDP广播发送一张大概160K的图片(大小未知),然后接收并显示。。。有没有高手来帮忙实现一下?
...全文
154 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
破碎的脸 2011-05-05
  • 打赏
  • 举报
回复
没有人会吗?
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
你加我QQ嘛,我把工程发给你,麻烦你看看。。。图片在200K左右
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
再大都不行,要拆包再组包才可以
我姓区不姓区 2011-05-04
  • 打赏
  • 举报
回复
data = new byte[102400];
把这个byte数组再设置大一点试试
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
一个在数据报套接字上发送的消息大于内部消息缓冲区或其他一些网络限制,或该用户用于接收数据报的缓冲区比数据报小。
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
高手,你加我QQ嘛 43725443
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
就是收不到那张图片
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
高手,加我QQ吧43725443
我姓区不姓区 2011-05-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jhkemail 的回复:]

我调试没通过呀
[/Quote]
没通过是指什么?
破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
我调试没通过呀
我姓区不姓区 2011-05-04
  • 打赏
  • 举报
回复
参考:
服务端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Drawing;

namespace UDPTest
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];

//构建TCP 服务器

//得到本机IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8001);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

//绑定网络地址
newsock.Bind(ipep);

Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());

//等待客户机连接
Console.WriteLine("Waiting for a client");

//得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

//客户机连接成功后,发送欢迎信息
string welcome = "Welcome ! ";

//字符串与字节数组相互转换
data = Encoding.ASCII.GetBytes(welcome);

//发送信息
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data = new byte[102400];
//发送接受信息
recv = newsock.ReceiveFrom(data, ref Remote);
MemoryStream ms = new MemoryStream(data);
Image img = Image.FromStream(ms);
img.Save("E:\\1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}



客户端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Drawing;
using System.IO;

namespace UDPTestClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input, stringData;

//构建TCP 服务器

Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());

//设置服务IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);

//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

string welcome = "Hello! ";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;

data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

Image img = Image.FromFile("F:\\1.jpg");
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
data = ms.ToArray();
server.SendTo(data, Remote);


server.Close();
}
}
}

破碎的脸 2011-05-04
  • 打赏
  • 举报
回复
有高手来个实例没?

110,538

社区成员

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

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

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