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


C# Socket.GetSocketOption方法代码示例

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


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

示例1: SetSocketOption

 protected static int SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, int value)
 {
     if (((int)socket.GetSocketOption(level, name)) == value)
         return value;
     socket.SetSocketOption(level, name, value);
     return (int)socket.GetSocketOption(level, name);
 }
开发者ID:ByteSempai,项目名称:Ubiquitous,代码行数:7,代码来源:NetworkUtils.cs

示例2: Connect

        public void Connect(EndPoint endPoint)
        {
            byte[] data = new byte[1024];
            string input, stringData;
            int recv;
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            int sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
            Console.WriteLine("Default timeout: {0}", sockopt);
            server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
            sockopt = (int)server.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
            Console.WriteLine("New timeout: {0}", sockopt);

            string welcome = "Hello, are you there?";
            data = Encoding.ASCII.GetBytes(welcome);
            server.SendTo(data, data.Length, SocketFlags.None, endPoint);

            data = new byte[1024];
            try
            {
                Console.WriteLine("Waiting from {0}:", endPoint.ToString());
                recv = server.ReceiveFrom(data, ref endPoint);
                Console.WriteLine("Message received from {0}:", endPoint.ToString());
                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.HostUnreachable)
                    throw new Exception("CLOSED");
                throw new Exception("TIME_OUT");
            }
            Console.WriteLine("Stopping client");
            server.Close();
        }
开发者ID:rqx110,项目名称:NScanner,代码行数:33,代码来源:UdpConnectCall.cs

示例3: SetSocketOption

		protected static bool SetSocketOption(Socket socket, SocketOptionLevel level, SocketOptionName name, bool value) {
			if (((int)socket.GetSocketOption(level, name)) == (value ? 1 : 0))
				return value;
			socket.SetSocketOption(level, name, value);
			if (((int)socket.GetSocketOption(level, name)) != 1) {
				return false;
			}
			return true;
		}
开发者ID:GodLesZ,项目名称:svn-dump,代码行数:9,代码来源:NetworkUtils.cs

示例4: setup

        public void setup(Socket listeningSocket)
        {
            /*Pre : client requests a video stream from a server
             *Post: the setup process is begun*/
            try
            {
                //gets dimensions of video player
                dimensions = (int[]) referenceToView.Invoke(referenceToView.getVideoDimensions);
                //creates new IPEndPoint
                IPEndPoint temp = new IPEndPoint((listeningSocket.RemoteEndPoint as IPEndPoint).Address, 6666);
                //stores endpoint
                listenTo = temp as EndPoint;
                //creates UDP socket
                streamSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                //bind it to any IP address on port: 6666
                streamSocket.Bind(bindEP);
                //probably not neccessary
                streamSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, BitConverter.GetBytes(10000));

                this.beginStreaming();
            }
            catch (SocketException se)
            {

            }
        }
开发者ID:PresCoke,项目名称:Network-Video-Player,代码行数:26,代码来源:RTP_Protocol.cs

示例5: PeerCred

		public PeerCred (Socket sock) {
			if (sock.AddressFamily != AddressFamily.Unix) {
				throw new ArgumentException ("Only Unix sockets are supported", "sock");
			}

			data = (PeerCredData)sock.GetSocketOption (SocketOptionLevel.Socket, (SocketOptionName)so_peercred);
		}
开发者ID:sushihangover,项目名称:playscript,代码行数:7,代码来源:PeerCred.cs

示例6: GetSendWindow

 public unsafe _RM_SEND_WINDOW GetSendWindow(Socket socket)
 {
     int size = sizeof(_RM_SEND_WINDOW);
     byte[] data = socket.GetSocketOption(PgmSocket.PGM_LEVEL, (SocketOptionName)1001, size);
     fixed (byte* pBytes = &data[0])
     {
         return *((_RM_SEND_WINDOW*)pBytes);
     }
 }
开发者ID:jbouzaglou,项目名称:PetPigeonsESB,代码行数:9,代码来源:PlayTests.cs

示例7: GetReceiverStats

 public unsafe _RM_RECEIVER_STATS GetReceiverStats(Socket socket)
 {
     int size = sizeof(_RM_RECEIVER_STATS);
     byte[] data = socket.GetSocketOption(PGM_LEVEL, (SocketOptionName)1013, size);
     fixed (byte* pBytes = &data[0])
     {
         return *((_RM_RECEIVER_STATS*)pBytes);
     }
 }
开发者ID:jbouzaglou,项目名称:PetPigeonsESB,代码行数:9,代码来源:PgmSocket.cs

