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


C# TcpListener.BeginAcceptTcpClient方法代码示例

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


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

示例1: Coob

        public Coob(CoobOptions options)
        {
            this.Options = options;
            MessageQueue = new ConcurrentQueue<Packet.Base>();
            PacketParsers = new Dictionary<int, PacketParserDel>();
            Clients = new Dictionary<ulong, Client>();
            World = new World(options.WorldSeed);

            PacketParsers.Add((int)CSPacketIDs.EntityUpdate, Packet.EntityUpdate.Parse);
            PacketParsers.Add((int)CSPacketIDs.Interact, Packet.Interact.Parse);
            PacketParsers.Add((int)CSPacketIDs.Shoot, Packet.Shoot.Parse);
            PacketParsers.Add((int)CSPacketIDs.ClientChatMessage, Packet.ChatMessage.Parse);
            PacketParsers.Add((int)CSPacketIDs.ChunkDiscovered, Packet.UpdateChunk.Parse);
            PacketParsers.Add((int)CSPacketIDs.SectorDiscovered, Packet.UpdateSector.Parse);
            PacketParsers.Add((int)CSPacketIDs.ClientVersion, Packet.ClientVersion.Parse);

            try
            {
                clientListener = new TcpListener(IPAddress.Any, options.Port);
                clientListener.Start();
                clientListener.BeginAcceptTcpClient(onClientConnect, null);
            }
            catch (SocketException e)
            {
                if (e.ErrorCode == 10048)
                    Log.Error("Something is already running on port " + options.Port + ". Can't start server.");
                else
                    Log.Error("Unknown error occured while trying to start server:\n" + e);

                Log.Display();
                Environment.Exit(1);
            }
        }
开发者ID:Wroud,项目名称:Coob,代码行数:33,代码来源:Coob.cs

示例2: Connect

        public void Connect(int localPort, int remotePort, IPAddress remoteAddress)
        {
            IPAddress localhost = Dns.GetHostAddresses("127.0.0.1")[0];
            listener = new TcpListener(localhost, localPort);
            BooleanEventArg arg;
            try
            {
                listener.Start();
            }
            catch (Exception e)
            {
                arg = new BooleanEventArg();
                arg.result = false;
                //localClientConnectedToRemoteServer.Set();
                connected(this, arg);
                return;
            }
            
            listener.BeginAcceptTcpClient(AcceptClient, listener);

            client = new TcpClient();

            Thread.Sleep(1000);
            //localClientConnectedToRemoteServer.Reset();
            client.BeginConnect(remoteAddress, remotePort, ServerConnected, client);

            //localClientConnectedToRemoteServer.WaitOne();
        }
开发者ID:Keammoort,项目名称:dotNET-lab3,代码行数:28,代码来源:Communicator.cs

示例3: Connect

 /*
  * Listens for clients connection requests
  */
 public void Connect()
 {
     object state = new object();
     tcp = new TcpListener(IPAddress.Any, 4550);
     tcp.Start();
     tcp.BeginAcceptTcpClient(onAccept, tcp);
 }
开发者ID:francoisvdv,项目名称:PungSeon,代码行数:10,代码来源:Server.cs

示例4: initializeListener

 public void initializeListener()
 {
     listener = new TcpListener(new IPEndPoint(IPAddress.Any, Multiplayer.PORT));
     listener.AllowNatTraversal(true);
     listener.Start();
     listener.BeginAcceptTcpClient(AcceptTcpConnectionCallback, listener);
 }
开发者ID:strangea,项目名称:OpenHacknet,代码行数:7,代码来源:ExternalNetworkedServer.cs

示例5: Start

		/// <summary>
		/// Start server and begin accepting clients
		/// </summary>
		public static void Start()
		{
			futureMoves = new Queue<FutureMove>();
			clientsPlaying = new List<Connection>();
			clientUpdate = new List<Connection>();
			clientAI = new List<Connection>();
			clientPlayers = new List<Connection>();
			clientsAll = new List<Connection>();
			updateTimer.Elapsed += new ElapsedEventHandler(UpdateTick);
			updateTimer.AutoReset = false;
			if (listener != null) listener.Stop();
			listener = TcpListener.Create(Program.port);
			listener.Start();
			allDone = new ManualResetEvent(false);
			try
			{
				while (true)
				{
					allDone.Reset();
					listener.BeginAcceptTcpClient(new AsyncCallback(AcceptTcpClient), listener);
					allDone.WaitOne();
				}
			}
			catch (ThreadAbortException)
			{
				listener.Stop();
			}
		}
