2

当我尝试从客户端发送消息时,服务器无法接收并打印该消息。谁能告诉我以下服务器客户端应用程序中的错误。

我创建了两个 WinForm 项目,一个是 UDP 服务器,另一个是 UDP 客户端。

在 UDP 服务器项目中,我创建了一个表单,其中包含一个名为richTextBox1 的 RichTextBox 来显示消息和一个名为 btStart 的按钮来启动/停止监听。这是代码片段:

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";
    }
}
}

在 UDP 客户端项目中,我还创建了一个表单,其中包含一个名为 RichTextBox1 的 RichTextBox 用于输入或显示消息,一个名为 btSend 的按钮用于发送输入消息。您可以运行该项目的多个实例。服务器将处理所有正在运行的客户端。这是代码片段:

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);
            //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)
        { 

        }
    }

}
}
4

1 回答 1

2

你没有设置你的目的地。您需要在使用UdpClient.Send (Byte[], Int32)之前使用 UdpClient.Connect或使用UdpClient.Send(Byte[], Int32, IPEndPoint)

于 2012-04-09T18:46:18.670 回答