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聊。我好想弄懂这个问题
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聊。我好想弄懂这个问题
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货