当前位置: 首页>>代码示例>>C#>>正文


C# Socket.?.Close方法代码示例

本文整理汇总了C#中Socket.?.Close方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.?.Close方法的具体用法?C# Socket.?.Close怎么用?C# Socket.?.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Socket的用法示例。


在下文中一共展示了Socket.?.Close方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateProxy

        public static bool ValidateProxy(HttpHost p)
        {
            if (_localAddr == null)
            {
                //Logger.Error("cannot get local ip");
                return false;
            }
            bool isReachable = false;
            Socket socket = null;
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);

                socket.Bind(new IPEndPoint(_localAddr, 0));
                IPAddress address = IPAddress.Parse(p.Host);
                socket.SendTimeout = 3000;
                socket.ReceiveTimeout = 3000;
                socket.Connect(address, p.Port);

                //Logger.Info("SUCCESS - connection established! Local: " + _localAddr + " remote: " + p);
                isReachable = true;
            }
            catch (IOException)
            {
                //Logger.Warn("FAILRE - CAN not connect! Local: " + _localAddr + " remote: " + p);
            }
            finally
            {
                    try
                    {
            #if !NET_CORE
                        socket?.Close();
            #else
                        socket?.Dispose();
            #endif
                    }
                    catch (IOException e)
                    {
                        //Logger.Warn("Error occurred while closing socket of validating proxy", e);
                    }
            }
            return isReachable;
        }
开发者ID:yalunwang,项目名称:DotnetSpider,代码行数:43,代码来源:ProxyUtil.cs

示例2: Main

 static void Main()
 {
     try
     {
         listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
         listeningSocket.Bind(localEndPoint);
         listeningSocket.Listen(LISTENERBACKLOG);
         Console.WriteLine("War Arena Master Server started.");
         Console.WriteLine($"Listening on {listeningSocket.LocalEndPoint}");
         while (true)
         {
             var newConnection = listeningSocket.Accept();
             Console.WriteLine($"New connection from {newConnection.RemoteEndPoint}");
             ParameterizedThreadStart pts = new ParameterizedThreadStart(ConnectionHandler);
             Thread thread = new Thread(pts);
             thread.Start(newConnection);
         }
     }
     catch (Exception exception)
     {
         Console.WriteLine(exception.Message);
         Console.ReadLine();
     }
     finally
     {
         listeningSocket?.Close();
     }
 }
开发者ID:No-ops,项目名称:WarArena,代码行数:28,代码来源:WarArenaMasterServer.cs

示例3: AcceptClientAsync

        private async Task AcceptClientAsync(Socket socket)
        {
            await Task.Yield();

            AsyncSocket s = null;
            try
            {
                s = new AsyncSocket(socket);
                _connectedClients.TryAdd(s, true);
                Console.WriteLine($"Client connected: {s.IpEndPoint?.ToString().Replace("::ffff:", "")}");

                while (true)
                {
                    Task<AsyncSocket.ReadResult> receive = s.ReceiveAsync();
                    await receive;
                    ReceiveTcp(receive.Result);
                }
            }
            catch (SocketException)
            {
            }
            finally
            {
                if (s != null)
                {
                    bool b;
                    if (_connectedClients.TryRemove(s, out b))
                    {
                        IPEndPoint ip = s.IpEndPoint;
                        if (ip != null)
                        {
                            Console.WriteLine($"Client: {ip.ToString().Replace("::ffff:", "")} disconnected.");
                            RemoveTarget(ip);
                        }
                    }
                    s.Dispose();
                }
                socket?.Close();
            }
        }
开发者ID:msjaelland,项目名称:BOD-Server,代码行数:40,代码来源:Broker.Transfer.cs

示例4: Open

        //-------------------------------------------------------------------------------------------------------------
        // Operations
        //-------------------------------------------------------------------------------------------------------------

        /**
         * Opens a socket to the indicated device at indicated address and port
         * 
         * @exception   SocketException thrown when the socket parameters are invalid
         * @exception   AdbException    Thrown when an Adb error condition occurs
         *
         * @param   address The address.
         * @param   device  the device to connect to. Can be null in which case the connection will be to the first available
         *                  device.
         * @param   port    The port.
         *
         * @return  A Socket.
         */
        public Socket Open(IPAddress address, Device device, int port)
            {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
                {
                s.Connect(address, port);
                s.Blocking = true;
                s.NoDelay = false;

                SetDevice(s, device?.SerialNumber);

                byte[] req = CreateAdbForwardRequest(null, port);
                Write(s, req);
                AdbResponse resp = ReadAdbResponse(s);
                if (!resp.Okay)
                    {
                    throw new AdbException("connection request rejected");
                    }
                s.Blocking = true;
                }
            catch (Exception)
                {
                s?.Close();
                throw;
                }
            return s;
            }