示例8: NetworkStream

        //
        // Summary:
        //     Initializes a new instance of the System.Net.Sockets.NetworkStream class
        //     for the specified System.Net.Sockets.Socket with the specified System.Net.Sockets.Socket
        //     ownership.
        //
        // Parameters:
        //   ownsSocket:
        //     true to indicate that the System.Net.Sockets.NetworkStream will take ownership
        //     of the System.Net.Sockets.Socket; otherwise, false.
        //
        //   socket:
        //     The System.Net.Sockets.Socket that the System.Net.Sockets.NetworkStream will
        //     use to send and receive data.
        //
        // Exceptions:
        //   System.IO.IOException:
        //     socket is not connected.-or- The value of the System.Net.Sockets.Socket.SocketType
        //     property of socket is not System.Net.Sockets.SocketType.Stream.-or- socket
        //     is in a nonblocking state.
        //
        //   System.ArgumentNullException:
        //     socket is null.
        public NetworkStream(Socket socket, bool ownsSocket)
        {
            if (socket == null) throw new ArgumentNullException();
            
            // This should throw a SocketException if not connected
            try
            {
                _remoteEndPoint = socket.RemoteEndPoint;
            }
            catch (Exception e)
            {
                int errCode = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);

                throw new IOException(errCode.ToString(), e);
            }
            
            // Set the internal socket
            _socket = socket;

            _socketType = (int)_socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Type);

            _ownsSocket = ownsSocket;
        }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:46,代码来源:NetworkStream.cs

示例9: GetSocketOption

		[Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
		public void GetSocketOption3_Socket_Closed ()
		{
			Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			s.Close ();
			try {
				s.GetSocketOption (0, 0, 0);
				Assert.Fail ("#1");
			} catch (ObjectDisposedException ex) {
				// Cannot access a disposed object
				Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
			}
		}
开发者ID:Therzok,项目名称:mono,代码行数:16,代码来源:SocketTest.cs

示例10: DiscoverDevices

		/// <summary>
		/// Obtains information about available devices using a socket.
		/// </summary>
		/// <param name="maxDevices">The maximum number of devices to get information about.</param>
		/// <param name="irdaSocket"></param>
		/// <returns></returns>
		public static IrDADeviceInfo[] DiscoverDevices(int maxDevices, Socket irdaSocket)
		{
            if (irdaSocket == null) {
                throw new ArgumentNullException("irdaSocket");
            }
            const int MaxItemsInHugeBuffer = (Int32.MaxValue - 4) / 32;
            if (maxDevices > MaxItemsInHugeBuffer || maxDevices < 0) {
                throw new ArgumentOutOfRangeException("maxDevices");
            }
            //
			byte[] buffer = irdaSocket.GetSocketOption(IrDASocketOptionLevel.IrLmp, IrDASocketOptionName.EnumDevice, 4 + (maxDevices * 32));
            return ParseDeviceList(buffer);
		}
开发者ID:intille,项目名称:mitessoftware,代码行数:19,代码来源:IrDAClient.cs

示例11: MCCommReceive

        void MCCommReceive()
        {
            var receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            receiveSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, false);
            receiveSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            //    receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, false);
            IPEndPoint localEP = new IPEndPoint(IPAddress.Any, multicastPort);
            receiveSocket.Bind(localEP);
            var rbuf = receiveSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);

            byte[] multicastAddrBytes = multicastAddr.GetAddressBytes();
            byte[] ipAddrBytes = IPAddress.Any.GetAddressBytes();
            byte[] multicastOpt = new byte[]
            {
               multicastAddrBytes[0], multicastAddrBytes[1], multicastAddrBytes[2], multicastAddrBytes[3],    // WsDiscovery Multicast Address: 239.255.255.250
                 ipAddrBytes       [0], ipAddrBytes       [1], ipAddrBytes       [2], ipAddrBytes       [3]
            };
            receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastOpt);

            //            mySocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, ipAddrBytes);
            EndPoint senderEP = new IPEndPoint(multicastAddr, multicastPort);
            int len = 0;
            byte[] dataBytes = new byte[RECEIVEBUFSIZE];
            bool mcgJoining = false;
            lock (this)
            {
                joinFlag = true;
                mcgJoining = joinFlag;
            }
            while (mcgJoining)
            {
                try
                {
                    len = receiveSocket.Receive(dataBytes, dataBytes.Length, SocketFlags.None);
                    if (dataBytes[0] == 0xff)
                    {
                        if (dataBytes[1] == 0xfa)
                        {
                            var ackMsgId = new byte[len - 2];
                            Array.Copy(dataBytes, 2, ackMsgId, 0, len - 2);
                            ackWaiting.Set();
                        }
                    }
                    else
                    {
                        len = ((int)(dataBytes[0]) << 8) + dataBytes[1];
                        byte[] buf = new byte[len];
                        for (int i = 0; i < len; i++)
                        {
                            buf[i] = dataBytes[i + 2];
                        }
                        if (OnMulticastMessageReceived != null)
                        {
                            OnMulticastMessageReceived(buf, len, (IPEndPoint)senderEP);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                }
                lock (this)
                {
                    mcgJoining = joinFlag;
                }
            }

            receiveSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, multicastOpt);
        }
开发者ID:seosoft,项目名称:Library,代码行数:69,代码来源:MulticastClient.cs

