本文整理汇总了C#中System.Net.Sockets.Socket.BeginAccept方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginAccept方法的具体用法?C# Socket.BeginAccept怎么用?C# Socket.BeginAccept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.BeginAccept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Listener
public Listener(IPEndPoint ipep)
{
m_Accepted = new Queue<Socket>();
m_AcceptedSyncRoot = ((ICollection)m_Accepted).SyncRoot;
m_Listener = Bind(ipep);
if (m_Listener == null)
{
return;
}
DisplayListener();
#if NewAsyncSockets
m_EventArgs = new SocketAsyncEventArgs();
m_EventArgs.Completed += new EventHandler<SocketAsyncEventArgs>( Accept_Completion );
Accept_Start();
#else
m_OnAccept = OnAccept;
try
{
IAsyncResult res = m_Listener.BeginAccept(m_OnAccept, m_Listener);
}
catch (SocketException ex)
{
NetState.TraceException(ex);
}
catch (ObjectDisposedException)
{ }
#endif
}
示例2: Main
public static void Main(string[] args)
{
Console.Out.WriteLine("This is the server");
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
{
acceptDone.Reset();
Console.Out.WriteLine("Listening on port {0}", listenPort);
listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
acceptDone.WaitOne();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
示例3: OnClientConnect
private void OnClientConnect(IAsyncResult AcceptAsync)
{
try
{
Socket Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Sock = ListenerSock.EndAccept(AcceptAsync);
Server ServerSock = new Server(Sock);
//RaiseEvent if event is linked
if (Connected != null)
{
Connected(ServerSock);
}
}
catch
{
}
finally
{
ListenerSock.Dispose();
ListenerSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
ListenerSock.Bind(new IPEndPoint(hostIP, m_Port));
ListenerSock.Listen(0);
ListenerSock.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
}
示例4: StartListening
private void StartListening()
{
_ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_ListenSocket.Bind(new IPEndPoint(IPAddress.Any, _ListenPort));
_ListenSocket.Listen(10);
_ListenSocket.BeginAccept(AcceptCallback, null);
}
示例5: ConnectMaster
public void ConnectMaster()
{
if (c_Connected && c_Server)
return;
try
{
c_Server = true;
//Console.WriteLine("CM Close Slave");
CloseSlave();
c_Master = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c_Master.Bind(new IPEndPoint(IPAddress.Any, Data.MultiPort));
c_Master.Listen(4);
c_Master.BeginAccept(new AsyncCallback(OnClientConnect), null);
//Console.WriteLine("Started");
c_Connecting = false;
c_Connected = true;
}
catch (Exception /*e*/)// I HID e to stop warning message.
{
c_Server = false;
//Console.WriteLine(e.Message);
//Console.WriteLine(e.StackTrace);
}
}
示例6: StartListening
public static void StartListening(int port = 11001)
{
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = IPAddress.Parse(LocalSettings.ServerIP);
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
_allDone = new ManualResetEvent(false);
// 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.
listener.BeginAccept(
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();
}
示例7: start_listening
private void start_listening()
{
try
{
if (m_logPath != null)
File.Delete(m_logPath);
// Create the listening socket...
m_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, PORT);
// Bind to local IP Address...
m_mainSocket.Bind(ipLocal);
// Start listening...
m_mainSocket.Listen(1000);
// Create the call back for any client connections...
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
// m_mainSocket.BeginAccept(new AsyncCallback(AcceptCallback), m_mainSocket);
}
catch (Exception se)
{
Environment.Exit(0);
}
}
示例8: Host
public bool Host(int port)
{
//Dev View
//Console.WriteLine("Hosting on port " + port);
m_vServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
m_vServerSocket.Bind(new IPEndPoint(IPAddress.Any, port));
m_vServerSocket.Listen(kHostConnectionBacklog);
m_vServerSocket.BeginAccept(new System.AsyncCallback(OnClientConnect), m_vServerSocket);
}
catch (System.Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Exception when attempting to host (" + port + "): " + e);
Console.ResetColor();
m_vServerSocket = null;
return false;
}
return true;
}
示例9: MusSocket
internal MusSocket(string musIp, int musPort, IEnumerable<string> allowedIps, int backlog)
{
MusIp = musIp;
MusPort = musPort;
AllowedIps = new HashSet<string>();
foreach (var item in allowedIps) AllowedIps.Add(item);
try
{
Out.WriteLine(
"Starting up asynchronous sockets server for MUS connections for port " +
musPort, "Server.AsyncSocketMusListener");
MsSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
MsSocket.Bind(new IPEndPoint(IPAddress.Any, MusPort));
MsSocket.Listen(backlog);
MsSocket.BeginAccept(OnEvent_NewConnection, MsSocket);
Out.WriteLine(
"Asynchronous sockets server for MUS connections running on port " +
musPort + Environment.NewLine,
"Server.AsyncSocketMusListener");
}
catch (Exception ex)
{
throw new ArgumentException($"No se pudo iniciar el Socket MUS:\n{ex}");
}
}
示例10: StartServer
public void StartServer()
{
try
{
//creez un soclu
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//se creeza un endpoint pentru server
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Parse(IP), 8888);
//setez endpoint-ul creat
listener.Bind(ipLocal);
//soclul trece in starea de ascultare;coada de asteptare este de 4 clienti
listener.Listen(4);
list = new ArrayList(4);
//incep ascultarea neblocanta a conexiunilor
//orice incercare de conectare va fi tratata de metoda OnClientConnect
listener.BeginAccept(new AsyncCallback(OnClientConnect), null);
//creez delegatul pentru afisarea mesajelor
DelegateShowText = new Action<string, Socket>(ShowText);
//atashez un handler pt schimbarea textului
rt.TextChanged += new System.EventHandler(textChanged);
DelegateSendText = new Action<Socket>(SendText);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
示例11: Tracker
public Tracker(string configfile)
{
Configuration = new TrackerConfig(configfile);
Logger.Active = Configuration.Log;
peers = new Dictionary<int, Peer>();
rooms = new Dictionary<string, Room>();
// Listening on socket
IPAddress ipAddr = IPAddress.Parse(Configuration.IPAddress);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, Configuration.Port);
SocketPermission permission = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, "", Configuration.Port);
permission.Demand();
try
{
listener = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(ipEndPoint);
Console.WriteLine("Listening at IP " + ipEndPoint.Address + " and port " + ipEndPoint.Port + ".");
listener.Listen(Configuration.Backlog);
AsyncCallback aCallback = new AsyncCallback(AcceptCallback);
listener.BeginAccept(aCallback, listener);
}
catch (SocketException exc)
{
Logger.WriteLine(exc);
}
}
示例12: Main
static void Main(string[] args)
{
Console.WriteLine("Hello from Console C# Classic HTTP Server");
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
Socket listener = new Socket(SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(100);
Console.ReadLine();
while (true)
{
// Set the event to nonsignaled state.
//allDone.Reset();
// Start an asynchronous socket to listen for connections and receive data from the client.
Console.WriteLine("Waiting for a connection...");
// Accept the connection and receive the first 10 bytes of data.
int receivedDataSize = 10;
listener.BeginAccept(receivedDataSize, new AsyncCallback(AcceptReceiveCallback), listener);
// Wait until a connection is made and processed before continuing.
//allDone.WaitOne();
}
}
示例13: StartListen
public void StartListen(int port)
{
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
m_socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
//bind to local IP Address...
//if ip address is allready being used write to log
try
{
m_socket.Bind(ipLocal);
}
catch(Exception ex)
{
Debug.Fail(ex.ToString(),
string.Format("Can't connect to port {0}!", port));
return;
}
//start listening...
m_socket.Listen(4);
// create the call back for any client connections...
m_socket.BeginAccept(new AsyncCallback(OnClientConnection), null);
}
示例14: SocketPair
/// <summary>
/// Create 2 sockets connected to each other, for communication between threads.
/// </summary>
public static Socket[] SocketPair()
{
Socket Listener;
Socket[] Pair = new Socket[2];
// Start the listener side.
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 0);
Listener = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Listener.Bind(endPoint);
Listener.Listen(1);
IAsyncResult ServerResult = Listener.BeginAccept(null, null);
// Connect the client to the server.
endPoint = new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)Listener.LocalEndPoint).Port);
Pair[0] = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult ClientResult = Pair[0].BeginConnect(endPoint, null, null);
// Get the server side socket.
Pair[1] = Listener.EndAccept(ServerResult);
Pair[0].EndConnect(ClientResult);
Listener.Close();
Pair[0].Blocking = false;
Pair[1].Blocking = false;
return Pair;
}
示例15: beginListening
public static void beginListening()
{
listenS = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenS.Bind(new IPEndPoint(IPAddress.Any, PORT));
listenS.Listen(4);
listenS.BeginAccept(new AsyncCallback(OnCallAccept), null);
}