public bool Start (int port)
{
Stop();
mPort = port;
mSocket
= new Socket
(AddressFamily
.InterNetwork, SocketType
.Dgram, ProtocolType
.Udp);// Use the default network interface if one wasn't explicitly chosen
#if (UNITY_IPHONE && !UNITY_EDITOR) || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
IPAddress networkInterface = useMulticasting ? multicastIP : (defaultNetworkInterface ?? IPAddress.Any);
#else
IPAddress networkInterface = defaultNetworkInterface ?? IPAddress.Any;
#endif
#if !UNITY_WEBPLAYER
// Web player doesn't seem to support broadcasts
mSocket.MulticastLoopback = true;
mMulticast = useMulticasting;
if (useMulticasting)
{
MulticastOption opt
= new MulticastOption
(multicastIP, networkInterface
); mSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, opt);
}
else mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
#endif
// Port zero means we will be able to send, but not receive
if (mPort == 0) return true;
try
{
mEndPoint
= new IPEndPoint
(networkInterface,
0); mDefaultEndPoint
= new IPEndPoint
(networkInterface,
0);
// Bind the socket to the specific network interface and start listening for incoming packets
mSocket
.Bind(new IPEndPoint
(networkInterface, mPort
)); mSocket.BeginReceiveFrom(mTemp, 0, mTemp.Length, SocketFlags.None, ref mEndPoint, OnReceive, null);
}