示例12: SocketTest1_TCP_SendTimeout

        public MFTestResults SocketTest1_TCP_SendTimeout()
        {
            ///<summary>
            ///1. Starts a server socket listening for TCP
            ///2. Starts a client socket connected to the server
            ///3. Verifies that data can be correctly sent and recieved
            ///4. Verifies Send timeout is functional
            ///</summary>

            ///skip this test for the emulator since setting socket options is not supported.
            if (Microsoft.SPOT.Hardware.SystemInfo.SystemID.SKU == 3)
                return MFTestResults.Skip;

            MFTestResults testResult = MFTestResults.Fail;

            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                int sendTimeout = (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
                Log.Comment("Get the SendTimeout size on the server = " + sendTimeout);

                int newTimeout = 1000;
                Log.Comment("Set the receiveTimeout size on the server = " + newTimeout);
                client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, newTimeout);

                int sendTimeout2 = (int)client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
                Log.Comment("The new SendTimeout size on the server=" + sendTimeout2);


                Log.Comment("Testing with port 0");
                client.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                server.Bind(new IPEndPoint(IPAddress.Loopback, 0));

                IPEndPoint epClient = (IPEndPoint)client.LocalEndPoint;
                IPEndPoint epServer = (IPEndPoint)server.LocalEndPoint;

                server.Listen(1);

                client.Connect(epServer);

                using (Socket sock = server.Accept())
                {
                    byte[] recBytes = new byte[12000];

                    Log.Comment("Send to a closed server to cause a timeout.");
                    try
                    {
                        for( int i=0; i<1000; ++i)
                        {
                            int bytesSent = client.Send(recBytes);
                        }
                        Log.Comment("Receiving bytes.");
                        testResult = MFTestResults.Fail;
                    }
                    catch (SocketException e)
                    {
                        Log.Comment("correctly threw exception after Send Timeout on server: " + e);
                        testResult = MFTestResults.Pass;
                    }
                    catch (Exception e)
                    {
                        Log.Comment("incorrectly threw exception: " + e);
                        testResult = MFTestResults.Fail;
                    }
                }
            }
            catch (SocketException e)
            {
                if (e.ErrorCode == (int)SocketError.ProtocolOption)
                {
                    testResult = MFTestResults.Skip;
                }
                else
                {
                    Log.Comment("Caught exception: " + e.Message);
                    Log.Comment("ErrorCode: " + e.ErrorCode.ToString());
                    testResult = MFTestResults.Fail;
                }
            }
            catch (Exception e)
            {
                Log.Comment("Caught exception: " + e.Message);
                testResult = MFTestResults.Fail;
            }
            finally
            {
                server.Close();
                client.Close();
            }
            return testResult;
        }
开发者ID:koson,项目名称:.NETMF_for_LPC17xx,代码行数:93,代码来源:SocketTests.cs

示例13: getSendBufferSize

 public static int getSendBufferSize(Socket socket)
 {
     int sz;
     try
     {
         sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
     }
     catch(SocketException ex)
     {
         closeSocketNoThrow(socket);
         throw new Ice.SocketException(ex);
     }
     return sz;
 }
开发者ID:bholl,项目名称:zeroc-ice,代码行数:14,代码来源:Network.cs

示例14: ProcessConnect

        protected void ProcessConnect(Socket socket, object state, SocketAsyncEventArgs e)
        {
            if (e != null && e.SocketError != SocketError.Success)
            {
                e.Dispose();
                m_InConnecting = false;
                OnError(new SocketException((int)e.SocketError));
                return;
            }

            if (socket == null)
            {
                m_InConnecting = false;
                OnError(new SocketException((int)SocketError.ConnectionAborted));
                return;
            }

            //To walk around a MonoTouch's issue
            //one user reported in some cases the e.SocketError = SocketError.Succes but the socket is not connected in MonoTouch
            if (!socket.Connected)
            {
                m_InConnecting = false;
#if SILVERLIGHT || NETFX_CORE
                var socketError = SocketError.ConnectionReset;
#else
                var socketError = (SocketError)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
#endif
                OnError(new SocketException((int)socketError));
                return;
            }

            if (e == null)
                e = new SocketAsyncEventArgs();

            e.Completed += SocketEventArgsCompleted;

            Client = socket;

            m_InConnecting = false;

#if !SILVERLIGHT && !NETFX_CORE
            try
            {
                //Set keep alive
                Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
            }
            catch
            {
            }
            
#endif
            OnGetSocket(e);
        }
开发者ID:felixdmAR,项目名称:SuperSocket.ClientEngine,代码行数:53,代码来源:TcpClientSession.cs

示例15: CheckError

 private void CheckError(int sentBytes, int length, Socket socket)
 {
     // if (sentBytes != length)
     //     _logger.Warn("Not all bytes sent");
     var socketError = (SocketError)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error);
     if (socketError != SocketError.Success)
     {
         _logger.ErrorFormat("Error on send :  {0}", socketError);
     }
 }
开发者ID:jbouzaglou,项目名称:PetPigeonsESB,代码行数:10,代码来源:SendingThread.cs


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