C# UDP通信 大数据情况下丢包解决思路
C# UDP通信 大数据情况下丢包
用SOCKET UDP通信写了个程序 但是在数据量很大的时候(信息收发速度加快、信息量大时)会出现丢包的情况,请大神帮忙看看程序
Socket udpServer;
IPEndPoint serverIP=new IPEndPoint(IPAddress.Parse(xxx.xx.x.xxx),9199);
udpServer.Bind(serverIP);
EndPoint.Remote=(EndPoint)serverIP
new Thread(()=>
{
while(true)
{
byte[] data=new byte[102400];
try
{
length=udpServer.ReceiveFrom(data, ref Remote);
if(length<=0)
break;
string str1=Encoding.GetEncoding("GBK").GetString(data,0,length);
Invoke(new ThreadStart(delegate()
{
this.textBox1.ApprendText(str1+Environment.NewLine);
this.textBox1.ScollToCaret();
}))
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}).Start();
将收到的str1逐行添加到textbox1时,会出现丢包
------解决方案--------------------
LZ可以改异步试试,比多线程效率高
用SOCKET UDP通信写了个程序 但是在数据量很大的时候(信息收发速度加快、信息量大时)会出现丢包的情况,请大神帮忙看看程序
Socket udpServer;
IPEndPoint serverIP=new IPEndPoint(IPAddress.Parse(xxx.xx.x.xxx),9199);
udpServer.Bind(serverIP);
EndPoint.Remote=(EndPoint)serverIP
new Thread(()=>
{
while(true)
{
byte[] data=new byte[102400];
try
{
length=udpServer.ReceiveFrom(data, ref Remote);
if(length<=0)
break;
string str1=Encoding.GetEncoding("GBK").GetString(data,0,length);
Invoke(new ThreadStart(delegate()
{
this.textBox1.ApprendText(str1+Environment.NewLine);
this.textBox1.ScollToCaret();
}))
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
}).Start();
将收到的str1逐行添加到textbox1时,会出现丢包
------解决方案--------------------
LZ可以改异步试试,比多线程效率高
static DataTable dt;
static DataTable Abbdt;
static SqlHelper objhelper;
static SqlConnection conn;
static OtherClass oc;
// Attributes
private ArrayList m_aryClients = new ArrayList(); // List of Client Connections
public const string GaoxinUDPProtocol = @"FDFDV\d.\d{2}.\d{2}L\d{3}DT(2\d\d\d[-](0[1-9]
------解决方案--------------------
1[012])[-](0[1-9]
------解决方案--------------------
[12][0-9]
------解决方案--------------------
3[01])\s+\d{2}:\d{2}:\d{2}),(.*)FEFE";
public const string GaoxinUDPData = @"^[0-9]*:[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?:[0-9]*$";
public const char Comma = ',';
public const char Colon = ':';
static Regex reg;
static Regex dataReg;
/// <summary>
/// Application starts here. Create an instance of this class and use it
/// as the main object.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
objhelper = new SqlHelper();
conn = objhelper.getConn();
conn.Open();
oc = new OtherClass();
reg = new Regex(GaoxinUDPProtocol);
dataReg = new Regex(GaoxinUDPData);