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.Net;
using System.Net.Sockets;
using System.Threading;namespace 聊天__qq群
{
public partial class Form1 : Form
{ bool done = false;
public Form1()
{
InitializeComponent();
}
private void StartListener()
{
UdpClient listener = new UdpClient(int.Parse(textBox1.Text.ToString().Trim())); //使用UDP协议
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(textBox1.Text.ToString().Trim())); //任意IP,
try
{
while (!done)//使用永真循环另其一直处于监听状态
{ byte[] bytes = listener.Receive(ref groupEP);
string strIP;
strIP = "信息来自" + groupEP.Address.ToString();//获得发信人的IP
string strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);//获得信息
MessageBox.Show(strInfo, strIP);
}
}
catch (Exception e)
{ Console.WriteLine(e.ToString()); }
finally { listener.Close(); } }
private void Form1_Load(object sender, EventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
StartListener(); //调用监听方法
}
}
}
C#一直监听
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.Net;
using System.Net.Sockets;
using System.Threading;namespace 聊天__qq群
{
public partial class Form1 : Form
{ bool done = false;
public Form1()
{
InitializeComponent();
}
private void StartListener()
{
UdpClient listener = new UdpClient(int.Parse(textBox1.Text.ToString().Trim())); //使用UDP协议
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, int.Parse(textBox1.Text.ToString().Trim())); //任意IP,
try
{
while (!done)//使用永真循环另其一直处于监听状态
{ byte[] bytes = listener.Receive(ref groupEP);
string strIP;
strIP = "信息来自" + groupEP.Address.ToString();//获得发信人的IP
string strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);//获得信息
MessageBox.Show(strInfo, strIP);
}
}
catch (Exception e)
{ Console.WriteLine(e.ToString()); }
finally { listener.Close(); } }
private void Form1_Load(object sender, EventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
StartListener(); //调用监听方法
}
}
}
C#一直监听
解决方案 »
- 想问TabControl控件的切换原理
- WPF程序 怎么用FTP下载文件到指定位置
- 请问有没有比较好的C# winform编程的书.
- 开始使用ADO的问题
- 求救!在线等!!!
- 新闻发布系统有个show页面怎无法正常运行!
- 求解:winform下下载文件的进度条如何做?up者有分!
- 界面组件
- 急!请教如何在C#中对串口进行开发!使用MSCOMM控件好象有问题!谢谢!!
- 读取文件的问题?
- datagridview綁定datatable后在定時器timer里給datatable添加列為何datagridview無法顯示?原來顯示頁面也崩潰了
- 请教ShellExcute调用WinRar不能正常解压的问题
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(StartListener, null); //调用监听方法
}好像是楼上的意思
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(StartListener, null); //调用监听方法
}好像是楼上的意思 不行啊 运行出现错误了
ThreadPool.QueueUserWorkItem(StartListener, null);替换StartListener();
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
的错误啊
你在主线程上使用死循环肯定会导致窗体卡死。
建议换成Timer控件,或使用另外一个线程执行。
这个参数下面要写什么东西吗
这个参数下面要写什么东西吗 谢谢啊 解决了 谢谢大神
这个参数下面要写什么东西吗
你可以不用你那个方法加个参数private void StartListener(object obj)
这个参数下面要写什么东西吗参数你可以不用,但是一定要写。public delegate void WaitCallback(object state);