开发者ID:Xsichtik,项目名称:Bomberman,代码行数:31,代码来源:Server.cs

示例6: Run

 /// <summary>
 /// Metoda uruchamiaj¹ca workera i rozpoczynaj¹ca nas³uch na odpowiednim IP i porcie
 /// </summary>
 public override void Run()
 {
     TcpListener listener = null;
     try
     {
         listener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["EncryptEndpoint"].IPEndpoint);
         listener.ExclusiveAddressUse = false;
         listener.Start();
     }
     catch (SocketException)
     {
         Trace.Write("Encrypting server could not start.", "Error");
         return;
     }
         while (true)
         {
             try
             {
                 IAsyncResult result = listener.BeginAcceptTcpClient(HandleAsyncConnection, listener);
                 connectionWaitHandle.WaitOne();
             }
             catch
             {
                 continue;
             }
         }
 }
开发者ID:RadekJ1992,项目名称:EncryptServer,代码行数:30,代码来源:WorkerRole.cs

示例7: Server

 public Server()
 {
     config = new INIReader(System.IO.File.ReadAllLines("config.ini"));
     chat = new ServerChat();
     instance = this;
     vehicleController = new ServerVehicleController();
     api = new ServerApi(this);
     gamemodeManager = new GamemodeManager(api);
     gamemodeManager.loadFromFile("gamemodes/" + config.getString("gamemode"));
     server = new TcpListener(IPAddress.Any, config.getInt("game_port"));
     server.Start();
     server.BeginAcceptTcpClient(onIncomingConnection, null);
     playerpool = new List<ServerPlayer>();
     Timer timer = new Timer();
     timer.Elapsed += onBroadcastTimer;
     timer.Interval = config.getInt("broadcast_interval");
     timer.Enabled = true;
     timer.Start();
     UDPStartPort = config.getInt("udp_start_port");
     Timer timer_slow = new Timer();
     timer_slow.Elapsed += timer_slow_Elapsed;
     timer_slow.Interval = config.getInt("slow_interval");
     timer_slow.Enabled = true;
     timer_slow.Start();
     http_server = new HTTPServer();
     Console.WriteLine("Started game server on port " + config.getInt("game_port").ToString());
     Console.WriteLine("Started http server on port " + config.getInt("http_port").ToString());
 }
开发者ID:andrefsantos,项目名称:gta-iv-multiplayer,代码行数:28,代码来源:Server.cs

示例8: TcpAcceptor

        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="iAddress">ローカルのIPアドレスを指定します。</param>
        /// <param name="iPort">待受けに使用するポート番号を指定します。</param>
        public TcpAcceptor(IPAddress iAddress, int iPort)
        {
            // 引数のチェック
            if (iAddress == null)
            {
                var nm = MethodBase.GetCurrentMethod().GetParameters()[0].Name;
                throw new ArgumentNullException(nm);
            }
            else if (iPort <= 0)
            {
                var nm = MethodBase.GetCurrentMethod().GetParameters()[1].Name;
                throw new ArgumentOutOfRangeException(nm, "不正なポート番号が指定されました。");
            }

            try
            {
                // 待受け開始
                var listener = new TcpListener(iAddress, iPort);
                listener.Start();
                listener.BeginAcceptTcpClient(new AsyncCallback(this.AcceptCallback), null);

                this.tcpListener = listener;
            }
            catch (Exception)
            {
                // そのまま呼び出し元にスローする。
                throw;
            }
        }
开发者ID:SatohNorio,项目名称:WpfTcpCommunicationSample,代码行数:34,代码来源:TcpAcceptor.cs

