当前位置:编程学习 > C#/ASP.NET >>

求大神帮忙看看我的程序那里错了,(实现监听QQ接收邮件的功能)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace Udp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Thread myThread;
      public  bool listenstate = true;

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
           // myThread = new Thread(new ThreadStart(listen));
        }
        private void listen()
        {
            int x = Convert.ToInt32(textBox1.Text);
            UdpClient ud = new UdpClient(x);//对本地端口进行侦听
            IPEndPoint RemotePoint = new IPEndPoint(IPAddress.Any,0);//创建Socket实例

            while (listenstate)//循环扫描(侦听)端口
            {
                try//捕捉异常
                {

                    Byte[] receiveBytes = ud.Receive(ref RemotePoint);//获取接受到的信息,存放到字节数据(缓冲区)receiveBytes中
                    string ipaddress = RemotePoint.Address.ToString();//获得发送方的IP地址存储到字符串ipaddress中
                    string megstr = Encoding.UTF8.GetString(receiveBytes); //将收到的数据解码为字符串,存储到字符串megstr中 

                    //listBox1.Items.Add(DateTime.Now + "" + ipaddress + "说:");
                    //listBox1.Items.Add(megstr);

                    //listBox1.SelectedIndex = listBox1.Items.Count - 1; //将收到的数据显示在本窗体的listBox2中 


                    if (megstr != "")
                    {

                        MessageBox.Show("您有新邮件");
                    }

                }
                catch (Exception ex)
                {
                    //listBox1.Items.Add(ex.Message + "消息接受失败!");
                    //listBox1.SelectedIndex = listBox1.Items.Count - 1;
                    //listenstate = false;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                myThread = new Thread(new ThreadStart(listen));
                myThread.Start();//运行线程
                button1.Enabled = false;
            }
            else
            {
                MessageBox.Show("不能为空!");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

      
    }
}求帮忙看看了好心人,会的加我Q328127602聊。我好想弄懂这个问题 --------------------编程问答-------------------- 坐等好心人帮忙看看程序,指导一下。多写 --------------------编程问答-------------------- 从深入的技术入手。你提出什么深度的问题,决定了你能得到什么深度的答案。不要摆出空壳子就想着套人家的实用的答案。 --------------------编程问答-------------------- 没问题啊,报什么异常
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,