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


C# Socket.BeginAccept方法代码示例

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


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

示例1: StartListening

	public static void StartListening()
	{
		// Data buffer for incoming data.     
		// Establish the local endpoint for the socket.     
		// The DNS name of the computer     
		// running the listener is "host.contoso.com".     
		//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
		//IPAddress ipAddress = ipHostInfo.AddressList[0];
		IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
		IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
		// Create a TCP/IP socket.     
		Socket listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
		// Bind the socket to the local     
		//endpoint and listen for incoming connections.     
		try
		{
			listener.Bind(localEndPoint);
			listener.Listen(100);
			while (!stop)
			{
				// Set the event to nonsignaled state.     
				allDone.Reset();
				// Start an asynchronous socket to listen for connections.     
				LogMgr.Log("Waiting for a connection...");
				listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);
				// Wait until a connection is made before continuing.     
				allDone.WaitOne();
			}
		}
		catch (Exception e)
		{
			LogMgr.LogError(e);
		}

	}
开发者ID:learnUnity,项目名称:KU_NET,代码行数:35,代码来源:AsynchronousSocketListener.cs

示例2: StartListening

    public static void StartListening()
    {
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

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

        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true)
            {
                AllDone.Reset();
                listener.BeginAccept(AcceptCallback, listener);
                AllDone.WaitOne();
            }
        }
        catch (Exception e)
        {
            throw;
        }
    }
开发者ID:EttienneS,项目名称:Contingency,代码行数:25,代码来源:Program.cs

示例3: CreateTcpServer

    public bool CreateTcpServer(string ip, int listenPort)
    {
        _port = listenPort;
        _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        foreach (IPAddress address in Dns.GetHostEntry(ip).AddressList)
        {
            try
            {
                IPAddress hostIP = address;
                IPEndPoint ipe = new IPEndPoint(address, _port);

                _listener.Bind(ipe);
                _listener.Listen(_maxConnections);
                _listener.BeginAccept(new System.AsyncCallback(ListenTcpClient), _listener);

                break;

            }
            catch (System.Exception)
            {
                return false;
            }
        }

        return true;
    }
开发者ID:5941120,项目名称:UnitySample,代码行数:27,代码来源:NetTCPServer.cs

示例4: StartListening

 public void StartListening(int port) 
 { 
     try { // Resolve local name to get IP address 
         IPHostEntry entry = Dns.Resolve(Dns.GetHostName()); 
         IPAddress ip = entry.AddressList[0]; 
         // Create an end-point for local IP and port 
         IPEndPoint ep = new IPEndPoint(ip, port); 
         if(isLogging)
             TraceLog.myWriter.WriteLine ("Address: " + ep.Address.ToString() +" : " + ep.Port.ToString(),"StartListening"); 
         EventLog.WriteEntry("MMFCache Async Listener","Listener started on IP: " + 
                 ip.ToString() + " and Port: " +port.ToString()+ "."); 
         // Create our socket for listening 
         s = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); 
         // Bind and listen with a queue of 100 
         s.Bind(ep); 
         s.Listen(100); 
         // Setup our delegates for performing callbacks 
         acceptCallback = new AsyncCallback(AcceptCallback); 
         receiveCallback = new AsyncCallback(ReceiveCallback); 
         sendCallback = new AsyncCallback(SendCallback); 
         // Set the "Accept" process in motion 
         s.BeginAccept(acceptCallback, s); 
     } 
     catch(SocketException e) { 
         Console.Write("SocketException: "+ e.Message); 
     } 
 } 
开发者ID:ppatoria,项目名称:SoftwareDevelopment,代码行数:27,代码来源:Program.cs

示例5: Server

	Server ()
	{
		_listener = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		_listener.Bind (new IPEndPoint (IPAddress.Loopback, 10000));
		_listener.Listen (10);
		_listener.BeginAccept (new AsyncCallback (OnAccept), _listener);
	}
开发者ID:mono,项目名称:gert,代码行数:7,代码来源:server.cs

示例6: Start

    /// <summary>
    /// Socket Initialization Place Any Other Code Before the Socket Initialization
    /// </summary>
    private void Start()
    {
        limits.x = -4;
        limits.y =  8;
        limits.z =  4;

        AudioManager.Instance.PlaySound(EAudioPlayType.BGM, audioClip);

        if (toPototatoeFountainOrNotToPotatoeFountain)
        { StartCoroutine(POTATOFOUNTAIN()); } 

        if (socketBehaviour == ESocketBehaviour.Server)
        {
            clients = new List<Socket>();

            buffer = new byte[1024];

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

            IPEndPoint parsedIP = new IPEndPoint(IPAddress.Parse(IP), port);

            mySocket.Bind(parsedIP);
            mySocket.Listen(100);
            mySocket.BeginAccept( new AsyncCallback(AcceptCallback), null );

            return;
        }

        if (socketBehaviour == ESocketBehaviour.Client)
        {
            buffer = new byte[1];
        }
    }
开发者ID:BenDy557,项目名称:Potato,代码行数:36,代码来源:SocketComm.cs