示例9: Main

        public static void Main(string[] args)
        {
            Action<object> cw = Console.WriteLine;
              cw("* any");
              cw(IPAddress.Any);
              cw(IPAddress.IPv6Any);
              cw("* loopback");
              cw(IPAddress.Loopback);
              cw(IPAddress.IPv6Loopback);
              cw("* none");
              cw(IPAddress.None);
              cw(IPAddress.IPv6None);
              cw("* bc4"); //trivia: there is no bc6, only multicast
              cw(IPAddress.Broadcast);

              TcpListener l = new TcpListener(IPAddress.Any, 7896);
              l.Start();
              l.BeginAcceptTcpClient(ar => {
            var c = l.EndAcceptTcpClient(ar);
            l.Stop();
            //var s = new StreamWriter(c.GetStream());
            //s.WriteLine("test writer : needed");
            //s.Flush();
            var bs = System.Text.Encoding.UTF8.GetBytes("test direct : not needed");
            c.GetStream().Write(bs, 0, bs.Length);
            c.Close();
              }, l);

              //start client
              var q = new TcpClient("localhost", 7896);
              var qs = new StreamReader(q.GetStream());
              Console.WriteLine(qs.ReadLine());
              // l.Stop();
        }
开发者ID:Maartyl,项目名称:cs-chan,代码行数:34,代码来源:Program.cs

示例10: MockBroker

        public MockBroker()
        {
            listener = new TcpListener(Dns.GetHostAddresses("localhost")[0], brokerPort);
            listener.Start();

            listener.BeginAcceptTcpClient(ConnectAccept, null);
        }
开发者ID:fm0597,项目名称:nmqtt,代码行数:7,代码来源:MockBroker.cs

示例11: StartListening

 static void StartListening()
 {
     while (!Server.shouldShutdown)
     {
         try
         {
             listener = new TcpListener(System.Net.IPAddress.Any, Configuration.PORT);
             listener.Start();
             IAsyncResult ar = listener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), listener);
             break;
         }
         catch (SocketException E)
         {
             Console.WriteLine("e1");
             Server.Log(E, LogTypesEnum.error);
             break;
         }
         catch (Exception E)
         {
             Console.WriteLine("e2");
             Server.Log(E, LogTypesEnum.error);
             continue;
         }
     }
 }
开发者ID:MinedroidFTW,项目名称:MineFrog,代码行数:25,代码来源:ConnectionHandler.cs

示例12: StartListener

 private void StartListener(IPEndPoint endPoint)
 {
     _listener = new TcpListener(endPoint);
     _listener.Start(5);
     _log.WriteLine("Server {0} listening", endPoint.Address.ToString());
     _listener.BeginAcceptTcpClient(OnAccept, null);
 }
开发者ID:nnyamhon,项目名称:corefx,代码行数:7,代码来源:DummyTcpServer.cs

示例13: TCPServer

 public TCPServer(int port, Func<Tuple<Socket, Data>, int> receive)
     : base(receive)
 {
     listener = new TcpListener(IPAddress.Any, port);
     listener.Start();
     listener.BeginAcceptTcpClient(ClientConnectedCallback, null);
 }
开发者ID:NamedJohnny,项目名称:ift585_tp3,代码行数:7,代码来源:TCPServer.cs

示例14: StartListening

        public void StartListening()
        {
            listener = new TcpListener(IPAddress.Any,port);
            listener.Start(50);
            try
            {
                for (; ; )
                {
                    //set the event to nonsignaled state.
                    allDone.Reset();

                    Console.WriteLine("Waiting for a connection...");

                    listener.BeginAcceptTcpClient(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:wangws556,项目名称:duoduo-chat,代码行数:28,代码来源:TcpAsynchronousServer.cs

示例15: Initialize

        public void Initialize()
        {
            tcpServer = new TcpListener(IPAddress.Any, 1901);

            tcpServer.BeginAcceptTcpClient()

            sender = new UdpClient();

            sender.DontFragment = true;

            sender.JoinMulticastGroup(remoteEndPoint.Address);

            listener = new UdpClient();

            listener.ExclusiveAddressUse = false;

            listener.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            listener.ExclusiveAddressUse = false;

            listener.Client.Bind(anyEndPoint);

            listener.DontFragment = true;

            listener.JoinMulticastGroup(remoteEndPoint.Address);

            listener.BeginReceive(ReceiveCallback, null);
        }
开发者ID:DisruptionTheory,项目名称:Iris,代码行数:28,代码来源:IrisNetworkManager.cs


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