本文整理汇总了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);
}
}
示例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;
}
}
示例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;
}
示例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);
}
}
示例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);
}
示例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];
}
}
示例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);
}
}
}
示例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);
}
示例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));
}
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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();
}
示例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();
}