日期:2014-05-17  浏览次数:20727 次

C# 关于UDP通信的问题
打算写个局域网QQ作为练习
刚开始写就遇到问题
代码贴下


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.Threading;
using System.Net;

namespace UDPclient_04
{
    public partial class Form1 : Form
    {
        UDPClient udp = new UDPClient();
        public delegate void Dele(string str);
        public Dele dele;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            // textBox1.Text= udp.GetMyIpAddress().ToString();
            
            IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress ip in ips)
            {
                 richTextBox1.Text += ip.ToString()+"\n";
            }
            //textBox1.Text = ips[3].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            udp.send(textBox2.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(receive));
            th.IsBackground = true;
            th.Start();
            
        }

        public void receive()
        {
            while(true)
            {