本文整理汇总了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);
}
}
示例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();
}
示例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);
}
示例4: initializeListener
public void initializeListener()
{
listener = new TcpListener(new IPEndPoint(IPAddress.Any, Multiplayer.PORT));
listener.AllowNatTraversal(true);
listener.Start();
listener.BeginAcceptTcpClient(AcceptTcpConnectionCallback, listener);
}
示例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();
}
}
示例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;
}
}
}
示例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());
}
示例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;
}
}
示例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();
}
示例10: MockBroker
public MockBroker()
{
listener = new TcpListener(Dns.GetHostAddresses("localhost")[0], brokerPort);
listener.Start();
listener.BeginAcceptTcpClient(ConnectAccept, null);
}
示例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;
}
}
}
示例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);
}
示例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);
}
示例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();
}
示例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);
}