示例7: ReceiveTimesOut_Throws

        public void ReceiveTimesOut_Throws()
        {
            using (Socket localSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
            {
                using (Socket remoteSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
                {
                    int port = localSocket.BindToAnonymousPort(IPAddress.IPv6Loopback);
                    localSocket.Listen(1);
                    IAsyncResult localAsync = localSocket.BeginAccept(null, null);

                    remoteSocket.Connect(IPAddress.IPv6Loopback, port);

                    Socket acceptedSocket = localSocket.EndAccept(localAsync);
                    acceptedSocket.ReceiveTimeout = 100;

                    SocketException sockEx = Assert.Throws<SocketException>(() =>
                   {
                       acceptedSocket.Receive(new byte[1]);
                   });

                    Assert.Equal(SocketError.TimedOut, sockEx.SocketErrorCode);
                    Assert.True(acceptedSocket.Connected);
                }
            }
        }
开发者ID:fuwei199006,项目名称:corefx,代码行数:25,代码来源:TimeoutTest.cs

示例8: BeginAccept

 public void BeginAccept(int port)
 {
     Ipep = new IPEndPoint(IPAddress.Any, port);
     ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     ServerSocket.Bind(Ipep);
     ServerSocket.Listen(1);
     ServerSocket.BeginAccept(OnAccept, ServerSocket);
 }
开发者ID:arahis,项目名称:Swiper,代码行数:8,代码来源:TcpClient.cs

示例9: BeginAccept_NotListening_Throws_InvalidOperation

        public void BeginAccept_NotListening_Throws_InvalidOperation()
        {
            using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));

                Assert.Throws<InvalidOperationException>(() => socket.BeginAccept(TheAsyncCallback, null));
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:9,代码来源:ArgumentValidationTests.cs

示例10: createServer

 void createServer(int port)
 {
     _acceptServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     _acceptServer.Bind(new IPEndPoint(IPAddress.Loopback, port));
     _acceptServer.Listen(0);
     _acceptServer.BeginAccept(ar => {
         var socket = (Socket)ar.AsyncState;
         _clientServer = socket.EndAccept(ar);
     }, _acceptServer);
 }
开发者ID:sschmid,项目名称:NLog,代码行数:10,代码来源:describe_TcpClientSocket.cs

示例11: CreateServer

 public void CreateServer()
 {
     MySocket = new Socket(AddressFamily.InterNetwork,
                           SocketType.Stream,
                           ProtocolType.IP);
     MySocket.Bind(new IPEndPoint(IPAddress.Any, NetworkConnector.Port));
     MySocket.Listen(10);
     MySocket.BeginAccept(new System.AsyncCallback(this.OnSocketConnect),
                          null);
 }
开发者ID:lasagnaphil,项目名称:Beat-It,代码行数:10,代码来源:NetworkConnector.cs

示例12: AppDomainMethod

	static void AppDomainMethod () {
		Console.WriteLine ("two");
		var socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
		IPEndPoint ep = new IPEndPoint(IPAddress.Any, 9999);
		socket.Bind (ep);
		socket.Listen (10);
		socket.BeginAccept ( delegate {
			Console.WriteLine ("Delegate should not be called!");
			Environment.Exit (1);
		}, socket);
	}
开发者ID:nicolas-raoul,项目名称:mono,代码行数:11,代码来源:appdomain-unload-doesnot-raise-pending-events.cs

示例13: SocketTestServerAPM

        public SocketTestServerAPM(int numConnections, int receiveBufferSize, EndPoint localEndPoint) 
        {
            _log = VerboseTestLogging.GetInstance();
            _receiveBufferSize = receiveBufferSize;

            socket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(localEndPoint);
            socket.Listen(numConnections);
            
            socket.BeginAccept(OnAccept, null);
        }
开发者ID:TAdityaAnirudh,项目名称:corefx,代码行数:11,代码来源:SocketTestServerAPM.cs

示例14: StartListening

    public void StartListening()
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024*1024];

        // Establish the local endpoint for the socket.
        // The DNS name of the computer
        // running the listener is "host.contoso.com".
        IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 12000);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        CalculationManager cm = new CalculationManager();

        // Bind the socket to the local endpoint and listen for incoming connections.
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(8);

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection... port : " + 12000);

                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                allDone.WaitOne();

                // Wait until a connection is made before continuing.

                Console.WriteLine("-------------------------------------");
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }
开发者ID:minikie,项目名称:OTCDerivativesCalculatorModule,代码行数:53,代码来源:AsynchronousSocketListener.cs

示例15: StartListening

    public static void StartListening()
    {
        // Data buffer for incoming data.
        byte[] bytes = new Byte[1024];

        // Establish the local endpoint for the socket.
        // The DNS name of the computer

        IPAddress ipAddress = IPAddress.Parse("188.26.11.125"); //ipHostInfo.AddressList[0];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 80);

        // Create a TCP/IP socket.
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        // Bind the socket to the local endpoint and listen for incoming connections.
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for a connection...");
                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // Wait until a connection is made before continuing.
                allDone.WaitOne();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }

        Console.WriteLine("\nPress ENTER to continue...");
        Console.Read();

    }
开发者ID:cristighr1995,项目名称:Server-Client,代码行数:46,代码来源:Server.cs


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