打算写个局域网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)
            {
                
                 richTextBox1.Invoke(dele,udp.receive_any());
            }
        }        private void Form1_Load(object sender, EventArgs e)
        {
            dele = new Dele(showmessage);
        }        public void showmessage(string str)
        {
            richTextBox1.Text+=str+"\n";
        }
    }
}
UDP类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Net;
using System.Net.Sockets;
using System.Threading;namespace UDPclient_04
{
    class UDPClient
    {
        public IPEndPoint ipendpoint = null;
        public IPAddress ipaddress = null;
        public UdpClient udp;        public UDPClient()
        {
            
            ipendpoint = new IPEndPoint(IPAddress.Parse("192.168.1.88"), 11111);
            udp = new UdpClient(ipendpoint);
        }
               public void send(string str)
        {
            byte[] by = Encoding.Default.GetBytes(str);  
            udp.Send(by,by.Length,ipendpoint);
        }        public IPAddress GetMyIpAddress()
        {
            return Dns.GetHostAddresses("")[0];
        }
        
        public string receive_any()
        {
            IPAddress i = IPAddress.Any;
            IPEndPoint a = new IPEndPoint(i, 11111);
            return Encoding.Default.GetString(udp.Receive(ref a));
        }
    }
}
我的内网IP固定为192.168.1.88
我想用我的机器作为信息中转点,所有运行的程序都把信息发给我但是我在虚拟机中运行这个程序时候出错,说文件被另外程序使用,而且我主系统也不能"双开",这是什么原因?  大神指点下  多谢。

解决方案 »

  1.   


    1台机器上运行 设置2个端口 1个接受端口 和1个发送端口
    http://www.cnblogs.com/zhili/archive/2012/09/01/UDP_Multicast.html
      

  2.   

    发送方可以是任意、多个端口,但接收的侦听端口只能是一个,你可以将接收独立出来。因为UDP本身不像TCP有握手的连接,所以在发送报文的时候,要带上多种发送方的信息,以便接收区分。独立出来的接收端,通过包头分辨发送方,并进行相应处理,最好有设缓冲区,这样处理粘包比较方便。
      

  3.   

    UDP的一个端口 只能提供一个服务使用···不像TCP可以多个同时监听··