- Home /
Question by
AkshayVats · Mar 05, 2015 at 11:02 AM ·
multiplayerandroid buildudp
Finding running servers on LAN
I am making a multiplayer game playable on local wifi. I want this game to find and list the running servers from local network. I read this and I am using following code to create multicast group.
isMulticastEnabled = true;
udp_client = new UdpClient();
udp_client.ExclusiveAddressUse = false;
udp_client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udp_client.ExclusiveAddressUse = false;
udp_client.Client.Bind(localEp);
udp_client.JoinMulticastGroup(multicastaddress);
Thread thread = new Thread(() =>
{
while (isMulticastEnabled)
{
var data = udp_client.Receive(ref localEp);
using (var ms = new System.IO.MemoryStream(data))
{
BinaryFormatter bf = new BinaryFormatter();
var msg = bf.Deserialize(ms) as Message;
if (msg.arg1 != Administrator.Inst.User.pid)
{
RunOnUI(() => { Administrator.Inst.ProcessMessage(msg); });
}
}
}
});
thread.Start();
Sending function goes like this -
public void MulticastMsg(Message msg)
{
byte[] data;
using (var ms = new System.IO.MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, msg);
data = ms.ToArray();
}
udp_client.Send(data, data.Length, remoteep);
}
Works fine on PC to PC but android devices are unable to receive multicast messages.Some answers suggest that android only processes packets directed to its address. I am stuck here and I'll appreciate any help. If there is a unity way of doing this that I am unaware of, please do suggest me that.
Comment
Your answer