udp广播,数据分包。。。。。。。。。。

lucbesson 2005-04-20 02:48:52
哪位朋友有例子,给个提示啊
...全文
163 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lucbesson 2005-04-20
  • 打赏
  • 举报
回复
up
conan19771130 2005-04-20
  • 打赏
  • 举报
回复
另一个例子
using System;
using System.Net;
using System.Net.Sockets;
using System.Data;
using System.Text;
using cn.com.daway;

namespace cn.com.daway
{
/// <summary>
/// 包
/// </summary>
public class package
{
/// <summary>
/// 查询包
/// </summary>
public struct QueryPackage
{
public int nOracle; //oracle查询序列号
public int nSelect; //select列长
public int nFrom; //from长
public int nWhere; //where长
public string strSelect; //select列
public string strFrom; //from长
public string strWhere; //where长
public int nPageSize; //分页大小
public int nPage; //页号
public bool bReturn; //是否返回记录总数
}
/// <summary>
/// 用户登录包
/// </summary>
public struct LoginPackage
{
public int nUser; //用户名长度
public string strUser; //用户名
public int nPassword; //密码长度
public string strPassword; //密码
}
/// <summary>
/// 用户登录结果包
/// </summary>
public struct LoginResult
{
public int nResult; //结果长度
public string strResult; //结果内容
public int nPower; //权限长度
public string strPower; //权限
}
private Rijndael m_aes;
private byte[] byteHead; //包头,4位类型,4位包长
private int MAX_PACKAGE_SIZE=1024;
public package()
{
m_aes=new Rijndael();
byteHead=new byte[8];
}
/// <summary>
/// 抛出错误
/// </summary>
/// <param name="nType">类型号</param>
/// <param name="nLength">包长</param>
/// <param name="socket">socket</param>
private void throwErrors(int nType,int nLength,Socket socket)
{
if(nType==0)
{
byte[] byteError=new byte[nLength];
socket.Receive(byteError);
string strError=Encoding.Unicode.GetString(byteError,0,nLength);
Exception err=new Exception(strError);
throw err;
}
}
/// <summary>
/// 发送包
/// </summary>
/// <param name="socket">socket</param>
/// <param name="bytePackage">包内容明文</param>
/// <param name="nType">包类型</param>
public void SendPackage(Socket socket,byte[] bytePackage,int nType)
{
int nLength=bytePackage.Length;
SendHead(socket,nType,nLength);
int nTimes,nLeft=MAX_PACKAGE_SIZE;
nTimes=nLength/MAX_PACKAGE_SIZE;
byte []byteTempBuff=new byte[1];
byte []byteTemp=m_aes.Encrypt(bytePackage);
if(nLength%MAX_PACKAGE_SIZE!=0)
nTimes++;
for(int i=0;i<nTimes;i++)
{
if(i==nTimes-1)
nLeft=byteTemp.Length-MAX_PACKAGE_SIZE*i;
socket.Send(byteTemp,MAX_PACKAGE_SIZE*i,nLeft,SocketFlags.None);
socket.Receive(byteTempBuff);
}
}

/// <summary>
/// 收包
/// </summary>
/// <param name="socket">socket</param>
/// <param name="nType">包类型</param>
/// <returns>2进制明文</returns>
public byte[] ReceivePackage(Socket socket,ref int nType)
{
try
{
int nLength=0;
ReceiveHead(socket,ref nType,ref nLength);
int nTrueLength=((nLength+32)/32)*32+4;
byte[] byteTempBuff=new byte[1];
byteTempBuff[0]=1;
byte[] byteTemp=new byte[nTrueLength];
int nTimes,nLeft=MAX_PACKAGE_SIZE;
nTimes=nLength/MAX_PACKAGE_SIZE;
if(nLength%MAX_PACKAGE_SIZE!=0)
nTimes++;
for(int i=0;i<nTimes;i++)
{
if(i==nTimes-1)
nLeft=nTrueLength-MAX_PACKAGE_SIZE*i;
int nRet=socket.Receive(byteTemp,MAX_PACKAGE_SIZE*i,nLeft,SocketFlags.None);
if(nRet==0)
{
Exception err=new Exception("客户端退出");
throw(err);
}
socket.Send(byteTempBuff);
}
return m_aes.Decrypt(byteTemp);
}
catch(Exception err)
{
throw err;
}

}

/// <summary>
/// 收取包头
/// </summary>
/// <param name="socket">socket</param>
/// <param name="nType">包类型</param>
/// <param name="nLength">包长度</param>
private void ReceiveHead(Socket socket,ref int nType,ref int nLength)
{
int nRet=socket.Receive(byteHead);
if(nRet==0)
{
Exception err=new Exception("客户端退出");
throw(err);
}
nType=BitConverter.ToInt32(byteHead,0);
nLength=BitConverter.ToInt32(byteHead,4);
byte []byteEnd=new byte[1];
byteEnd[0]=1;
socket.Send(byteEnd);
}
/// <summary>
/// 发送包头
/// </summary>
/// <param name="socket">socket</param>
/// <param name="nType">包类型</param>
/// <param name="nLength">包长度</param>
private void SendHead(Socket socket,int nType,int nLength)
{
byte []byteType=BitConverter.GetBytes(nType);
byte []byteLength=BitConverter.GetBytes(nLength);
byte []byteTemp=new byte[1];
byteType.CopyTo(byteHead,0);
byteLength.CopyTo(byteHead,4);
socket.Send(byteHead);
socket.Receive(byteTemp);
}
/// <summary>
/// 2进制转成LoginPackage
/// </summary>
/// <param name="byteContent">2进制内容</param>
/// <returns>LoginPackage</returns>
public LoginPackage ByteToLoginPackage(byte[] byteContent)
{
LoginPackage result=new LoginPackage();
result.nUser=BitConverter.ToInt32(byteContent,0);
result.strUser=Encoding.Unicode.GetString(byteContent,4,result.nUser);
result.nPassword=BitConverter.ToInt32(byteContent,4+result.nUser);
result.strPassword=Encoding.Unicode.GetString(byteContent,8+result.nUser,result.nPassword);
return result;
}
/// <summary>
/// LoginPackage(用户名,密码)转成2进制
/// </summary>
/// <param name="strUser">用户名</param>
/// <param name="strPassword">密码</param>
/// <returns>2进制内容</returns>
public byte[] LoginPackageToByte(string strUser,string strPassword)
{
byte[] byteUserContent=Encoding.Unicode.GetBytes(strUser);
byte[] byteUserLength=BitConverter.GetBytes(byteUserContent.Length);
byte[] bytePasswordContent=Encoding.Unicode.GetBytes(strPassword);
byte[] bytePasswordLength=BitConverter.GetBytes(bytePasswordContent.Length);
byte[] byteResult=new byte[8+byteUserContent.Length+bytePasswordContent.Length];
byteUserLength.CopyTo(byteResult,0);
byteUserContent.CopyTo(byteResult,4);
bytePasswordLength.CopyTo(byteResult,4+byteUserContent.Length);
bytePasswordContent.CopyTo(byteResult,8+byteUserContent.Length);
return byteResult;
}
/// <summary>
/// 2进制转成LoginResult(登录结果)
/// </summary>
/// <param name="byteContent">2进制内容</param>
/// <returns>LoginResult(登录结果)</returns>
public LoginResult ByteToLoginResult(byte[] byteContent)
{
LoginResult result=new LoginResult();
result.nResult=BitConverter.ToInt32(byteContent,0);
result.strResult=Encoding.Unicode.GetString(byteContent,4,result.nResult);
result.nPower=BitConverter.ToInt32(byteContent,4+result.nResult);
result.strPower=Encoding.Unicode.GetString(byteContent,8+result.nResult,result.nPower);
return result;
}

/// <summary>
/// LoginResult(登录结果)转成2进制内容
/// </summary>
/// <param name="strResult">结果</param>
/// <param name="strPower">权限</param>
/// <returns>2进制内容</returns>
public byte[] LoginResultToByte(string strResult,string strPower)
{
byte[] byteResultContent=Encoding.Unicode.GetBytes(strResult);
byte[] byteResultLength=BitConverter.GetBytes(byteResultContent.Length);
byte[] bytePowerContent=Encoding.Unicode.GetBytes(strPower);
byte[] bytePowerLength=BitConverter.GetBytes(bytePowerContent.Length);
byte[] byteResult=new byte[8+byteResultContent.Length+bytePowerContent.Length];
byteResultLength.CopyTo(byteResult,0);
byteResultContent.CopyTo(byteResult,4);
bytePowerLength.CopyTo(byteResult,4+byteResultContent.Length);
bytePowerContent.CopyTo(byteResult,8+byteResultContent.Length);
return byteResult;
}
}
}
conan19771130 2005-04-20
  • 打赏
  • 举报
