如何在组播组中发送消息(有线到无线)

时间:2013-06-24 07:49:59

标签: c#-4.0 network-programming

我的项目是让两个设备相互通信,一个是笔记本电脑,另一个是桌面,使用c#中的套接字类。笔记本电脑通过无线连接连接到路由器,而桌面连接到LAN或有线连接到同一路由器。

问题是当我让笔记本电脑将数据发送到桌面时,桌面没有收到任何数据。但是,当我尝试通过另一台通过无线连接连接的笔记本电脑发送消息时,第一台笔记本电脑能够接收另一台笔记本电脑发送的数据。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Collections;

namespace gslib
{

public class StateObject
{
    private Socket m_socket;
    private byte[] m_data;
    private StringBuilder m_message;

    public StateObject()
    {
        m_message = new StringBuilder();
    }

    public Socket Socket
    { 
        set { m_socket = value; } 
        get { return m_socket; } 
    }
    public byte[] Data
    { 
        set { m_data = value; } 
        get { return m_data; } 
    }
    public StringBuilder Message
    {
        set { m_message = value; }
        get { return m_message; }
    }
}

public class GroupAgent
{
    private const int BUFFERSIZE = 4096;

    public GroupAgent()
    {
        m_ip = string.Empty;
        m_port = -1;
    }

    public GroupAgent(string ip, int port)
    {
        m_ip = ip;
        this.m_port = port;
    }

    #region decleration

    private string m_ip;
    private int m_port;

    #endregion

    #region events

    public delegate void MessageRecievedEventhandler(string message);
    public event MessageRecievedEventhandler MessageRecieved;

    #endregion

    #region private methods

    private byte[] trimByte(byte[] receive, int x)
    {
        List<byte> temp = new List<byte>();
        for (int i = 0; i < x; i++)
        {
            temp.Add(receive[i]);
        }
        return temp.ToArray();
    }

    private void receive(Socket sock)
    {
        StateObject state = new StateObject();
        state.Socket = sock;
        state.Data = new byte[BUFFERSIZE];

        sock.BeginReceive(state.Data, 0, BUFFERSIZE, SocketFlags.None, new    AsyncCallback(DoReceiveFrom), state);
    }

    private void DoReceiveFrom(IAsyncResult iar)
    {
        StateObject state = (StateObject)iar.AsyncState;   
        try
        {                             
            int count = state.Socket.EndReceive(iar);

            byte[] temp = trimByte(state.Data, count);

            if (MessageRecieved != null)
            {
                MessageRecieved(ASCIIEncoding.ASCII.GetString(temp));
            }

            state.Socket.BeginReceive(state.Data, 0, BUFFERSIZE, SocketFlags.None, new AsyncCallback(DoReceiveFrom), state);
        }
        catch 
        {
            state.Socket.BeginReceive(state.Data, 0, BUFFERSIZE, SocketFlags.None, new AsyncCallback(DoReceiveFrom), state);
        }
    }

    #endregion

    #region public properties

    public string _GroupIP
    {
        set { m_ip = value; }
        get { return m_ip; }
    }

    public int _Port
    {
        set { m_port = value; }
        get { return m_port; }        
    }

    #endregion

    #region public methods

    public void connect()
    {
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
        sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 
ProtocolType.Udp);
        sock.Bind(new IPEndPoint(IPAddress.Any, _Port));

        sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new 
MulticastOption(IPAddress.Parse(_GroupIP), IPAddress.Any));
        sock.SetSocketOption(SocketOptionLevel.IP, 
SocketOptionName.MulticastTimeToLive, 2);

        receive(sock);           
    }

    public void connect(string m_ip, int port)
    {
        _GroupIP = m_ip;
        _Port = port;
        connect();
    }

    public void sendMessage(string message)
    {
        byte[] msg = ASCIIEncoding.ASCII.GetBytes(message);
        Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 
ProtocolType.Udp);

        sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new 
MulticastOption(IPAddress.Parse(_GroupIP)));
        sock.SetSocketOption(SocketOptionLevel.IP, 
SocketOptionName.MulticastTimeToLive, 2);

        IPEndPoint iep = new IPEndPoint(IPAddress.Parse(_GroupIP), _Port);
        sock.Connect(iep);

        sock.Send(msg);

        sock.Close();
    }

    #endregion
}
}

我在这里缺少什么?我必须在路由器上设置任何东西吗?请回复

1 个答案:

答案 0 :(得分:0)

sock.Bind(new IPEndPoint(IPAddress.Any, _Port));

问题出在这里。这导致UDP根据单播IP路由表路由输出数据报,并且如果路由表不喜欢无线路由,则不会使用它。将其绑定到本地wifi接口的IP地址。