Пример #1
0
 //New client - Server Accepted
 internal unsafe Socket(ws2_32.AddressFamily family, ws2_32.ProtocolType protocol, ws2_32.SocketType type,
                        IntPtr Handle, ws2_32.sockaddr_in addr, HeaderTypeSize headerSize, uint SendBufferSize, uint ReceiveBufferSize)
     : this(family, protocol, type, Handle)
 {
     this.SendBufferSize    = SendBufferSize;
     this.ReceiveBufferSize = ReceiveBufferSize;
     this.RemoteIp          = Marshal.PtrToStringAnsi(new IntPtr(ws2_32.inet_ntoa(addr.sin_addr)));
     this.Port       = (uint)ws2_32.ntohs(addr.sin_port);
     this.HeaderSize = headerSize;
 }
Пример #2
0
        public virtual bool Connect(string remoteIp, ushort port)
        {
            ws2_32.sockaddr_in addr = new ws2_32.sockaddr_in();
            addr.sin_family = (short)family;
            addr.sin_port   = ws2_32.htons(port);
            addr.sin_addr   = (int)ws2_32.inet_addr(remoteIp);

            int ret = ws2_32.connect(this.Handle, ref addr, Marshal.SizeOf(addr));

            if (ret == 0)
            {
                Connected = true;
            }
            this.RemoteIp = remoteIp;
            return(ret == 0);
        }
Пример #3
0
        /// <summary>This function returns the accepted connection that has been made by a client</summary>
        /// <returns>The accepted connection</returns>
        public virtual Socket Accept()
        {
            if (this.Handle.ToInt32() == 0)
            {
                throw new Exception("The socket handle was invalid");
            }

            ws2_32.sockaddr_in addr = new ws2_32.sockaddr_in();
            int dwSize = Marshal.SizeOf(addr);

            IntPtr CHandle = ws2_32.accept(this.Handle, ref addr, ref dwSize);
            Socket sock    = new Socket(family, protocol, type, CHandle, addr, this.HeaderSize, this.SendBufferSize, this.ReceiveBufferSize);

            sock.Connected        = true;
            sock.EnableSizeHeader = this.EnableSizeHeader;
            return(sock);
        }
Пример #4
0
        /// <summary> </summary>
        /// <param name="IpAddress">Default: 0.0.0.0</param>
        /// <param name="port"></param>
        public virtual bool Bind(string IpAddress, ushort port)
        {
            ws2_32.sockaddr_in addr = new ws2_32.sockaddr_in();
            addr.sin_family = (short)family;
            addr.sin_port   = ws2_32.htons(port);
            addr.sin_addr   = (int)ws2_32.inet_addr(IpAddress);

            int result = ws2_32.bind(this.Handle, ref addr, Marshal.SizeOf(addr));

            if (result == 0)
            {
                this.Port = port;
                this.AddrRemoteAddress = addr;
                return(true);
            }
            else
            {
                throw new Exception("Unable to bind at port: " + port);
            }
        }