回复
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Data;
using com.zggps;

namespace com.zggps
{
/// <summary>
/// package 的摘要说明。
/// </summary>
public class package
{
public static int BUFF_RECORDS=100;
/// <summary>
/// 包
/// </summary>
public struct Type_Package
{
public short infoLength; //信息长度
public byte info_def; //信息标识
public byte[] info_content; //秤重点编号
}

/// <summary>
/// 发送包
/// </summary>
public struct Type_SendPackage
{
public ushort infoLength; //包长度
public byte info_def; //信息标识
public byte info_id; //流水号
public byte place_id; //秤重点编号
public ushort count; //信息条数
public byte[] info_content; //信息内容
}

/// <summary>
/// 发送结果包
/// </summary>
public struct Type_SendResultPackage
{
public bool bContinue; //是否继续发送
public int idx; //记录集中记录条数索引号
public string strId; //流水号
public int nStart; //开始在记录集中索引号
}

/// <summary>
/// 发送结果包
/// </summary>
public struct Type_ResultPackage
{
public ushort infoLength;
public byte info_def;
public byte info_id;
public byte result;
}
/// <summary>
/// 包转2进制,错误抛出
/// </summary>
/// <param name="pak">普通包</param>
/// <returns>2进制</returns>
public static byte[] ToByte(Type_Package pak)
{
try
{
byte[] bytePak=new byte[pak.infoLength];
bytePak[0]=0x20;
byte[] byteLength=ShortToByte((ushort)pak.infoLength);
byteLength.CopyTo(bytePak,1);
bytePak[3]=pak.info_def;
pak.info_content.CopyTo(bytePak,4);
bytePak[pak.infoLength-1]=0x21;
return bytePak;
}
catch
{
throw(new Exception("转换普通包错误"));
}
}

/// <summary>
/// 发送包转2进制,错误抛出
/// </summary>
/// <param name="pakSend">发送包</param>
/// <returns>2进制</returns>
public static byte[] ToByte(Type_SendPackage pak)
{
try
{
byte[] bytePak=new byte[pak.infoLength];
bytePak[0]=0x20;
byte[] byteLength=ShortToByte(pak.infoLength);
byteLength.CopyTo(bytePak,1);
bytePak[3]=pak.info_def;
bytePak[4]=pak.info_id;
bytePak[5]=pak.place_id;
byteLength=ShortToByte(pak.count);
byteLength.CopyTo(bytePak,6);
pak.info_content.CopyTo(bytePak,8);
bytePak[pak.infoLength-1]=0x21;
return bytePak;
}
catch
{
throw(new Exception("转换发送包错误"));
}
}

/// <summary>
/// 2进制转换成普通包
/// </summary>
/// <param name="pak"></param>
/// <param name="bytePak"></param>
public static void ToPackage(ref Type_Package pak,byte[] bytePak)
{
try
{

byte []byteLength=new byte[2];
byteLength[0]=bytePak[1];
byteLength[1]=bytePak[2];
ushort infoLength=ByteToShort(byteLength);
pak.info_def=bytePak[3];
pak.info_content =new byte[infoLength-5];
for(short i=0;i<infoLength-5;i++)
{
pak.info_content[i]=bytePak[4+i];
}
}
catch
{
throw(new Exception("普通包转2进制错误"));
}
}

public static void ToPackage(ref Type_ResultPackage pak,byte[] bytePak)
{
byte []bL=new byte[2];
bL[0]=bytePak[1];
bL[1]=bytePak[2];
pak.infoLength=ByteToShort(bL);
pak.info_def=bytePak[3];
pak.info_id=bytePak[4];
pak.result=bytePak[5];
}
/// <summary>
/// 转换记录到2进制
/// </summary>
/// <param name="row">记录</param>
/// <returns>2进制</returns>
public static byte[]MakeRecord(System.Data.DataRow row)
{
byte[] byteResult=new byte[30];
string strBh=row["车牌号"].ToString();
string id;
try
{
id=row["流水号"].ToString();
}
catch
{
throw(new Exception("记录中转换\"流水号\"出错"));
}
byte[] tmpBh=Encoding.Unicode.GetBytes(strBh);
if(tmpBh.Length>20)
{
throw(new Exception(string.Format("车牌号大于10位,流水号{0}",id)));
}
string strYear,strMonth,strDay,strHour,strMinute,strSecond;
try
{
strYear=(DateTime.Parse(row["记录时间"].ToString())).Year.ToString();
strMonth=(DateTime.Parse(row["记录时间"].ToString())).Month.ToString();
strDay=(DateTime.Parse(row["记录时间"].ToString())).Day.ToString();
strHour=(DateTime.Parse(row["记录时间"].ToString())).Hour.ToString();
strMinute=(DateTime.Parse(row["记录时间"].ToString())).Minute.ToString();
strSecond=(DateTime.Parse(row["记录时间"].ToString())).Second.ToString();
}
catch
{
throw(new Exception(string.Format("记录中转换\"记录时间\"出错,,流水号{0}",id)));
}
ushort shYear=ushort.Parse(strYear);
byte[] byteYear=package.ShortToByte(shYear);
byte byteMonth=byte.Parse(strMonth);
byte byteDay=byte.Parse(strDay);
byte byteHour=byte.Parse(strHour);
byte byteMinute=byte.Parse(strMinute);
byte byteSecond=byte.Parse(strSecond);
byte[] byteJz;
try
{
ushort sh=ushort.Parse(row["净重"].ToString());
byteJz=ShortToByte(sh);
}
catch
{
byteJz=new byte[2];
byteJz[0]=0xff;
byteJz[1]=0xff;
//throw(new Exception(string.Format("记录中转换净重出错,流水号{0}",id)));
}
bool bRj;
try
{
bRj=bool.Parse(row["是否日结"].ToString());
}
catch
{
throw(new Exception(string.Format("记录中转换\"是否日结\"出错,流水号{0}",id)));
}
//组合结果
tmpBh.CopyTo(byteResult,0);
for(int i=tmpBh.Length;i<20;i++)
{
byteResult[i]=0;
}
byteYear.CopyTo(byteResult,20);
byteResult[22]=byteMonth;
byteResult[23]=byteDay;
byteResult[24]=byteHour;
byteResult[25]=byteMinute;
byteResult[26]=byteSecond;
byteJz.CopyTo(byteResult,27);
if(bRj)
byteResult[29]=1;
else
byteResult[29]=0;
return byteResult;
}

/// <summary>
/// 2进制转字符串
/// </summary>
/// <param name="b">2进制</param>
/// <returns>字符串</returns>
public static string ByteToString(byte[] b)
{
string strResult="";
for(int i=0;i<b.Length;i++)
strResult+=ByteToHex(b[i])+" ";
return strResult;
}

private static string ByteToHex(byte b)
{
string strResult="";
byte h=(byte)(b/16);
byte l=(byte)(b%16);
switch(h)
{
case 10:
strResult+="A";
break;
case 11:
strResult+="B";
break;
case 12:
strResult+="C";
break;
case 13:
strResult+="D";
break;
case 14:
strResult+="E";
break;
case 15:
strResult+="F";
break;
default:
strResult+=h.ToString();
break;
}
switch(l)
{
case 10:
strResult+="A";
break;
case 11:
strResult+="B";
break;
case 12:
strResult+="C";
break;
case 13:
strResult+="D";
break;
case 14:
strResult+="E";
break;
case 15:
strResult+="F";
break;
default:
strResult+=l.ToString();
break;
}

return strResult;
}
/// <summary>
/// short转byte[2]
/// </summary>
/// <param name="o">ushort</param>
/// <returns>2进制</returns>
public static byte[] ShortToByte(ushort sh)
{
byte[] result=new byte[2];
result[0]=(byte)(sh/256);
result[1]=(byte)(sh%256);
return result;
}

/// <summary>
/// 2进制转short
/// </summary>
/// <param name="b">2进制</param>
/// <returns>short</returns>
public static ushort ByteToShort(byte []b)
{
ushort result;
result=(ushort)(b[0]*0x100+b[1]);
return result;
}
}
}
关键在自己订个最大包长
lucbesson 2005-04-20
  • 打赏
  • 举报
回复
我是第一次接触编程,第一次接触网络编程。

一直在研究

顺便当做散分的帖子吧。
wokagoka 2005-04-20
  • 打赏
  • 举报
回复
我也求!!

111,120

社区成员

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

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

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