时间:2021-07-01 10:21:17 帮助过:12人阅读
using System;using System.Collections.Generic;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;namespace monitorFlowworkAndSubmit.DAL{ class SendSocket { string MYOA_TDIM_ADDR = "127.0.0.1"; int MYOA_TDIM_PORT = xxxx; public string Send() { string rst = ""; //设定服务器IP地址 IPAddress ip = IPAddress.Parse(MYOA_TDIM_ADDR); Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { clientSocket.Connect(new IPEndPoint(ip, MYOA_TDIM_PORT)); //配置服务器IP与端口 Console.WriteLine("连接服务器成功"); } catch (Exception ex) { rst = "连接服务器失败,请按回车键退出!"; return ex.ToString(); } try { string sendMessage = "x^a^admin"; clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage)); Console.WriteLine("向服务器发送消息:{0}" + sendMessage); } catch { clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); return "sed err"; } return "OK"; } }}