本人因为不懂用udp在局域网传输数据,所以照着一位大侠的日志写了一个小的聊天程序。因为没有内网的环境,所以只是在单机的不同端口上做了试验。还有好多东西都不是很懂,请各位高人帮帮忙吧~先谢谢了!~~
S:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;namespace S
{
    public partial class Form1 : Form
    {
        UdpClient uc; //声明UDPClient
        UdpClient ucs;        public Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            ucs = new UdpClient(); //初始化
            uc = new UdpClient(8888);            //开一线程            Thread th = new Thread(new ThreadStart(listen));            //设置为后台            th.IsBackground = true;            th.Start();
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {            string temp = this.textBox1.Text; //保存TextBox文本            //将该文本转化为字节数组            byte[] b = System.Text.Encoding.UTF8.GetBytes(temp);            //向本机的8888端口发送数据            ucs.Send(b, b.Length, Dns.GetHostName(), 8889);        }        private void listen()
        {            //声明终结点            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.0.10"), 8888);            while (true)
            {                //获得Form1发送过来的数据包                string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep));                //加入ListBox                this.listBox1.Items.Add(text);            }C:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Configuration;namespace C
{
    public partial class Form1 : Form
    {
        UdpClient uc = null; //声明UDPClient
        UdpClient ucs;        public Form1()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            //注意此处端口号要与发送方相同
            ucs = new UdpClient(); //初始化
            uc = new UdpClient(8889);            //开一线程            Thread th = new Thread(new ThreadStart(listen));            //设置为后台            th.IsBackground = true;            th.Start();
        }        private void listen()
        {            //声明终结点            IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);
            
            while (true)
            {
                
                //获得Form1发送过来的数据包                string text = System.Text.Encoding.UTF8.GetString(uc.Receive(ref iep));                //加入ListBox
                
                this.listBox1.Items.Add(text);            }        }        private void button1_Click(object sender, EventArgs e)
        {
            string temp = this.textBox1.Text; //保存TextBox文本            //将该文本转化为字节数组            byte[] b = System.Text.Encoding.UTF8.GetBytes(temp);            //向本机的8888端口发送数据            ucs.Send(b, b.Length, Dns.GetHostName(), 8888);
        }
    }
}因为本人根本也不懂这些东西,看了很多资料也没有一个从头到尾教局域网聊天的,所以有如下几个问题:
1.能不能不通过服务端与客户端进行聊天,最好只是平级的客户端与客户端的,像QQ那样的?
2.如果上面的东西拿到内网上面会不会不能传输数据,如果不行该怎么改呢?
3.IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);里面的IP改成什么样都一样,这个有什么作用呢?
4.大家有没有做过类似的东西的,希望可以给我点帮助[email protected]
5.因为要交任务,所以只有这几天时间,如果这个根本就不是我想要的东西,那还得重新写,网上众说纷纭,有说简单的,有说复杂的,希望大家给我点指示!~再次感谢中... ...

解决方案 »

  1.   

    1想给哪台发都可以 有IP就行 发消息需要知道对方的IP 取消息也要知道要取哪个IP的消息
    2能 
    3有地址才能发送消息
    4.。
    5.。
      

  2.   

    这句IPEndPoint iep = new IPEndPoint(IPAddress.Parse("192.168.1.104"), 8888);在局域网内是不是没有用到还是怎么的,我改成什么是还能在单机传递数据。还是说到内网就会有用了??
      

  3.   

    我写的聊天工具
    http://download.csdn.net/source/1550301
    C/S的,简单的聊天工具当然容易,就是几个字符串传来传去~但是功能慢慢扩展,当然就会越来越复杂~~~