我想写一个监听具体一个端口,并显示获取到的内容,可是一运行连窗口都不见了,下面附具体函数:
public void ListerPort()
{ int Port = 8000;
// string host = "127.0.0.1";
string[] getArr = new string[2];
UdpClient Listener = new UdpClient(Port);
IPEndPoint cleint = new IPEndPoint(IPAddress.Any, Port);
try
{
byte[] bytes = Listener.Receive(ref cleint);
getArr[0] = cleint.Address.ToString();
getArr[1] = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
textBox1.Text = textBox1.Text + getArr[0] + ":" + getArr[1] + "\b";
}
catch (Exception er)
{
textBox1.Text = er.ToString();
}
}
public void ListerPort()
{ int Port = 8000;
// string host = "127.0.0.1";
string[] getArr = new string[2];
UdpClient Listener = new UdpClient(Port);
IPEndPoint cleint = new IPEndPoint(IPAddress.Any, Port);
try
{
byte[] bytes = Listener.Receive(ref cleint);
getArr[0] = cleint.Address.ToString();
getArr[1] = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
textBox1.Text = textBox1.Text + getArr[0] + ":" + getArr[1] + "\b";
}
catch (Exception er)
{
textBox1.Text = er.ToString();
}
}
private void ReceiveData()
{
//在本机指定的端口接收
udpClient = new UdpClient(port);
IPEndPoint remote = null;
//接收从远程主机发送过来的信息;
while (true)
{
try
{
//关闭udpClient时此句会产生异常
byte[] bytes = udpClient.Receive(ref remote);
string str = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
AddListBoxItem(string.Format("来自{0}:{1}", remote, str));
}
catch
{
//退出循环,结束线程
break;
}
}
}你参考一下吧
private void Form1_Load(object sender, EventArgs e)
{ ListerPort(); //Thread myThread = new Thread(new ThreadStart(ListerPort));
// myThread.Start();
} public void ListerPort()
{ int Port = 80;
string[] getArr = new string[2];
UdpClient Listener = new UdpClient(Port);
IPEndPoint cleint = new IPEndPoint(IPAddress.Any, Port);
textBox1.Text = "开始了.....";
try
{
while (true)
{
byte[] bytes = Listener.Receive(ref cleint);
getArr[0] = cleint.Address.ToString();
getArr[1] = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
textBox1.Text = textBox1.Text + getArr[0] + ":" + getArr[1] + "\b";
}
}
catch (Exception er)
{
textBox1.Text = er.ToString();
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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;
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(textBox3.Text);
UdpClient Udp2 = new UdpClient(x); //对自己的端口进行侦听
IPEndPoint RemotePoint = new IPEndPoint(IPAddress.Any, 0);
//定义IPEndPoint类的实例RemotePoint,该类能将IP地址和端口捆绑于一体
while (listenstate) //循环扫描(侦听)端口
{
try //捕捉异常
{
Byte[] receiveBytes = Udp2.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中
}
catch (Exception ex) //如果接受数据有异常或者出错,将出错信息显示出在listBox2中
{
listBox1.Items.Add(ex.Message + " 消息接受失败!!!");
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
}
} private void button1_Click(object sender, EventArgs e)
{
if (textBox3.Text != "")
{
myThread.Start();
button1.Enabled = false;
}
else
{
MessageBox.Show("不能为空!");
}
} private void button2_Click(object sender, EventArgs e)
{
if (textBox4.Text != "")
{
int y = Convert.ToInt32(textBox2.Text);
UdpClient udp1 = new UdpClient(textBox1.Text, y);//确定要发送消息的远程机器
Byte[] sendBytes = Encoding.UTF8.GetBytes(textBox4.Text);
//将待发送的消息编码存储到数组(缓冲区)sendBytes中
udp1.Send(sendBytes, sendBytes.Length); //向对方发送消息
//将发送的数据在自己窗体中的listBox1中显示 ,Dns.GetHostName()得到本机的主机名称
listBox1.Items.Add(DateTime.Now + " " + Dns.GetHostName() + " 说: ");
listBox1.Items.Add(textBox4.Text);
textBox4.Text = "";
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
else
{ MessageBox.Show("不能发送空信息!");
}
}
}
}