public static void Main() { string ip = "172.0.0.1"; int port = 11111; IPAddress iotAddress = IPAddress.Parse(ip); IPEndPoint iotEndpoint = new IPEndPoint(iotAddress, port); byte[] bytes = Encoding.UTF8.GetBytes("Hello World"); UdpState state = new UdpState(); try { using (UdpClient client = new UdpClient(0, AddressFamily.InterNetwork)) { client.Connect(iotEndpoint); state.e = iotEndpoint; state.u = client; // await the response of the IoT device client.BeginReceive(new AsyncCallback(ReceiveCallback), state); client.BeginSend(bytes, bytes.Length, iotEndpoint, new AsyncCallback(SendCallback), client); receiveDone.WaitOne(); var response = state.receivedMessage; } } catch { // Ignore } }
public static void ReceiveMessages() { IPEndPoint e = new IPEndPoint(IPAddress.Any, 15000); UdpClient u; while (true) { try { // Receive a message and write it to the console. u = new UdpClient(e); break; } catch { Thread.Sleep(1000); } } UdpState s = new UdpState(); s.e = e; s.u = u; u.BeginReceive(new AsyncCallback(ReceiveCallback), s); }
public static void ReceiveCallback(IAsyncResult ar) { UdpState state = ar.AsyncState as UdpState; UdpClient u = state.u; IPEndPoint e = state.e; state.buffer = u.EndReceive(ar, ref e); state.receivedMessage = Encoding.ASCII.GetString(state.buffer); receiveDone.Set(); }
public static void ReceiveCallback(IAsyncResult ar) { UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u; IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e; Byte[] receiveBytes = u.EndReceive(ar, ref e); string receiveString = Encoding.ASCII.GetString(receiveBytes); UdpState s = new UdpState(); s.e = e; s.u = u; //u.Send(Encoding.UTF8.GetBytes(res.Output), res.Output.Length, e); u.BeginReceive(new AsyncCallback(ReceiveCallback), s); }
public static void ReceiveMessages(int listenPort) { // Receive a message and write it to the console. IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort); UdpClient u = new UdpClient(e); UdpState s = new UdpState(); s.e = e; s.u = u; Console.WriteLine("listening for messages"); u.BeginReceive(new AsyncCallback(ReceiveCallback), s); // Do some work while we wait for a message. For this example, // we'll just sleep while (!messageReceived) { Thread.Sleep(100); } }
static void Main(string[] args) { UdpClient u = new UdpClient(); try { IPAddress ipAddress = IPAddress.Parse("192.168.100.87"); IPEndPoint e = new IPEndPoint(ipAddress, 6600); UdpState s = new UdpState(); s.e = e; s.u = u; u.Connect(ipAddress, 6600); // Sends a message to the host to which you have connected. //Byte[] sendBytes = Encoding.ASCII.GetBytes(" Is anybody there?"); Byte[] sendBytes = { 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12 }; //string str; //byte[] vals = { 0x01, 0xAA, 0xB1, 0xDC, 0x10, 0xDD }; //for (byte i = 0; i < vals.Length/2; i++) //{ // str[i] = BitConverter.ToString(vals,i,2).Replace("-", ""); //} //string str = BitConverter.ToString(vals,0,2); //string str1 = vals.ToString(); //str = Convert.ToString((int)(vals[0] << 8 | vals[1]), 2).PadLeft(16,'0'); //str = str.PadLeft(16,'0'); //Console.WriteLine("DG_Cmd: "+ str); u.Send(sendBytes, sendBytes.Length); Console.SetWindowSize(60, 40); Console.WriteLine("\nMessage sent to port: 6600 IP:" + ipAddress + "\n 01,02,03,04,05,06,07,08,09,010,011,012"); u.BeginReceive(new AsyncCallback(ReceiveCallback), s); byte cnt = 0, cnt1 = 0; uint dt = 0; while (true) { Thread.Sleep(100); if (cnt > 100) { dt += (cnt - (cnt_read)); cnt = 0; cnt_read = 0; Console.Clear(); } if (messageReceived) { messageReceived = false; cnt1 = 0; /* * uint16_t DG_Cmd; //биты Команды ДГ * uint16_t DG_Un; //ДГ U * uint16_t DG_Fn; //ДГ F * uint16_t DG_res1; //ДГ рез * uint16_t GOM_Cmd; //биты Команды ГОМ * uint16_t GOM_Un; //ГОМ U * uint16_t GOM_Fn; //ГОМ F * uint16_t GOM_res1; //ГОМ рез * uint16_t SET_GOM_cmd; //СЕТЬ ГОМ команды * uint16_t SET_GOM_res1; //СЕТЬ ГОМ рез1 * uint16_t SET_GOM_res2; //СЕТЬ ГОМ рез2 * uint16_t SET_GOM_res3; //СЕТЬ ГОМ рез3 * uint16_t Indikator; //Индикация * uint16_t Ind_TOG; //Индикация ТОЖ * uint16_t Ind_PM; //Индикация ПМ * uint16_t Ind_Iload; //Индикация ТОК нагр * uint16_t Ind_Uload; //Индикация U нагр * uint16_t Ind_Iab; //Индикация ТОК ab * uint16_t Ind_Uab; //Индикация U ab * uint16_t Err_DG; //аварии ДГ * uint16_t Err_GOM; //аварии ГОМ * uint16_t Err_SET; //аварии сеть * uint16_t Ctrl_BTN; //button contrl */ Console.Write('\n'); Console.SetCursorPosition(0, 4); Console.WriteLine("DG_Cmd: {0}", str[0]); Console.WriteLine("DG_Un: {0}", str[1]); Console.WriteLine("DG_Fn: {0}", str[2]); Console.WriteLine("DG_res1: {0}", str[3]); Console.WriteLine("GOM_Cmd: {0}", str[4]); Console.WriteLine("GOM_Un: {0}", str[5]); Console.WriteLine("GOM_Fn: {0}", str[6]); Console.WriteLine("GOM_res1: {0}", str[7]); Console.WriteLine("SET_GOM_cmd: {0}", str[8]); Console.WriteLine("SET_GOM_res1: {0}", str[9]); Console.WriteLine("SET_GOM_res2: {0}", str[10]); Console.WriteLine("SET_GOM_res3: {0}", str[11]); Console.WriteLine("Indikator: {0}", str[12]); Console.WriteLine("Ind_TOG: {0}", str[13]); Console.WriteLine("Ind_PM: {0}", str[14]); Console.WriteLine("Ind_Iload: {0}", str[15]); Console.WriteLine("Ind_Uload: {0}", str[16]); Console.WriteLine("Ind_Iab: {0}", str[17]); Console.WriteLine("Ind_Uab: {0}", str[18]); Console.WriteLine("Err_DG: {0}", str[19]); Console.WriteLine("Err_GOM: {0}", str[20]); Console.WriteLine("Err_SET: {0}", str[21]); Console.WriteLine("Btn_Ctrl: {0}", str[22]); ++cnt_read; ++cnt; u.Send(sendBytes, sendBytes.Length); u.BeginReceive(new AsyncCallback(ReceiveCallback), s); } else { ++cnt1; if (cnt1 > 4) { cnt1 = 0; ++cnt; u.Send(sendBytes, sendBytes.Length); u.BeginReceive(new AsyncCallback(ReceiveCallback), s); } } Console.SetCursorPosition(0, 27); Console.WriteLine("\n\nОтправлено {0,3}, потеряно {1,3} раз", cnt, dt); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.ReadKey(true); u.Close(); }