using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace 显示用户ip { public partial class Form1 : Form { private static Socket socketUdp; private static IPEndPoint ip; private static string hostName; private static byte[] data; private static EndPoint ipe2; public Form1() { InitializeComponent(); socketBroadcast(); } private void socketBroadcast() { socketUdp = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); ip = new IPEndPoint(IPAddress.Broadcast, 6800); socketUdp.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); hostName = Dns.GetHostName(); string stringData = ":USER:" + hostName; data = Encoding.ASCII.GetBytes(stringData); Thread thread = new Thread(sendMessage); thread.Start(); receiveMessage(); } private void sendMessage() { while(true) { socketUdp.SendTo(data, ip); Thread.Sleep(5000); } } private void receiveMessage() { Socket socketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipe1 = new IPEndPoint(IPAddress.Any, 6800); ipe2 = ipe1; socketReceive.Bind(ipe1); while(true) { byte[] data1 = new byte[1024]; int recv = socketReceive.ReceiveFrom(data1, ref ipe2); string stringData = Encoding.ASCII.GetString(data1, 0, recv); string messageHead = stringData.Substring(0, 6); string messageBody = stringData.Substring(6); switch (messageHead) { case ":USER:": { string[] sBody = messageBody.Split(';'); listBox1.Items.Add(ipe2.ToString()); //不管他实现结果是什么,但是连winform窗体都出现不了。 break; } } //MessageBox.Show(string.Format("receive from:{0} ,ip is {1}", stringData, ipe2.ToString())); } } } }
1、这段代码请老司机看看,遇到了一个奇怪的问题,在循环里面是出现不了窗体的,为什么。我没有明白,没有弄循环就能窗体出来。
刚开始学,然后就想实现一下上线通知,下线通知这个,但是,却死活弄不出来。可以的话,老司机给给思路。谢谢。
2、还有很奇怪的问题,在思路理清的情况下,还有很多代码写不出来是不是很奇怪。基础不扎实吧
3、纯写一个即时通讯的软件,对于新手来说是不是最基本的,