本文整理汇总了C#中System.Net.Sockets.Socket.ReceiveFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.ReceiveFrom方法的具体用法?C# Socket.ReceiveFrom怎么用?C# Socket.ReceiveFrom使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.Socket
的用法示例。
在下文中一共展示了Socket.ReceiveFrom方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReceiveFrom1
public static void ReceiveFrom1()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);
Socket s = new Socket(endPoint.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);
// Creates an IPEndPoint to capture the identity of the sending host.
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)sender;
// Binding is required with ReceiveFrom calls.
s.Bind(endPoint);
byte[] msg = new Byte[256];
Console.WriteLine ("Waiting to receive datagrams from client...");
// This call blocks.
s.ReceiveFrom(msg, ref senderRemote);
s.Close();
}
示例2: ReceiveFrom2
public static void ReceiveFrom2()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);
Socket s = new Socket(endPoint.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);
// Creates an IpEndPoint to capture the identity of the sending host.
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)sender;
// Binding is required with ReceiveFrom calls.
s.Bind(endPoint);
byte[] msg = new Byte[256];
Console.WriteLine ("Waiting to receive datagrams from client...");
// This call blocks.
s.ReceiveFrom(msg, SocketFlags.None, ref senderRemote);
s.Close();
}
示例3: ReceiveFrom3
public static void ReceiveFrom3()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);
Socket s = new Socket(endPoint.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);
// Creates an IPEndPoint to capture the identity of the sending host.
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)sender;
// Binding is required with ReceiveFrom calls.
s.Bind(endPoint);
byte[] msg = new Byte[256];
Console.WriteLine ("Waiting to receive datagrams from client...");
// This call blocks.
s.ReceiveFrom(msg, msg.Length, SocketFlags.None, ref senderRemote);
s.Close();
}
示例4: ReceiveFrom4
public static void ReceiveFrom4()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);
Socket s = new Socket(endPoint.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);
// Creates an IpEndPoint to capture the identity of the sending host.
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint senderRemote = (EndPoint)sender;
// Binding is required with ReceiveFrom calls.
s.Bind(endPoint);
byte[] msg = new Byte[256];
Console.WriteLine ("Waiting to receive datagrams from client...");
// This call blocks.
s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
s.Close();
}
示例5: Main
/*
C# Network Programming
by Richard Blum
Publisher: Sybex
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SimpleUdpClient
{
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while(true)
{
input = Console.ReadLine();
if (input == "exit")
break;
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("Stopping client");
server.Close();
}
}