Ejemplo n.º 1
0
        public TcpConnection Accept()
        {
            IntPtr accepted = SocketImports.accept(_handle, IntPtr.Zero, 0);

            if (accepted == new IntPtr(-1))
            {
                var error = SocketImports.WSAGetLastError();
                SocketImports.WSACleanup();
                throw new Exception(String.Format("listen failed with {0}", error));
            }
            return(new TcpConnection(accepted));
        }
Ejemplo n.º 2
0
        public int Send(byte[] bytes, int count)
        {
            unsafe
            {
                fixed(byte *buffer = bytes)
                {
                    IntPtr ptr       = new IntPtr(buffer);
                    int    bytesSent = SocketImports.send(_handle, ptr, count, 0);

                    if (bytesSent == SocketImports.SOCKET_ERROR)
                    {
                        var error = SocketImports.WSAGetLastError();
                        throw new Exception(String.Format("send failed with {0}", error));
                    }
                    return(bytesSent);
                }
            }
        }
Ejemplo n.º 3
0
        public int Receive(byte[] bytes)
        {
            unsafe
            {
                fixed(byte *buffer = bytes)
                {
                    IntPtr ptr           = new IntPtr(buffer);
                    int    bytesReceived = SocketImports.recv(Handle, ptr, bytes.Length, 0);

                    if (bytesReceived < 0)
                    {
                        var error = SocketImports.WSAGetLastError();
                        throw new Exception(String.Format("receive failed with {0}", error));
                    }
                    return(bytesReceived);
                }
            }
        }
Ejemplo n.º 4
0
        public TcpServer(ushort port, byte address1, byte address2, byte address3, byte address4)
        {
            var    version = new TcpServer.Version(2, 2);
            WSDATA data;
            int    result = SocketImports.WSAStartup((short)version.Raw, out data);

            if (result != 0)
            {
                var error = SocketImports.WSAGetLastError();
                throw new Exception(String.Format("ERROR: WSAStartup returned {0}", error));
            }

            _handle = SocketImports.socket(ADDRESS_FAMILIES.AF_INET, SOCKET_TYPE.SOCK_STREAM, PROTOCOL.IPPROTO_TCP);
            if (_handle == IntPtr.Zero)
            {
                var error = SocketImports.WSAGetLastError();
                SocketImports.WSACleanup();
                throw new Exception(String.Format("ERROR: socket returned {0}", error));
            }

            Start(port, address1, address2, address3, address4);
        }
Ejemplo n.º 5
0
        private void Start(ushort port, byte address1, byte address2, byte address3, byte address4)
        {
            // BIND
            in_addr inAddress = new in_addr();

            inAddress.s_b1 = address1;
            inAddress.s_b2 = address2;
            inAddress.s_b3 = address3;
            inAddress.s_b4 = address4;

            sockaddr_in sa = new sockaddr_in();

            sa.sin_family = ADDRESS_FAMILIES.AF_INET;
            sa.sin_port   = SocketImports.htons(port);
            sa.sin_addr   = inAddress;

            int result;

            unsafe
            {
                var size = sizeof(sockaddr_in);
                result = SocketImports.bind(_handle, ref sa, size);
            }
            if (result == SocketImports.SOCKET_ERROR)
            {
                SocketImports.WSACleanup();
                throw new Exception("bind failed");
            }

            // LISTEN
            result = SocketImports.listen(_handle, 10);
            if (result == SocketImports.SOCKET_ERROR)
            {
                SocketImports.WSACleanup();
                throw new Exception("listen failed");
            }
        }
Ejemplo n.º 6
0
 public void Stop()
 {
     SocketImports.WSACleanup();
 }
Ejemplo n.º 7
0
 public void Close()
 {
     SocketImports.closesocket(_handle);
 }