public bool CreateServer(IPAddress ip, int listenPort, out string a) { IPEndPoint ipe = new IPEndPoint(ip, listenPort); try { RSAkey = EncryptAndCompress.GenerateRSAKeys(2048); listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp); listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); if ((byte)Environment.OSVersion.Platform >= 0 && (byte)Environment.OSVersion.Platform <= 3) { listener.IOControl((IOControlCode)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null); } listener.Bind(ipe); EndPoint remoteEP = new IPEndPoint(IPAddress.IPv6Any, 0); listener.BeginReceiveFrom(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, ref remoteEP, Receive, listener); StartThread(); a = ListenerIP.ToString() + " 成功建立伺服器"; } catch (SocketException) { RSAkey = new EncryptAndCompress.RSAKeyPair(); a = ipe.ToString() + "無法建立伺服器"; return(false); } return(true); }
public bool CreateServer(IPAddress ip, int listenPort, out string a) { IPEndPoint ipe = new IPEndPoint(ip, listenPort); try { RSAkey = EncryptAndCompress.GenerateRSAKeys(2048); listener = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false); listener.Bind(ipe); listener.Listen(MaxConnections); listener.BeginAccept(ListenClient, listener); a = ipe.ToString() + " 成功建立伺服器"; } catch (SocketException) { RSAkey = new EncryptAndCompress.RSAKeyPair(); a = ipe.ToString() + "無法建立伺服器"; return(false); } return(true); }