本文整理汇总了C#中UdpClient.Send方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.Send方法的具体用法?C# UdpClient.Send怎么用?C# UdpClient.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UdpClient
的用法示例。
在下文中一共展示了UdpClient.Send方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
UdpClient server = new UdpClient("127.0.0.1", 9877);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9876);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.Send(data, data.Length);
data = server.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
while(true){
input = Console.ReadLine();
if (input == "exit")
break;
server.Send(Encoding.ASCII.GetBytes(input), input.Length);
data = server.Receive(ref sender);
}
Console.WriteLine("Stopping client");
server.Close();
}
示例2: StartBroadcast
IEnumerator StartBroadcast()
{
// multicast send setup
udp_client = new UdpClient ();
udp_client.JoinMulticastGroup (group_address);
IPEndPoint remote_end = new IPEndPoint (group_address, startup_port);
// sends multicast
while (true)
{
byte[] buffer = Encoding.ASCII.GetBytes ("GameServer");
udp_client.Send (buffer, buffer.Length, remote_end);
yield return new WaitForSeconds (1);
}
}
示例3: Main
public static void Main()
{
byte[] data = new byte[1024];
string input, stringData;
UdpClient udpClient = new UdpClient("127.0.0.1", 9999);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
string welcome = "Hello";
data = Encoding.ASCII.GetBytes(welcome);
udpClient.Send(data, data.Length);
data = udpClient.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
while(true)
{
input = Console.ReadLine();
udpClient.Send(Encoding.ASCII.GetBytes(input), input.Length);
data = udpClient.Receive(ref sender);
stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine(stringData);
}
udpClient.Close();
}
示例4: exportData
private void exportData(ref UdpClient client, ref IPEndPoint anyIP)
{
byte[] data;
string text;
text = "SOT"; // start of table
text = text + System.Environment.NewLine;
data = System.Text.Encoding.ASCII.GetBytes(text);
client.Send(data, data.Length, anyIP);
foreach (var ptr in timeGraphScript.dateTime_val_dic) {
text = ptr.Key.ToString("yyyy/MM/dd HH:mm:ss");
text = text + ",";
text = text + ptr.Value.ToString();
text = text + System.Environment.NewLine;
data = System.Text.Encoding.ASCII.GetBytes(text);
client.Send(data, data.Length, anyIP);
}
text = "EOT"; // end of table
text = text + System.Environment.NewLine;
data = System.Text.Encoding.ASCII.GetBytes(text);
client.Send(data, data.Length, anyIP);
}
示例5: Main
public static void Main()
{
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9876);
UdpClient newsock = new UdpClient(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 9877);
data = newsock.Receive(ref sender);
Console.WriteLine("Message received from {0}:", sender.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
string msg = Encoding.ASCII.GetString(data, 0, data.Length);
data = Encoding.ASCII.GetBytes(msg);
newsock.Send(data, data.Length, sender);
while(true){
data = newsock.Receive(ref sender);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length));
newsock.Send(data, data.Length, sender);
}
}
示例6: Main
public static void Main()
{
string addr = "127.0.0.1";
int port = 7980;
short cmd = 1;
short seq = 0;
string msg = "Hello";
byte[] payload = Encoding.ASCII.GetBytes(msg);
byte[] packet = Packet.Create(cmd, seq, payload);
UdpClient udp = new UdpClient();
var ip = IPAddress.Parse(addr);
IPEndPoint ep = new IPEndPoint(ip, port);
udp.Connect(ep);
// send
udp.Send(packet, packet.Length);
// receive
bool done = false;
while (!done) {
if (udp.Available <= 0) {
IPEndPoint ep2 = new IPEndPoint(0, 0);
byte[] packet2 = udp.Receive(ref ep2);
Console.WriteLine("packet size: {0}", packet2.Length);
Dictionary<string, object> parsed = Packet.Parse(packet2);
foreach (KeyValuePair<string, object> item in parsed) {
Console.WriteLine("Received:{0} = {1}", item.Key, item.Value);
}
done = true;
}
}
}
示例7: FuncRcvData
private void FuncRcvData()
{
client = new UdpClient (port);
client.Client.ReceiveTimeout = 300; // msec
client.Client.Blocking = false;
while (stopThr == false) {
try {
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
string text = Encoding.ASCII.GetString(data);
lastRcvd = text;
if (lastRcvd.Length > 0) {
Thread.Sleep(delay_msec);
client.Send(data, data.Length, anyIP); // echo
}
}
catch (Exception err)
{
// print(err.ToString());
}
// without this sleep, on adnroid, the app will not start (freeze at Unity splash)
Thread.Sleep(20); // 200
}
client.Close ();
}
示例8: SendMes
private static void SendMes(string datagram)
{
// Создаем UdpClient
UdpClient sender = new UdpClient();
// Создаем endPoint по информации об удаленном хосте
IPEndPoint endPoint = new IPEndPoint(remoteIPAddress, remotePort);
try
{
// Преобразуем данные в массив байтов
byte[] bytes = Encoding.UTF8.GetBytes(datagram);
// Отправляем данные
sender.Send(bytes, bytes.Length, endPoint);
}
catch (Exception ex)
{
Console.WriteLine("Возникло исключение: " + ex.ToString() + "\n " + ex.Message);
}
finally
{
// Закрыть соединение
sender.Close();
}
}
示例9: WakeUp
public static void WakeUp(byte[] mac, string ip = "255.255.255.255", int port = 9)
{
using (var client = new UdpClient {EnableBroadcast = true})
{
client.Send(CreateMagickPacket(mac), MagickPacketSize, ip, port);
}
}
示例10: procComm
void procComm()
{
port = getPort();
string ipadr = getIpadr ();
client = new UdpClient ();
// send
string sendstr = IFmsg.text + System.Environment.NewLine;
byte[] data = ASCIIEncoding.ASCII.GetBytes (sendstr);
client.Send (data, data.Length, ipadr, port);
// receive
client.Client.ReceiveTimeout = 2000; // msec
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
lastRcvd = "";
try {
data = client.Receive (ref remoteIP);
if (data.Length > 0) {
string text = Encoding.ASCII.GetString (data);
lastRcvd = text;
}
} catch (Exception err) {
}
client.Close ();
}
示例11: broadCastCloudRequests
public void broadCastCloudRequests(bool continuous)
{
UdpClient udp = new UdpClient ();
string message = CloudMessage.createRequestMessage (continuous ? 1 : 0);
byte[] data = Encoding.UTF8.GetBytes (message);
IPEndPoint remoteEndPoint = new IPEndPoint (IPAddress.Broadcast, TrackerProperties.Instance.listenPort + 1);
udp.Send (data, data.Length, remoteEndPoint);
}
示例12: Send
public void Send()
{
UdpClient client = new UdpClient();
IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 15000);
byte[] bytes = Encoding.ASCII.GetBytes("Foo");
client.Send(bytes, bytes.Length, ip);
client.Close();
}
示例13: broadcast
void broadcast(string message) {
// multicast send setup
udp_client = new UdpClient ();
udp_client.JoinMulticastGroup (group_address);
remote_end = new IPEndPoint (group_address, port);
byte[] buffer = Encoding.ASCII.GetBytes (message);
udp_client.Send (buffer, buffer.Length, remote_end);
}
示例14: Main
static void Main(string[] args)
{
UdpClient client = new UdpClient("127.0.0.1", 0);
client.Receive += (o, e) => {
Console.WriteLine(Encoding.UTF8.GetString(
e.Data, 0, e.Count));
};
client.Send("http://www.163.com", "127.0.0.1", 8089);
Console.Read();
}
示例15: Main
public static void Main ()
{
var ip = IPAddress.Parse ("239.255.255.250");
var bytes = new byte [] {60, 70, 75};
UdpClient udp = new UdpClient ();
udp.Connect (ip, 3802);
udp.Send (bytes, 3);
Console.WriteLine ("Sent");
udp.Close ();
}