为什么我的客户端服务器udp代码未发送?

时间:2018-09-28 12:50:49

标签: c# .net winforms udp ip-address

最近我对tcp,udp和命名管道很感兴趣,并试图自学。我正在尝试建立udp连接,以了解其工作原理。我在带有richtextBox1和btStart的Windows窗体应用程序中有一个客户端和服务器程序。我想在同一台计算机上进行此设置,然后尝试使用两台计算机。我什么也没发。谁能告诉我我在做什么错?我只是想学习。

这是服务器代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace UDPServer
{
public partial class Form1 : Form
{
    delegate void ShowMessageMethod(string msg);

    UdpClient _server = null;
    IPEndPoint _client = null;
    Thread _listenThread = null;
    private bool _isServerStarted = false;

    public Form1()
    {
        InitializeComponent();
    }
    private void serverMsgBox_Load(object sender, EventArgs e)
    {
        this.btStart.Text = "StartServer";
    } 

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void btStart_Click(object sender, EventArgs e)
    {
        if (_isServerStarted)
        {
            Stop();
            btStart.Text = "StartServer";
        }
        else
        {
            Start();
            btStart.Text = "StopServer";
        }

    }
    private void Start()
    {
        //Create the server.
        IPEndPoint serverEnd = new IPEndPoint(IPAddress.Any, 1234);
        _server = new UdpClient(serverEnd);
        ShowMsg("Waiting for a client...");
        //Create the client end.
        _client = new IPEndPoint(IPAddress.Any, 0);

        //Start listening.
        Thread listenThread = new Thread(new ThreadStart(Listening));
        listenThread.Start();
        //Change state to indicate the server starts.
        _isServerStarted = true;
    }

    private void Stop()
    {
        try
        {
            //Stop listening.
            listenThread.Join();
            ShowMsg("Server stops.");
            _server.Close();
            //Changet state to indicate the server stops.
            _isServerStarted = false;
        }
        catch (Exception excp)
        { }
    }

    private void Listening()
    {
        byte[] data;
        //Listening loop.
        while (true)
        {
            //receieve a message form a client.
            data = _server.Receive(ref _client);
            string receivedMsg = Encoding.ASCII.GetString(data, 0, data.Length);
            //Show the message.
            this.Invoke(new ShowMessageMethod(ShowMsg), new object[] { "Client:" + receivedMsg });
            //Send a response message.
            data = Encoding.ASCII.GetBytes("Server:" + receivedMsg);
            _server.Send(data, data.Length, _client);
            //Sleep for UI to work.
            Thread.Sleep(500);
        }
    }
    private void ShowMsg(string msg)
    {
        this.richTextBox1.Text += msg + "\r\n";
    }
}
}

这是客户端代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace UDPClient
{
public partial class Form1 : Form
{
    UdpClient _server = null;
    IPEndPoint _client = null;

    public Form1()
    {
        InitializeComponent();
    }



    private void Form1_Load(object sender, EventArgs e)
    {

    }
   private void serverMsgBox_Load(object sender, EventArgs e)
    {
        //Get the server.
        _server = new UdpClient("127.0.0.1", 16000);
        //Create a client.
        _client = new IPEndPoint(IPAddress.Any, 0);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        try
        {
            _server.Close();
        }
        catch (Exception s)
        { 
        }
    }

    private void btSend_Click(object sender, EventArgs e)
    {
        try
        {
            //Send the input message.
            string text = this.richTextBox1.Text;
            _server.Send(Encoding.ASCII.GetBytes(text), text.Length, _client);
            //Receive the response message.
            byte[] data = _server.Receive(ref _client);
            string msg = Encoding.ASCII.GetString(data, 0, data.Length);
            //Show the response message.
            this.richTextBox1.Text = msg;
        }
        catch (Exception exp)
        { 

        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

作为一般规则,当您要检查连接性问题或调试正在发送的内容时,可以使用Fiddler或ZAP之类的代理,以便能够拦截流量,进行分析和处理,以防万一。您需要,这将为您提供很多信息。

作为您问题的可能解决方案,请查看Microsoft documentation