本文整理汇总了C#中Socket.BeginReceiveFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginReceiveFrom方法的具体用法?C# Socket.BeginReceiveFrom怎么用?C# Socket.BeginReceiveFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.BeginReceiveFrom方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
var sk = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint my = new IPEndPoint(IPAddress.Loopback,1233);
sk.BeginReceiveFrom(new byte[1500], 0, 1500, SocketFlags.None, ref my, new AsyncCallback(ReceiveCallback), null);
sk.Close();
}
示例2: UDPServer
public UDPServer()
{
try
{
// Iniciando array de clientes conectados
this.listaClientes = new ArrayList();
entrantPackagesCounter = 0;
sendingPackagesCounter = 0;
// Inicializando el delegado para actualizar estado
//this.updateStatusDelegate = new UpdateStatusDelegate(this.UpdateStatus);
// Inicializando el socket
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Inicializar IP y escuhar puerto 30000
IPEndPoint server = new IPEndPoint(IPAddress.Any, 30001);
// Asociar socket con el IP dado y el puerto
serverSocket.Bind(server);
// Inicializar IPEndpoint de los clientes
IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
// Inicializar Endpoint de clientes
EndPoint epSender = (EndPoint)clients;
// Empezar a escuhar datos entrantes
serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(ReceiveData), epSender);
}
catch (Exception ex)
{
//Debug.Log("Error al cargar servidor: " + ex.Message+ " ---UDP ");
}
}
示例3: UDPClient
public UDPClient(string serverIP) {
//Paquete sendData = new Paquete ();
//sendData.id = 0;
//sendData.identificadorPaquete = Paquete.Identificador.conectar;
entrantPackagesCounter = 0;
sendingPackagesCounter = 0;
//Creamos conexion
this.clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//Inicializamos IP del servidor
try{
IPAddress ipServer = IPAddress.Parse (serverIP);
IPEndPoint server = new IPEndPoint (ipServer, 30001);
epServer = (EndPoint)server;
//Debug.Log("Enviando data de inicio de conexion: ");
//string data = sendData.GetDataStream();
//byte[] dataBytes = GetBytes (data);
//Enviar solicitud de conexion al servidor
//clientSocket.BeginSendTo (dataBytes,0,dataBytes.Length,SocketFlags.None,epServer,new System.AsyncCallback(this.SendData),null);
// Inicializando el dataStream
this.dataStream = new byte[1024];
// Empezamos a escuhar respuestas del servidor
clientSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epServer, new AsyncCallback(this.ReceiveData), null);
}
catch(FormatException e){
throw e;
}
catch(SocketException e){
throw e;
}
}
示例4: StartReceive
public void StartReceive()
{
if (receiveSocket != null)
StopReceive();
receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
bindEndPoint = new IPEndPoint(IPAddress.Any, 3001);
recBuffer = new byte[maxBuffer];
receiveSocket.Bind(bindEndPoint);
receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this);
}
示例5: Initialize
public void Initialize(String _remoteIp, int _remotePort, int _localPort)
{
Debug.Log ("UAC CTOR");
localPort = _localPort;
remoteIp = _remoteIp;
remotePort = _remotePort;
//Setup the socket and message buffer
udpSock = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind (new IPEndPoint (IPAddress.Any, localPort));
buffer = new byte[1024];
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint (IPAddress.Any, 0);
udpSock.BeginReceiveFrom (buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
示例6: StartReceive
public void StartReceive()
{
try
{
if (receiveSocket != null)
StopReceive();
receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
bindEndPoint = new IPEndPoint(IPAddress.Any, 3002);
recBuffer = new byte[maxBuffer];
receiveSocket.Bind(bindEndPoint);
receiveSocket.BeginReceiveFrom(recBuffer, 0, recBuffer.Length, SocketFlags.None, ref bindEndPoint, new AsyncCallback(MessageReceivedCallback), (object)this);
}
catch (SocketException ex)
{
System.Diagnostics.Debug.WriteLine(String.Format("StartReceive SocketException: {0} ", ex.Message));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(String.Format("StartReceive Exception: {0} ", ex.Message));
}
}
示例7: BeginReceiveFrom_NotSupported
public void BeginReceiveFrom_NotSupported()
{
using (Socket sock = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
sock.Bind(ep);
byte[] buf = new byte[1];
Assert.Throws<PlatformNotSupportedException>(() => sock.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref ep, null, null));
}
}
示例8: BeginReceiveFrom_Helper
private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
{
using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
serverSocket.ReceiveTimeout = 500;
int port = serverSocket.BindToAnonymousPort(listenOn);
EndPoint receivedFrom = new IPEndPoint(connectTo, port);
IAsyncResult async = serverSocket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);
// Behavior difference from Desktop: receivedFrom will _not_ change during the synchronous phase.
// IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
// Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
// Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout);
if (!success)
{
throw new TimeoutException();
}
receivedFrom = new IPEndPoint(connectTo, port);
int received = serverSocket.EndReceiveFrom(async, ref receivedFrom);
Assert.Equal(1, received);
Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));
IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
}
}
示例9: Socket_BeginReceiveFromDnsEndPoint_Throws
// "The parameter remoteEP must not be of type DnsEndPoint."
public void Socket_BeginReceiveFromDnsEndPoint_Throws()
{
using (Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
EndPoint receivedFrom = new DnsEndPoint("localhost", port, AddressFamily.InterNetworkV6);
Assert.Throws<ArgumentException>(() =>
{
socket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);
});
}
}
示例10: Socket_BeginReceiveFromV4IPEndPointFromV4Client_Throws
[Fact] // Base case
// "The supplied EndPoint of AddressFamily InterNetwork is not valid for this Socket, use InterNetworkV6 instead."
public void Socket_BeginReceiveFromV4IPEndPointFromV4Client_Throws()
{
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
socket.DualMode = false;
EndPoint receivedFrom = new IPEndPoint(IPAddress.Loopback, UnusedPort);
Assert.Throws<ArgumentException>(() =>
{
socket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);
});
}
示例11: SocketUdpServer
public SocketUdpServer(IPAddress address, bool dualMode, out int port)
{
_server = new Socket(address.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
if (dualMode)
{
_server.DualMode = dualMode;
}
port = _server.BindToAnonymousPort(address);
IPAddress remoteAddress = address.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any;
EndPoint remote = new IPEndPoint(remoteAddress, 0);
_server.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref remote, Received, null);
}
示例12: BeginReceiveFrom_Helper
private void BeginReceiveFrom_Helper(IPAddress listenOn, IPAddress connectTo)
{
using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
serverSocket.ReceiveTimeout = 500;
int port = serverSocket.BindToAnonymousPort(listenOn);
EndPoint receivedFrom = new IPEndPoint(connectTo, port);
IAsyncResult async = serverSocket.BeginReceiveFrom(new byte[1], 0, 1, SocketFlags.None, ref receivedFrom, null, null);
IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
SocketUdpClient client = new SocketUdpClient(serverSocket, connectTo, port);
bool success = async.AsyncWaitHandle.WaitOne(500);
if (!success)
{
throw new TimeoutException();
}
receivedFrom = new IPEndPoint(connectTo, port);
int received = serverSocket.EndReceiveFrom(async, ref receivedFrom);
Assert.Equal(1, received);
Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));
remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
}
}
示例13: Listen
public void Listen(int Port)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
try
{
socket.Bind(new IPEndPoint(IPAddress.Parse("0"), Port));
socket.BeginReceiveFrom(ReceiveBuffer, 0, ReceiveBuffer.Length, SocketFlags.None, ref RemoteEP, new AsyncCallback(ReceiveFrom_Callback), socket);
}
catch
{
if (OnBindFail != null)
{
OnBindFail();
}
}
}