private bool TryToConnectSocket() { bool hasSucceeded = false; try { multicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); endPoint = new IPEndPoint(IPAddress.Any, discoveryUri.Port); //We have to allow reuse in the multicast socket. Otherwise, we would be unable to use multiple clients on the same machine. multicastSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); multicastSocket.Bind(endPoint); IPAddress ipaddress; if (!TcpTransportFactory.TryParseIPAddress(discoveryUri.Host, out ipaddress)) { ipaddress = TcpTransportFactory.GetIPAddress(discoveryUri.Host, AddressFamily.InterNetwork); if (null == ipaddress) { throw new NMSConnectionException("Invalid host address."); } } multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipaddress, IPAddress.Any)); #if !NETCF multicastSocket.ReceiveTimeout = SOCKET_TIMEOUT_MILLISECONDS; #endif hasSucceeded = true; } catch (SocketException) { } return(hasSucceeded); }
private bool TryToConnectSocket(string targetHost, int targetPort) { bool hasSucceeded = false; try { multicastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); endPoint = new IPEndPoint(IPAddress.Any, targetPort); // We have to allow reuse in the multicast socket. Otherwise, we would be unable to // use multiple clients on the same machine. multicastSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); multicastSocket.Bind(endPoint); IPAddress ipaddress; if (!TcpTransportFactory.TryParseIPAddress(targetHost, out ipaddress)) { ipaddress = TcpTransportFactory.GetIPAddress(targetHost, AddressFamily.InterNetwork); if (null == ipaddress) { throw new NMSConnectionException("Invalid host address."); } } if (LoopBackMode) { multicastSocket.MulticastLoopback = true; } if (TimeToLive != 0) { multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, timeToLive); } if (!String.IsNullOrEmpty(mcJoinNetworkInterface)) { // TODO figure out how to set this. throw new NotSupportedException("McJoinNetworkInterface not yet implemented."); } else { multicastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ipaddress, IPAddress.Any)); } if (!String.IsNullOrEmpty(mcNetworkInterface)) { // TODO figure out how to set this. throw new NotSupportedException("McNetworkInterface not yet implemented."); } multicastSocket.ReceiveTimeout = (int)keepAliveInterval; hasSucceeded = true; } catch (SocketException) { } return(hasSucceeded); }