我的概况:
我几年没用C#了,已经全部忘记了。
但现在又必须使用这个工具了。问题介绍:
我在嵌入式系统中已经完成了web服务程序,通过IE输入我在嵌入式系统中的IP即可看到事先设定好的网页。通过UDP给这个嵌入式系统(192.168.0.174:1025,本机端口定义一个一起发送过去)具体的IP和端口发送任意字符后,我也已经将处理好的字符串回送(目前是原封不动回送)。
需要解决的问题:
在C#的WinForm中给定一个IP输入框、一个远端端口输入框、一个本地端口输入框、一个发送文本输入框、一个接收文本显示框、一个“发送”按钮。用UDP方式将输入信息发送过去,并接收信息显示到“显示”框中。
因为确实已经几乎全部忘记了C#,所以请兄弟姐妹们将做好的完成此项功能的小项目打成压缩包发送到
谢谢各位了,因为很急,所以在短时间内得到我需要的东西的话我将另外开一贴给最辛苦的人100分。
(请别发往我在这儿登记的邮箱了,因为已经被盗了   再次感谢大家! )

解决方案 »

  1.   

    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Windows.Forms;class UdpTest : Form
    {
      TextBox tbxHost, tbxPort, tbxSend, tbxRecv;  UdpTest()
      {
        Text                = "Udp Test";
        Width               = 500;
        Height              = 400;    Label lblHost       = new Label();
        lblHost.Parent      = this;
        lblHost.Text        = "远端服务器URL或IP(&H)";
        lblHost.Top         = 5;
        lblHost.Left        = 10;
        lblHost.AutoSize    = true;    tbxHost             = new TextBox();
        tbxHost.Parent      = this;
        tbxHost.Top         = 22;
        tbxHost.Left        = 10;
        tbxHost.Width       = 150;
        tbxHost.MaxLength   = 50;
        tbxHost.BorderStyle = BorderStyle.FixedSingle;    Label lblPort       = new Label();
        lblPort.Parent      = this;
        lblPort.Text        = "端口(&P)";
        lblPort.Top         = 5;
        lblPort.Left        = 165;
        lblPort.AutoSize    = true;    tbxPort             = new TextBox();
        tbxPort.Parent      = this;
        tbxPort.Top         = 22;
        tbxPort.Left        = 165;
        tbxPort.Width       = 50;
        tbxPort.MaxLength   = 5;
        tbxPort.BorderStyle = BorderStyle.FixedSingle;    Label lblSend       = new Label();
        lblSend.Parent      = this;
        lblSend.Text        = "要发送的文本(&S)";
        lblSend.Top         = 49;
        lblSend.Left        = 10;
        lblSend.AutoSize    = true;    tbxSend             = new TextBox();
        tbxSend.Parent      = this;
        tbxSend.Multiline   = true;
        tbxSend.Top         = 66;
        tbxSend.Left        = 10;
        tbxSend.Width       = 205;
        tbxSend.Height      = ClientSize.Height - tbxSend.Top - 5;
        tbxSend.Anchor     |= AnchorStyles.Bottom;
        tbxSend.WordWrap    = false;
        tbxSend.ScrollBars  = ScrollBars.Both;    Button btnSend      = new Button();
        btnSend.Parent      = this;
        btnSend.Text        = "发送(&T)";
        btnSend.Top         = 5;
        btnSend.Left        = 225;
        btnSend.Width       = 100;
        btnSend.Height      = 38;
        btnSend.Click      += new EventHandler(UdpSend);    Label lblRecv       = new Label();
        lblRecv.Parent      = this;
        lblRecv.Text        = "接收到的文本(&R)";
        lblRecv.Top         = 49;
        lblRecv.Left        = 225;
        lblRecv.AutoSize    = true;    tbxRecv             = new TextBox();
        tbxRecv.Parent      = this;
        tbxRecv.Multiline   = true;
        tbxRecv.Top         = 66;
        tbxRecv.Left        = 225;
        tbxRecv.Width       = ClientSize.Width  - tbxRecv.Left - 5;
        tbxRecv.Height      = ClientSize.Height - tbxRecv.Top  - 5;
        tbxRecv.Anchor     |= AnchorStyles.Bottom | AnchorStyles.Right;
        tbxRecv.ReadOnly    = true;
        tbxRecv.WordWrap    = false;
        tbxRecv.ScrollBars  = ScrollBars.Both;
      }  void UdpSend(object sender, EventArgs ea)
      {
        ((Button)sender).Enabled = false;
        try
        {
          string   Host = tbxHost.Text.Trim();
          int      Port = int.Parse(tbxPort.Text.Trim());
          byte []    b0 = Encoding.UTF8.GetBytes(tbxSend.Text);
          UdpClient   u = new UdpClient(Host, Port);
          u.Send(b0, b0.Length);
          IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
          byte []    b1 = u.Receive(ref ep);
          string     s1 = Encoding.UTF8.GetString(b1);
          tbxRecv.Text += string.Format("Recive from {0}:{1}{3}{2}{3}{3}",
            ep.Address, ep.Port, s1, Environment.NewLine);
          u.Close();
        }
        catch (Exception e)
        {
          MessageBox.Show(e.ToString());
        }
        finally
        {
          ((Button)sender).Enabled = true;
        }
      }  static void Main()
      {
        Application.Run(new UdpTest());
      }
    }