我的项目是让两台设备相互通信,一台是笔记本电脑,一台是台式机,使用c#中的Sockets类。笔记本电脑通过无线连接连接到路由器,而台式机连接到 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
}
}
我在这里缺少什么?我必须在路由器上设置什么吗?请回复