// Set address to listen on. return the used port public virtual void SetAddress(String addr) { m_address.Resolve(addr, m_options.IPv4Only); m_endpoint = m_address.ToString(); try { m_handle = AsyncSocket.Create(m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); if (!m_options.IPv4Only && m_address.Address.AddressFamily == AddressFamily.InterNetworkV6) { try { // This is not supported on old windows operation system and might throw exception m_handle.SetSocketOption(SocketOptionLevel.IPv6, IPv6Only, 0); } catch { } } m_handle.ExclusiveAddressUse = false; m_handle.Bind(m_address.Address); m_handle.Listen(m_options.Backlog); m_socket.EventListening(m_endpoint, m_handle); m_port = m_handle.LocalEndPoint.Port; } catch (SocketException ex) { Close(); throw NetMQException.Create(ex); } }
// Internal function to start the actual connection establishment. private void StartConnecting() { Debug.Assert(m_s == null); // Create the socket. try { m_s = AsyncSocket.Create(m_addr.Resolved.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (SocketException) { AddReconnectTimer(); return; } m_ioObject.AddSocket(m_s); m_handleValid = true; // Connect to the remote peer. try { m_s.Connect(m_addr.Resolved.Address.Address, m_addr.Resolved.Address.Port); m_socket.EventConnectDelayed(m_endpoint, ErrorCode.InProgres); } catch (SocketException ex) { OutCompleted(ex.SocketErrorCode, 0); } }
private void Accept() { m_acceptedSocket = AsyncSocket.Create(m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // start accepting socket async m_handle.Accept(m_acceptedSocket); }
/// <summary> /// Internal function to start the actual connection establishment. /// </summary> private void StartConnecting() { Debug.Assert(m_s == null); // Create the socket. try { m_s = AsyncSocket.Create(m_addr.Resolved.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); } catch (SocketException) { AddReconnectTimer(); return; } m_ioObject.AddSocket(m_s); m_handleValid = true; // Connect to the remote peer. try { m_s.Connect(m_addr.Resolved.Address.Address, m_addr.Resolved.Address.Port); m_socket.EventConnectDelayed(m_endpoint, ErrorCode.InProgress); } catch (SocketException ex) { OutCompleted(ex.SocketErrorCode, 0); } // TerminatingException can occur in above call to EventConnectDelayed via // MonitorEvent.Write if corresponding PairSocket has been sent Term command catch (TerminatingException) {} }
private void Accept() { m_acceptedSocket = AsyncSocket.Create(m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // start accepting socket async m_handle.Accept(m_acceptedSocket); // Disable TIME_WAIT tcp state if (m_options.DisableTimeWait) { m_handle.LingerState = new LingerOption(true, 0); } }
/// <summary> /// Set address to listen on. /// </summary> /// <param name="addr">a string denoting the address to set this to</param> public virtual void SetAddress(string addr) { m_address.Resolve(addr, m_options.IPv4Only); Assumes.NotNull(m_address.Address); try { m_handle = AsyncSocket.Create(m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Assumes.NotNull(m_handle); if (!m_options.IPv4Only && m_address.Address.AddressFamily == AddressFamily.InterNetworkV6) { try { // This is not supported on old windows operating systems and might throw exception m_handle.SetSocketOption(SocketOptionLevel.IPv6, IPv6Only, 0); } catch { } } #if NETSTANDARD2_0 || NETSTANDARD2_1 // This command is failing on linux if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { m_handle.ExclusiveAddressUse = false; } #else m_handle.ExclusiveAddressUse = false; #endif m_handle.Bind(m_address.Address); m_handle.Listen(m_options.Backlog); // Copy the port number after binding in case we requested a system-allocated port number (TCP port zero) m_address.Address.Port = m_handle.LocalEndPoint.Port; m_endpoint = m_address.ToString(); m_socket.EventListening(m_endpoint, m_handle); m_port = m_handle.LocalEndPoint.Port; } catch (SocketException ex) { Close(); throw NetMQException.Create(ex); } }
/// <summary> /// Set address to listen on. /// </summary> /// <param name="addr">a string denoting the address to set this to</param> public virtual void SetAddress([NotNull] string addr) { this.m_address.Resolve(addr, this.m_options.IPv4Only); try { this.m_handle = AsyncSocket.Create(this.m_address.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Debug.Assert(this.m_handle != null); if (!this.m_options.IPv4Only && this.m_address.Address.AddressFamily == AddressFamily.InterNetworkV6) { try { // This is not supported on old windows operation system and might throw exception this.m_handle.SetSocketOption(SocketOptionLevel.IPv6, TcpListener.IPv6Only, 0); } catch { } } #if NETSTANDARD1_3 // This command is failing on linux if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { m_handle.ExclusiveAddressUse = false; } #else this.m_handle.ExclusiveAddressUse = false; #endif this.m_handle.Bind(this.m_address.Address); this.m_handle.Listen(this.m_options.Backlog); // Copy the port number after binding in case we requested a system-allocated port number (TCP port zero) this.m_address.Address.Port = this.m_handle.LocalEndPoint.Port; this.m_endpoint = this.m_address.ToString(); this.m_socket.EventListening(this.m_endpoint, this.m_handle); this.m_port = this.m_handle.LocalEndPoint.Port; } catch (SocketException ex) { this.Close(); throw NetMQException.Create(ex); } }
/// <summary> /// Perform initialization of this PgmSocket, including creating the socket handle. /// </summary> internal void Init() { #if DEBUG // Don't want to bloat the code with excessive debugging information, unless this is a DEBUG build. jh try { #endif this.Handle = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PgmSocket.PgmProtocolType); #if DEBUG } catch (SocketException x) { string xMsg = $"SocketException with SocketErrorCode={x.SocketErrorCode}, Message={x.Message}, in PgmSocket.Init, within AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PGM_PROTOCOL_TYPE), {this}"; Debug.WriteLine(xMsg); // If running on Microsoft Windows, suggest to the developer that he may need to install MSMQ in order to get PGM socket support. #if NETSTANDARD1_3 bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); #else PlatformID p = Environment.OSVersion.Platform; bool isWindows = true; switch (p) { case PlatformID.Win32NT: break; case PlatformID.Win32S: break; case PlatformID.Win32Windows: break; default: isWindows = false; break; } #endif if (isWindows) { Debug.WriteLine("For Microsoft Windows, you may want to check to see whether you have installed MSMQ on this host, to get PGM socket support."); } throw new FaultException(x, xMsg); } #endif this.Handle.ExclusiveAddressUse = false; this.Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); }
public void KeepAlive() { var socket = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); tcp_keepalive tcpKeepalive = new tcp_keepalive(); tcpKeepalive.onoff = 1; tcpKeepalive.keepaliveinterval = 1000; tcpKeepalive.keepalivetime = 1000; int size = Marshal.SizeOf(tcpKeepalive); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(tcpKeepalive, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); socket.IOControl(IOControlCode.KeepAliveValues, (byte[])arr, null); }
static void Main(string[] args) { ForceDotNet.Force(); CompletionPort completionPort = CompletionPort.Create(); AutoResetEvent listenerEvent = new AutoResetEvent(false); AutoResetEvent clientEvent = new AutoResetEvent(false); AutoResetEvent serverEvent = new AutoResetEvent(false); AsyncSocket listener = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(listener, listenerEvent); AsyncSocket server = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(server, serverEvent); AsyncSocket client = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(client, clientEvent); Task.Factory.StartNew(() => { CompletionStatus [] completionStatuses = new CompletionStatus[10]; int removed; while (true) { var result = completionPort.GetMultipleQueuedCompletionStatus(-1, completionStatuses, out removed); for (int i = 0; i < removed; i++) { var completionStatus = completionStatuses[i]; Console.WriteLine("{0} {1} {2}", completionStatus.SocketError, completionStatus.OperationType, completionStatus.BytesTransferred); if (completionStatus.State != null) { AutoResetEvent resetEvent = (AutoResetEvent)completionStatus.State; resetEvent.Set(); } } Console.WriteLine("Handled {0} statuses", removed); Thread.Sleep(100); } }); listener.Bind(IPAddress.Any, 5555); listener.Listen(1); //Console.WriteLine(listener.LocalEndPoint); client.Bind(IPAddress.Any, 0); client.Connect("localhost", 5555); ////Thread.Sleep(100); listener.Accept(server); listenerEvent.WaitOne(); clientEvent.WaitOne(); byte[] sendBuffer = new byte[1] { 2 }; byte[] recvBuffer = new byte[1]; server.Receive(recvBuffer); client.Send(sendBuffer); clientEvent.WaitOne(); serverEvent.WaitOne(); ////Console.WriteLine("server received"); Console.ReadLine(); }
internal void InitReceiver() { Handle = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PgmProtocolType); }
internal void InitReceiver() { Handle = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PGM_PROTOCOL_TYPE); }
internal void Init() { Handle = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Rdm, PGM_PROTOCOL_TYPE); Handle.ExclusiveAddressUse = false; Handle.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); }
public static void Main(string[] args) { CompletionPort completionPort = CompletionPort.Create(); AutoResetEvent listenerEvent = new AutoResetEvent(false); AutoResetEvent clientEvent = new AutoResetEvent(false); AutoResetEvent serverEvent = new AutoResetEvent(false); AsyncSocket listener = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(listener, listenerEvent); AsyncSocket server = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(server, serverEvent); AsyncSocket client = AsyncSocket.Create(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); completionPort.AssociateSocket(client, clientEvent); Task.Factory.StartNew(() => { CompletionStatus completionStatus; while (true) { var result = completionPort.GetQueuedCompletionStatus(-1, out completionStatus); if (result) { Console.WriteLine("{0} {1} {2}", completionStatus.SocketError, completionStatus.OperationType, completionStatus.BytesTransferred); if (completionStatus.State != null) { AutoResetEvent resetEvent = (AutoResetEvent)completionStatus.State; resetEvent.Set(); } } } }); listener.Bind(IPAddress.Any, 5555); listener.Listen(1); client.Connect("localhost", 5555); listener.Accept(server); listenerEvent.WaitOne(); clientEvent.WaitOne(); byte[] sendBuffer = new byte[1] { 2 }; byte[] recvBuffer = new byte[1]; client.Send(sendBuffer); server.Receive(recvBuffer); clientEvent.WaitOne(); serverEvent.WaitOne(); server.Dispose(); client.Dispose(); Console.ReadLine(); }