本文整理汇总了C#中UdpClient.BeginReceive方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.BeginReceive方法的具体用法?C# UdpClient.BeginReceive怎么用?C# UdpClient.BeginReceive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UdpClient
的用法示例。
在下文中一共展示了UdpClient.BeginReceive方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Backend
public Backend(string ipAddress, int port)
{
// endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
// this.EnableTimedTriggers();
endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
client = new UdpClient();
client.Connect(endpoint);
client.BeginReceive(new AsyncCallback(OnMessageRecieved), null);
}
示例2: StartListen
void StartListen()
{
// multicast receive setup
remote_end = new IPEndPoint (IPAddress.Any, port);
udp_client = new UdpClient (remote_end);
udp_client.JoinMulticastGroup (group_address);
// async callback for multicast
udp_client.BeginReceive (new AsyncCallback (ReceiveAnnounceCallback), null);
}
示例3: Main
static void Main(string[] args)
{
int receiverPort = 2000;
UdpClient receiver = new UdpClient(receiverPort);
Console.WriteLine("port: " + receiverPort);
receiver.BeginReceive(alinandata, receiver);
Console.ReadKey();
}
示例4: Init
public void Init(string nothing)
{
remoteEndPoint = new IPEndPoint(IPAddress.Parse(remoteAddress), remotePort);
timestamps = new Dictionary<string, Timestamp>();
receivedMsgBuffer = new StringBuilder();
completeMsgBuffer = new List<string>();
try
{
TCPBuffer = new byte[1024];
sendingUDPClient = new UdpClient();
sendingUDPClient.ExclusiveAddressUse = false;
sendingUDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
sendingUDPClient.Connect(remoteAddress, remotePort);
receivingUDPClient = new UdpClient();
receivingUDPClient.ExclusiveAddressUse = false;
receivingUDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
receivingUDPClient.Client.Bind((IPEndPoint)sendingUDPClient.Client.LocalEndPoint);
receivingUDPClient.Connect(remoteAddress, remotePort);
receivingUDPClient.BeginReceive(new AsyncCallback(OnMessageUDP), null);
// Send initial message to register the address and port at the server
string hello = "Hello Server";
byte[] bytes = Encoding.UTF8.GetBytes(hello);
try
{
sendingUDPClient.Send(bytes, bytes.Length);
}
catch (Exception err)
{
#if !UNITY_EDITOR
Application.ExternalCall(onErrorCallback, "An error occurred while sending the initial message to the server: " + err.Message);
#else
UnityEngine.Debug.LogError("An error occurred while sending the initial message to the server: " + err.Message);
#endif
}
TCPClient = new TcpClient();
TCPClient.NoDelay = true;
TCPClient.Connect(remoteEndPoint);
TCPClient.GetStream().BeginRead(TCPBuffer, 0, 1024, new AsyncCallback(OnMessageTCP), null);
}
catch (Exception err)
{
#if !UNITY_EDITOR
Application.ExternalCall(onErrorCallback, err.ToString());
#else
UnityEngine.Debug.LogError("Init(): " + err.ToString());
#endif
}
}
示例5: DiscoveryManager
public DiscoveryManager()
{
ActivityServices = new List<ServiceInfo>();
DiscoveryType = DiscoveryType.WsDiscovery;
#if ANDROID
_messageId = Guid.NewGuid().ToString();
_udpClient = new UdpClient(WsDiscoveryPort);
_udpClient.JoinMulticastGroup(IPAddress.Parse(WsDiscoveryIPAddress));
_udpClient.BeginReceive(HandleRequest, _udpClient);
#endif
}
示例6: RecieveCandidates
void RecieveCandidates()
{
IPEndPoint remote_end = new IPEndPoint (IPAddress.Any, startup_port);
UdpClient udp_client = new UdpClient (remote_end);
udp_client.JoinMulticastGroup (group_address);
UdpState s = new UdpState();
s.e = remote_end;
s.u = udp_client;
// async callback for multicast
udp_client.BeginReceive (new AsyncCallback (ServerLookup), s);
}
示例7: Main
static int Main (string [] args)
{
int port = 8001;
UdpClient udpClient = new UdpClient (port);
IPAddress ip = IPAddress.Parse ("224.0.0.2");
udpClient.JoinMulticastGroup (ip, IPAddress.Any);
udpClient.MulticastLoopback = true;
udpClient.Ttl = 1;
udpClient.BeginReceive (ReceiveNotification, udpClient);
udpClient.Send (new byte [1] { 255 }, 1, new IPEndPoint (ip, port));
System.Threading.Thread.Sleep (1000);
udpClient.DropMulticastGroup (ip);
if (!_receivedNotification)
return 1;
return 0;
}
示例8: ReceiveMessages
public static void ReceiveMessages()
{
// Receive a message and write it to the console.
e = new IPEndPoint(IPAddress.Any, 19000);
u = new UdpClient(e);
Console.WriteLine("listening for messages");
u.BeginReceive(new AsyncCallback(ReceiveCallback), null);
// Do some work while we wait for a message. For this example,
// we'll just sleep
while (true)
{
Thread.Sleep(100);
}
}
示例9: udpRestart
public void udpRestart()
{
if (_udpClient != null)
{
_udpClient.Close();
}
_stringsToParse = new List<byte[]>();
_anyIP = new IPEndPoint(IPAddress.Any, TrackerProperties.Instance.listenPort);
_udpClient = new UdpClient(_anyIP);
_udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
Debug.Log("[UDPListener] Receiving in port: " + TrackerProperties.Instance.listenPort);
}
示例10: Start
void Start()
{
foreach (PositionUpdateBehavior pub in GameObject.FindObjectsOfType<PositionUpdateBehavior>())
{
updateBehaviors.Add(pub);
}
client = new UdpClient(port);
try
{
client.BeginReceive(new AsyncCallback(AcceptCallback), null);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
示例11: ListenServer
/********************/
/****** CLIENT ******/
/********************/
public void ListenServer()
{
// open a listening port on a random port
// to receive a response back from server
// using 0 doesn't seem to work reliably
// so we'll just do it ourselves
int myPort = UnityEngine.Random.Range(15001,16000);
IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, myPort);
UdpClient uc1 = new UdpClient(ep1);
UdpState us1 = new UdpState();
us1.e = ep1;
us1.u = uc1;
uc1.BeginReceive(new AsyncCallback(ListenServerCallback), us1);
broadcastClient = uc1;
broadcastEndPoint = ep1;
Debug.Log("Broadcast listener opened on port " + broadcastEndPoint.Port.ToString());
}
示例12: ListenForClients
/********************/
/****** SERVER ******/
/********************/
public void ListenForClients(string g)
{
// open a listening port on known port 15000
// to listen for any clients
IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, 15000);
UdpClient uc1 = new UdpClient(ep1);
UdpState us1 = new UdpState();
us1.e = ep1;
us1.u = uc1;
uc1.BeginReceive(new AsyncCallback(ListenForClientsCallback), us1);
multiGameName = g;
waitingResponse = true;
Debug.Log("Server listening port opened");
}
示例13: Receive
/// <summary>
/// Asynchronously listens on the given client for a single packet.
/// </summary>
public static void Receive(UdpClient Client, ReceiveRawPacketHandler OnReceive)
{
// Good job Microsoft, for making this so easy O_O
while (true)
{
try
{
Client.BeginReceive(delegate(IAsyncResult ar)
{
lock (Client)
{
IPEndPoint end = new IPEndPoint(IPAddress.Any, 0);
byte[] data;
try
{
data = Client.EndReceive(ar, ref end);
OnReceive(end, data);
}
catch (SocketException se)
{
if (se.SocketErrorCode == SocketError.Shutdown)
{
return;
}
if (_CanIgnore(se))
{
Receive(Client, OnReceive);
}
else
{
throw se;
}
}
catch (ObjectDisposedException)
{
return;
}
}
}, null);
return;
}
catch (SocketException se)
{
if (!_CanIgnore(se))
{
throw se;
}
}
catch (ObjectDisposedException)
{
return;
}
}
}
示例14: SendUdpNetworkPacket
// Actually builds, sends, sets the received bytes and returns the whole packet
private EDNSPacket SendUdpNetworkPacket(string sDNSIP, string sIPToResolve, int nPort, byte bType, bool bEDNS, byte[] bMachine_ID)
{
// Create empty EDNS packet
EDNSPacket Packet = new EDNSPacket();
try
{
IPEndPoint Endpoint = new IPEndPoint(IPAddress.Parse(sDNSIP), nPort);
// Send the current machine_id host
if (bEDNS)
Packet.CreateEDNSPacketMachineID(sIPToResolve, bType, bMachine_ID);
else
Packet.CreateDNSPacket(sIPToResolve, bType);
if (Packet.GetPacketLen() > 0)
{
// Create a udp client to send the packet
UdpClient udpGo = new UdpClient();
udpGo.Client.SendTimeout = 2000;
udpGo.Client.ReceiveTimeout = 2000;
udpGo.Send(Packet.GetPacket(), Packet.GetPacket().Length, Endpoint);
//IPEndPoint object will allow us to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Launch Asynchronous
IAsyncResult iarResult = udpGo.BeginReceive(null, null);
// Wait until complete
bool bKill = false;
DateTime dtTimeStart = DateTime.Now;
while (iarResult.IsCompleted == false)
{
// Sleep instead of cycling
System.Threading.Thread.Sleep(100);
// Watchdog, if doesn't return in 5 seconds get out
if (dtTimeStart.AddSeconds(5) < DateTime.Now)
{
bKill = true;
break;
}
}
// This can hang when not happy about a broken connection
if (bKill)
udpGo.Close();
else
Packet.SetReceivePacket(udpGo.EndReceive(iarResult, ref RemoteIpEndPoint));
}
}
catch (Exception Ex)
{
// TODO: Log an exception?
}
// Always just return packet
return Packet;
}
示例15: ListenForClients
//specific to LAN
private void ListenForClients(int _ListenPort)
{
System.Diagnostics.Debug.Assert(m_BroadcastListeningClientLAN == null);
// open a listening port on known port
// to listen for any clients
IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, _ListenPort);
UdpClient uc1 = new UdpClient(ep1);
UdpClientState ucs1 = new UdpClientState(ep1, uc1);
uc1.BeginReceive(new System.AsyncCallback(ListenForClientsCallback), ucs1);
m_BroadcastListeningClientLAN = ucs1;
Debug.Log("Server listening for clients on port: " + _ListenPort);
}