看一下System.Net.Sockets.UdpClient的内容,发送、接收。
广播,只需要向广播地址发送消息就可以了。
如果不知道广播地址,请翻一下TCP/IP的书。

解决方案 »

  1.   

    可以给一个server和client端的例程吗?测试通过立刻给分
      

  2.   

    //服务器端
    public void HandleThread()
    {
    Socket ClientSocket=new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.11.110");
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint ipe = new IPEndPoint(ipAddress,2300);

    System.Net.Sockets.TcpListener ClientSocket = new System.Net.Sockets.TcpListener(ipAddress,2300);
    ClientSocket.Start();
    System.Net.Sockets.TcpClient mySocket;
    while (true)
    {
    Application.DoEvents();//同时接受其他消息

    mySocket=ClientSocket.AcceptTcpClient();

    try
    {
    ReviceMsg(mySocket);
    mySocket.Close();
    }
    catch(Exception Err)
    {
    mySocket.Close();
    System.Windows.Forms.MessageBox.Show(Err.ToString()); }
    }
    }
    //接受消息
    private void ReviceMsg(System.Net.Sockets.TcpClient mySocket)
    { byte[] bytes = new byte[2048];
    string Str=null;
    System.Text.Encoding encoding =System.Text.Encoding.Unicode; System.Net.Sockets.NetworkStream ns = mySocket.GetStream();//Receive(bytes,bytes.Length,0);
    ns.Read(bytes,0,2048);
    //int i = stream.Read(bytes,0,1024); 
    string Rstr=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length);
    //ASCII.GetString(RecvBytes, 0, bytes);

    txtSocket.Text = Rstr + "server"; //接受新连接发送在线用户列表到连接Socket
    //mySocket.Send(txtdst.Text);
    byte[] bString = System.Text.Encoding.Unicode.GetBytes(txtSocket.Text);
    ns.Write(bString,0,bString.Length);

    return;
    }
    //客户端
    IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.11.110");
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    IPEndPoint ipe = new IPEndPoint(ipAddress,2300);
    TcpClient mySocket = new TcpClient();
    mySocket.Connect(ipe);
    System.Net.Sockets.NetworkStream ns = mySocket.GetStream();
    byte[] bytes = new byte[2048];
    ns.Read(bytes,0,2048);
    string Rstr=System.Text.Encoding.Unicode.GetString(bytes,0,bytes.Length);
    txtsrc.Text = Rstr+"client";
    byte[] bString = System.Text.Encoding.Unicode.GetBytes(txtSocket.Text);
    ns.Write(bString,0,bString.Length);
    mySocket.Close();
      

  3.   

    to:veaven(风林火山) 
    你贴的代码好象有问题.
    1是重复定义了ClientSocket,
    2没有txtsocket的定义...
      

  4.   

    这是TCP/IP网络编程的必修例子啊!!
    很多书上都有例子!可以参考!!