0

我需要接收多个数据包,但我只收到一个数据包,我希望下一个数据包的值应该覆盖同一数组中前一个数据包的值

例如我的代码示例是:

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();
}

在这里,我只在我的数据数组中接收到一次值,但是如果我需要在单个数组中接收多个数据包,那么我该怎么做。

请帮我

提前致谢..

4

1 回答 1

2
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);
}
于 2012-08-08T08:37:29.010 回答