本文整理汇总了C#中UdpClient.Close方法的典型用法代码示例。如果您正苦于以下问题:C# UdpClient.Close方法的具体用法?C# UdpClient.Close怎么用?C# UdpClient.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UdpClient
的用法示例。
在下文中一共展示了UdpClient.Close方法的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: StartListener
private static void StartListener()
{
//Initiate UDP server
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UdpClient listener = new UdpClient(ListenPort);
IPEndPoint groupEp = new IPEndPoint(IPAddress.Any,ListenPort);
IPEndPoint responseEp;
string receivedCommand;
try
{
while (true)
{
//Wait for incoming command
Console.WriteLine("Waiting for command");
byte[] bytes = listener.Receive( ref groupEp);
receivedCommand = Encoding.ASCII.GetString(bytes,0,bytes.Length);
Console.WriteLine("Received command: " + receivedCommand + " from " + groupEp.Address);
//Send matching response
responseEp = new IPEndPoint(groupEp.Address, ListenPort);
if (receivedCommand == "U" || receivedCommand == "u")
{
using (StreamReader sr = new StreamReader ("/proc/uptime"))
{
String line = sr.ReadToEnd();
Console.WriteLine("Sending uptime: " + line);
byte[] sendbuf = Encoding.ASCII.GetBytes(line);
s.SendTo(sendbuf, responseEp);
}
}
else if(receivedCommand == "L" || receivedCommand == "l")
{
using (StreamReader sr = new StreamReader ("/proc/loadavg"))
{
String line = sr.ReadToEnd();
Console.WriteLine("Sending load average: " + line);
byte[] sendbuf = Encoding.ASCII.GetBytes(line);
s.SendTo(sendbuf, responseEp);
}
}
else
{
Console.WriteLine("Command " + receivedCommand + " not found\n");
byte[] sendbuf = Encoding.ASCII.GetBytes("Input not recognized, please try again!");
s.SendTo(sendbuf, responseEp);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
示例3: Main
public static int Main()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
string received_data;
byte[] receive_byte_array;
try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
// this is the line of code that receives the broadcase message.
// It calls the receive function from the object listener (class UdpClient)
// It passes to listener the end point groupEP.
// It puts the data from the broadcast message into the byte array
// named received_byte_array.
// I don't know why this uses the class UdpClient and IPEndPoint like this.
// Contrast this with the talker code. It does not pass by reference.
// Note that this is a synchronous or blocking call.
receive_byte_array = listener.Receive(ref groupEP);
Console.WriteLine("Received a broadcast from {0}", groupEP.ToString());
received_data = Encoding.ASCII.GetString(receive_byte_array, 0, receive_byte_array.Length);
Console.WriteLine("data follows \n{0}\n\n", received_data);
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
listener.Close();
return 0;
}
示例4: StartListener
private static void StartListener()
{
bool done = false;
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient listener = new UdpClient(groupEP);
Log.Notice("Listener", "Waiting for broadcast");
try
{
while (!done)
{
byte[] bytes = listener.Receive(ref groupEP);
Log.Info("Listener", "Client " + groupEP.ToString() + " is trying to connect");
listener.Connect(groupEP);
Log.Succes("Listener", "Listener connected to client " + groupEP.ToString());
done = true;
//TODO - rest of district server connecting
}
}
catch (Exception e)
{
FrameWork.Logger.Log.Error("Listener", e.ToString());
}
finally
{
listener.Close();
}
}
示例5: ThreadFuncReceive
static void ThreadFuncReceive()
{
try
{
while (true)
{
//подключение к локальному хосту
UdpClient uClient = new UdpClient(LocalPort);
IPEndPoint ipEnd = null;
//получание дейтаграммы
byte[] responce = uClient.Receive(ref ipEnd);
//преобразование в строку
string strResult = Encoding.Unicode.GetString(responce);
Console.ForegroundColor = ConsoleColor.Green;
//вывод на экран
Console.WriteLine(strResult);
Console.ForegroundColor = ConsoleColor.Red;
uClient.Close();
}
}
catch (SocketException sockEx)
{
Console.WriteLine("Ошибка сокета: " + sockEx.Message);
}
catch (Exception ex)
{
Console.WriteLine("Ошибка : " + ex.Message);
}
}
示例6: 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();
}
示例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: 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 ();
}
示例9: StartListener
public void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive( ref groupEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes,0,bytes.Length));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
示例10: 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();
}
示例11: Broadcast
private void Broadcast()
{
if (IsBroadcasting()) {
UdpClient client = new UdpClient();
IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, port);
byte[] data = Encoding.ASCII.GetBytes(message);
client.Send(data, data.Length, ip);
client.Close();
}
}
示例12: 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 ();
}
示例13: read_data
public string read_data(int player, int data_type)
{
int offset = data_type*2 - 2 + player;
UdpClient listener = new UdpClient(port_base+offset);
listener.Client.ReceiveTimeout = 50;
IPEndPoint address = new IPEndPoint(IPAddress.Any, port_base+offset);
byte[] recb;
try {
recb = listener.Receive( ref address );
listener.Close();
return Encoding.ASCII.GetString(recb);
} catch ( SocketException e ) {
e.ToString();
}
listener.Close();
return "silent";
}
示例14: Main
public static void Main ()
{
var ip = IPAddress.Parse ("239.255.255.250");
while (true) {
UdpClient udp = new UdpClient (3802);
udp.JoinMulticastGroup (ip, 1);
IPEndPoint dummy = null;
udp.Receive (ref dummy);
Console.WriteLine ("Received");
udp.DropMulticastGroup (ip);
udp.Close ();
}
}
示例15: ReceiveData
private void ReceiveData()
{
client = new UdpClient(port);
while (m_keepRunning)
{
try
{
IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = client.Receive(ref anyIP);
// Bytes mit der UTF8-Kodierung in das Textformat kodieren.
string text = Encoding.UTF8.GetString(data);
Debug.Log(text);
string[] words = text.Split(' ');
Quaternion q = new Quaternion(float.Parse(words[12]),
float.Parse(words[13]),
float.Parse(words[14]),
float.Parse(words[15]));
Vector3 t = new Vector3(float.Parse(words[18]),
float.Parse(words[19]),
float.Parse(words[20]));
m_newData = new Pose();
UbiMeasurementUtils.coordsysemChange(t, ref m_newData.pos);
UbiMeasurementUtils.coordsysemChange(q, ref m_newData.rot);
}
catch (Exception err)
{
Debug.Log(err.ToString());
}
}
client.Close();
}