如何在单个阵列中的单个套接字上使用UDP接收多个数据包
问题描述:
我需要接收多个数据包,但我只获取一个数据包,而且我希望下一个数据包的值应该覆盖之前数据包的值阵列如何在单个阵列中的单个套接字上使用UDP接收多个数据包
例如我的代码的样品是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using microLCC;
namespace A2LDFCSolver
{
public partial class Form1 : Form
{
int m;
byte[] data = new byte[1024];
//byte[] buffer = new byte[10];
IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1235);
Socket newsocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint sen = new IPEndPoint(IPAddress.Loopback, 5001);
microLCC.microLCC uLC = new microLCC.microLCC();
EndPoint tmp;
StateObject obj = new StateObject();
int count = 0;
int cout = 0;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public Form1()
{
InitializeComponent();
newsocket.Bind(endpoint);
tmp = (EndPoint)sen;
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void bw_Reset(object sender, RunWorkerCompletedEventArgs e)
{
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBox1.Text = "Message Recieved" + count.ToString();
}
private void bw_doit(object sender, DoWorkEventArgs e)
{
StateObject state = new StateObject();
while (true)
{
allDone.Reset();
//newsocket.ReceiveFrom(data, 0, 40, SocketFlags.None, ref tmp);
newsocket.BeginReceive(data, 0, 40, 0, new AsyncCallback(ReadCallback), state);
backgroundWorker1.ReportProgress(0);
allDone.WaitOne();
}
}
protected void ReadCallback(IAsyncResult ar)
{
allDone.Set();
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
//Socket newsocket = (Socket)ar.AsyncState;
//newsocket.EndReceive(ar);
count = count + 1;
for (m = 0; m <= 17; m = m + 1)
{
if (m <= 7)
{
uLC.setDAC_raw(m, data[m]);
}
if (m >= 8 && m <= 14)
{
if (data[m] == 1)
{
uLC.setRelay((m - 7), true);
}
if (data[m] == 0)
{
uLC.setRelay((m - 7), false);
}
}
if (m == 15)
{
uLC.setEPM_CaS(data[m]);
}
if (m == 16)
{
uLC.setEPM_CrS(data[m]);
}
}
send(newsocket);
}
private void send(Socket newsocket)
{
StateObject state = new StateObject();
byte[] buffer = new byte[1024];
for (int i = 0; i < 6; i++)
{
buffer[i] = (Byte)uLC.getADC(i);
}
newsocket.BeginSendTo(buffer, 0, 7, 0, sen,new AsyncCallback(SendCallback), state);
}
protected void SendCallback(IAsyncResult ar)
{
try
{
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
int s = newsocket.EndSendTo(ar);
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
public class StateObject
{
public Socket workSocket = null;
public const int BufferSize = 1024;
public StringBuilder sb = new StringBuilder();
}
在这里,我接收在我的数据阵列中的值仅一次,但如果我需要的单个阵列中接收一个以上的数据包然后如何我可以那样做吗?
请帮我提前
谢谢..
答
IPEndPoint sen = new IPEndPoint(IPAddress.Loopback, 5001);
EndPoint tmp = (EndPoint)sen;
newsocket.Bind(endpoint);
int count=0;
while(newsocket.Available>0 && count<data.Length)
{
count+= newsocket.ReceiveFrom(data,count, 40, SocketFlags.None, ref tmp);
}
感谢,这是工作的帮助,但实际上根据我的要求,我需要第二分组中的值应该来在阵列的起点..例如,我只需要获取17个值,它们可以从数组的第一位直到数组的第17位存储,如果我得到另一个数据包,它应该覆盖数组的前一个值,并且应该从数组的第一个位置.... – user1465977 2012-08-08 11:09:43
@ user1465977: - newsocket.ReceiveFrom(data,0,40,..)并声明一个数组列表并继续添加它是传入的数据包。 – perilbrain 2012-08-08 11:14:55
@匿名ymous: - 再次感谢您可以通过编写代码告诉我,我该怎么做.. – user1465977 2012-08-08 11:26:48