udp在窗体上的显示遇到的问题

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、纯写一个即时通讯的软件,对于新手来说是不是最基本的,

转载于:https://www.cnblogs.com/wesley2018/p/5848604.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值