本文整理匯總了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();
}
}