开发者ID:SwerveRobotics,项目名称:tools,代码行数:44,代码来源:AdbHelper.cs

示例5: GetFrameBuffer

        public RawImage GetFrameBuffer(IPEndPoint adbSockAddr, Device device)
            {
            RawImage imageParams = new RawImage();
            byte[] request = FormAdbRequest("framebuffer:"); //$NON-NLS-1$
            byte[] nudge = { 0 };
            byte[] reply;

            Socket adbChan = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
                {
                adbChan.Connect(adbSockAddr);
                adbChan.Blocking = true;

                // if the device is not -1, then we first tell adb we're looking to talk
                // to a specific device
                SetDevice(adbChan, device?.SerialNumber);
                Write(adbChan, request);

                AdbResponse resp = ReadAdbResponse(adbChan);
                if (!resp.IOSuccess || !resp.Okay)
                    {
                    Log.v(LOGGING_TAG, "Got timeout or unhappy response from ADB fb req: " + resp.Message);
                    adbChan.Close();
                    return null;
                    }

                // first the protocol version.
                reply = new byte[4];
                Read(adbChan, reply);
                BinaryReader buf;
                int version = 0;
                using (MemoryStream ms = new MemoryStream(reply))
                    {
                    buf = new BinaryReader(ms);
                    version = buf.ReadInt16();
                    }

                // get the header size (this is a count of int)
                int headerSize = RawImage.GetHeaderSize(version);
                // read the header
                reply = new byte[headerSize*4];
                Read(adbChan, reply);
                using (MemoryStream ms = new MemoryStream(reply))
                    {
                    buf = new BinaryReader(ms);

                    // fill the RawImage with the header
                    if (imageParams.ReadHeader(version, buf) == false)
                        {
                        Log.v(LOGGING_TAG, "Unsupported protocol: " + version);
                        return null;
                        }
                    }

                Log.d(LOGGING_TAG, "image params: bpp=" + imageParams.Bpp + ", size="
                    + imageParams.Size + ", width=" + imageParams.Width
                    + ", height=" + imageParams.Height);

                Write(adbChan, nudge);

                reply = new byte[imageParams.Size];
                Read(adbChan, reply);
                imageParams.Data = reply;
                }
            finally
                {
                adbChan?.Close();
                }

            return imageParams;
            }
开发者ID:SwerveRobotics,项目名称:tools,代码行数:71,代码来源:AdbHelper.cs

示例6: CreateForward

        public bool CreateForward(IPEndPoint adbSockAddr, Device device, int localPort, int remotePort)
            {
            Socket adbChan = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
                {
                adbChan.Connect(adbSockAddr);
                adbChan.Blocking = true;

                // host-serial should be different based on the transport...
                byte[] request = FormAdbRequest($"host-serial:{device.SerialNumber}:forward:tcp:{localPort};tcp:{remotePort}");

                Write(adbChan, request);

                AdbResponse resp = ReadAdbResponse(adbChan);
                if (!resp.IOSuccess || !resp.Okay)
                    {
                    throw new AdbException("Device rejected command: " + resp.Message);
                    }
                }
            finally
                {
                adbChan?.Close();
                }

            return true;
            }
开发者ID:SwerveRobotics,项目名称:tools,代码行数:26,代码来源:AdbHelper.cs

示例7: CreatePassThroughConnection

        public Socket CreatePassThroughConnection(IPEndPoint endpoint, Device device, int pid)
            {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
                {
                socket.Connect(endpoint);
                socket.NoDelay = true;

                // if the device is not -1, then we first tell adb we're looking to talk to a specific device
                SetDevice(socket, device?.SerialNumber);

                byte[] req = CreateJdwpForwardRequest(pid);

                Write(socket, req);

                AdbResponse resp = ReadAdbResponse(socket);
                if (!resp.Okay)
                    throw new AdbException("connection request rejected: " + resp.Message); //$NON-NLS-1$
                }
            catch (Exception)
                {
                socket?.Close();
                throw;
                }

            return socket;
            }
开发者ID:SwerveRobotics,项目名称:tools,代码行数:27,代码来源:AdbHelper.cs


注:本文中的Socket.?.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。