??? public partial class ceshiForm2 : Form ??? { ??????? public ceshiForm2() ??????? { ??????????? InitializeComponent(); ??????? } ??????? IPAddress ip; ??????? IPEndPoint point = new IPEndPoint(IPAddress.Any, 0); ??????? string protocol; ??????? Socket aimSocket; ??????? Socket mySocket; ??????? UdpClient RecUdpClient;
??????? private void ceshiForm2_Load(object sender, EventArgs e) ??????? { ??????????? Control.CheckForIllegalCrossThreadCalls = false; ??????????? aimSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//建立通信的Socket ??????????? ip = IPAddress.Parse("10.0.20.11"); ??????????? point = new IPEndPoint(ip, Convert.ToInt32(51201)); ??????????? MessageBox.Show("UDP Client设置成功"); ??????????? Thread thReceive = new Thread(UCReceive); ??????????? thReceive.IsBackground = true; ??????????? thReceive.Start(); ??????? } ??????? //UDP Clinet接收线程 ??????? void UCReceive() ??????? { ??????????? byte[] buffer = new byte[1024]; ??????????? while (true) ??????????? { ??????????????? try ??????????????? { ??????????????????? IPEndPoint ss = new IPEndPoint(IPAddress.Any, 0); ??????????????????? EndPoint Remote = (EndPoint)(ss); ??????????????????? int r = aimSocket.ReceiveFrom(buffer, ref Remote);//接收目标IP Remote 发送过来的数 ??????????????????? string strRec = Encoding.Default.GetString(buffer, 0, r); ??????????????????? MessageBox.Show(Remote.ToString() + ": " + strRec + "\r\n"); ??????????????? } ??????????????? catch ??????????????? { } ??????????? } ??????? }
??????? private void btnPlaySound_Click(object sender, EventArgs e) ??????? { ??????????? try ??????????? { ??????????????? byte[] buffer = Encoding.Default.GetBytes("TEXT_PLAY; 控制,1234;2;"+txtSoundContent.Text); ??????????????? aimSocket.SendTo(buffer, point); ??????????????? MessageBox.Show("发送"); ??????????? } ??????????? catch ??????????? { } ??????? } ??? }
|