在同一台机运行两个实例,接收正常。
但是在局域网里边就不行了,所谓的局域网就是一台家用路由,连着几台电脑。
在win7与win7(局域网内的另一台电脑),win7与xp(同一台电脑,装在vm里,桥接上网),都接收不了。
怎么才能局域网里接收呀?class multiCast //发送
{
private Socket sock;
private IPAddress mcIP;
private int mcPort;
private IPEndPoint ipep;
private int ttl = 10; private Mutex mut; private string searchString;
public string SeartchString
{
set
{
mut.WaitOne();
searchString = value;
mut.ReleaseMutex();
}
get
{
return searchString;
}
}
public multiCast()
{
mut = new Mutex();
} public multiCast(string IP, string port):this()
{ searchString = "";
mcIP = IPAddress.Parse(IP);
mcPort = Int32.Parse(port); ipep = new IPEndPoint(mcIP, mcPort); sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl); sock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress, 1);
} public void Send()
{
if (searchString == "")
{
return;
}
System.Text.UnicodeEncoding encode = new UnicodeEncoding();
byte[] inputToBeSent = encode.GetBytes(searchString);
sock.SendTo(inputToBeSent, 0, inputToBeSent.Length, SocketFlags.None, ipep); SeartchString = "";
} public void CloseSocket()
{
sock.Close();
}
}
class multiCastRecv
{
private Socket sock;
private IPAddress mcIP;
private int mcPort;
private IPEndPoint receivePoint;
private EndPoint tempReceivePoint;
private List<string> recvStrings; public string [] RecvStrings
{
get
{
string[] strs = new string[recvStrings.Count];
recvStrings.CopyTo(strs);
return strs;
}
} private Mutex mut; public void OpRecvStrs(string op,string recvStr)
{
mut.WaitOne();
if (op == "clear")
{
recvStrings.Clear();
}
else if (op == "add")
{
recvStrings.Add(recvStr);
} mut.ReleaseMutex();
} public multiCastRecv(string IP, string Port)
{
recvStrings = new List<string>(); mut = new Mutex(); mcIP = IPAddress.Parse(IP);
mcPort = Int32.Parse(
Port); sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, mcPort); sock.Bind(ipep); sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(mcIP, IPAddress.Any)); receivePoint = new IPEndPoint(IPAddress.Any, 0);
tempReceivePoint = (EndPoint)receivePoint;
} public void Recieve()
{
while (true)
{
try
{ byte[] recvData = new byte[512];
int length = sock.ReceiveFrom(recvData, 0, 512, SocketFlags.None, ref tempReceivePoint);
System.Text.UnicodeEncoding encode = new UnicodeEncoding();
OpRecvStrs("add", encode.GetString(recvData, 0, length));
}
catch
{
}
}
} public void CloseSocket()
{
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.DropMembership,
new MulticastOption(mcIP, IPAddress.Any)); sock.Close();
}
}
但是在局域网里边就不行了,所谓的局域网就是一台家用路由,连着几台电脑。
在win7与win7(局域网内的另一台电脑),win7与xp(同一台电脑,装在vm里,桥接上网),都接收不了。
怎么才能局域网里接收呀?class multiCast //发送
{
private Socket sock;
private IPAddress mcIP;
private int mcPort;
private IPEndPoint ipep;
private int ttl = 10; private Mutex mut; private string searchString;
public string SeartchString
{
set
{
mut.WaitOne();
searchString = value;
mut.ReleaseMutex();
}
get
{
return searchString;
}
}
public multiCast()
{
mut = new Mutex();
} public multiCast(string IP, string port):this()
{ searchString = "";
mcIP = IPAddress.Parse(IP);
mcPort = Int32.Parse(port); ipep = new IPEndPoint(mcIP, mcPort); sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl); sock.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress, 1);
} public void Send()
{
if (searchString == "")
{
return;
}
System.Text.UnicodeEncoding encode = new UnicodeEncoding();
byte[] inputToBeSent = encode.GetBytes(searchString);
sock.SendTo(inputToBeSent, 0, inputToBeSent.Length, SocketFlags.None, ipep); SeartchString = "";
} public void CloseSocket()
{
sock.Close();
}
}
class multiCastRecv
{
private Socket sock;
private IPAddress mcIP;
private int mcPort;
private IPEndPoint receivePoint;
private EndPoint tempReceivePoint;
private List<string> recvStrings; public string [] RecvStrings
{
get
{
string[] strs = new string[recvStrings.Count];
recvStrings.CopyTo(strs);
return strs;
}
} private Mutex mut; public void OpRecvStrs(string op,string recvStr)
{
mut.WaitOne();
if (op == "clear")
{
recvStrings.Clear();
}
else if (op == "add")
{
recvStrings.Add(recvStr);
} mut.ReleaseMutex();
} public multiCastRecv(string IP, string Port)
{
recvStrings = new List<string>(); mut = new Mutex(); mcIP = IPAddress.Parse(IP);
mcPort = Int32.Parse(
Port); sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReuseAddress, 1); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, mcPort); sock.Bind(ipep); sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(mcIP, IPAddress.Any)); receivePoint = new IPEndPoint(IPAddress.Any, 0);
tempReceivePoint = (EndPoint)receivePoint;
} public void Recieve()
{
while (true)
{
try
{ byte[] recvData = new byte[512];
int length = sock.ReceiveFrom(recvData, 0, 512, SocketFlags.None, ref tempReceivePoint);
System.Text.UnicodeEncoding encode = new UnicodeEncoding();
OpRecvStrs("add", encode.GetString(recvData, 0, length));
}
catch
{
}
}
} public void CloseSocket()
{
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.DropMembership,
new MulticastOption(mcIP, IPAddress.Any)); sock.Close();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货