从我的网页(客户端)发送消息到PC(服务器)?

时间:2015-09-01 17:54:25

标签: c# asp.net udp

我想从我的网页(客户端)发送消息到电脑 当我尝试发送数据时出现问题。它抱怨这一行:m_socWorker.Send(byData);错误消息是未将对象引用设置为对象的实例。 这是我的代码

 // Counts the pings
private int pingsSent;
// Can be used to notify when the operation completes
AutoResetEvent resetEvent = new AutoResetEvent(false);
public static Socket m_socWorker;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{


    if (DropDownList2.SelectedIndex != null)
    {
        // Reset the number of pings
        pingsSent = 0;
        // Clear the textbox of any previous content
        TextBox2.Dispose();
        TextBox2.Text += "Pinging " + DropDownList1.SelectedValue + " with 32 bytes of data:\r\n\r\n";
        // Send the ping
        Thread thd = new Thread(new ThreadStart(SendPing));
        thd.Start();
        //SendPing();
        try
        {

            UdpClient udpclient = new UdpClient();

            IPAddress remotipadd = IPAddress.Parse(DropDownList1.SelectedValue);
            udpclient.Connect(remotipadd,10001);
            byte[] data = new byte[1024];
            IPEndPoint senderip = new IPEndPoint(remotipadd, Convert.ToInt32(DropDownList2.SelectedValue));

            udpclient.Connect(senderip);
            TextBox2.Text += "Connect";
           // listbox1.Items.Add("connected");



        }
        catch (SocketException s)
        {
            MessageBox.Show(s.Message);
        }


    }

}
private void SendPing()
{
    System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();

    // Create an event handler for ping complete
    pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);

    // Create a buffer of 32 bytes of data to be transmitted.
    byte[] packetData = Encoding.ASCII.GetBytes("................................");

    // Jump though 50 routing nodes tops, and don't fragment the packet
    PingOptions packetOptions = new PingOptions(50, true);

    // Send the ping asynchronously

    pingSender.SendAsync(DropDownList1.SelectedValue, 5000, packetData, packetOptions, resetEvent);

}

private void pingSender_Complete(object sender, PingCompletedEventArgs e)
{
    // If the operation was canceled, display a message to the user.
    if (e.Cancelled)
    {
        TextBox2.Text += "Ping was canceled...\r\n";

        // The main thread can resume
        ((AutoResetEvent)e.UserState).Set();
    }
    else if (e.Error != null)
    {
        TextBox2.Text += "An error occured: " + e.Error + "\r\n";

        // The main thread can resume
        ((AutoResetEvent)e.UserState).Set();
    }
    else
    {
        PingReply pingResponse = e.Reply;
        // Call the method that displays the ping results, and pass the information with it
        ShowPingResults(pingResponse);
    }
}

public void ShowPingResults(PingReply pingResponse)
{
    if (pingResponse == null)
    {
        // We got no response
        TextBox2.Text += "There was no response.\r\n\r\n";
        return;
    }
    else if (pingResponse.Status == IPStatus.Success)
    {
        // We got a response, let's see the statistics
        TextBox2.Text += "Reply from " + pingResponse.Address.ToString() + ": bytes=" + pingResponse.Buffer.Length + " time=" + pingResponse.RoundtripTime + " TTL=" + pingResponse.Options.Ttl + "\r\n";
    }
    else
    {
        // The packet didn't get back as expected, explain why
        TextBox2.Text += "Ping was unsuccessful: " + pingResponse.Status + "\r\n\r\n";
    }
    // Increase the counter so that we can keep track of the pings sent
    pingsSent++;
    // Send 4 pings
    if (pingsSent < 4)
    {
        SendPing();
    }
}


protected void Button2_Click(object sender, EventArgs e)
{

    try
    {
        Object objData = TextBox2.Text;
        byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
        //Session["m_socWorker"] = m_socWorker;
        //m_socWorker = (Socket) Session["m_socWorker"];
        m_socWorker.Send(byData);
    }
    catch (SocketException se)
    {
        MessageBox.Show(se.Message);
    }
}

0 个答案:

没有答案