这个是执行窗体using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace 客户端
{
class 服务器
{
//声明一个int类型常量存放端口号
private const int listenPort = 11000;
public string strInfo=null;
public bool boo = true;
public void StartListener()
{
//实例化UdpClient类,接收值为端口号
UdpClient listener = new UdpClient(listenPort);
//实例化类IPEndPoint,用listenPort的端口来接收来自所有ip的信息
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
//写一个死循环监控
while (boo)
{
//声明一个byte类型数组,存放接收过来的值。
byte[] bytes = listener.Receive(ref groupEP); //ref 关键字使参数按引用传递
//获得发信人的IP
string strIP = "信息来自" + groupEP.Address.ToString();
//获得信息通过gb2312编码转化为汉字
strInfo = Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length); //return strInfo;
//MessageBox.Show(strInfo, strIP);
}
}
catch (Exception e)
{
strInfo = e.ToString();
//return e.ToString();
} finally
{
listener.Close();
}
}
}
}
这是服务器类代码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.Sockets;
using System.Net;
using System.Threading;
namespace 客户端
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thread = null;
private Thread thread1 = null;
public void Send()
{
//实例化Socket对象,指定 Socket 类的实例可以使用的寻址方案,Socket 类的实例类型以及协议类型
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //用IPAddress类型的broadcast来接收文本框中的ip
IPAddress broadcast = IPAddress.Parse(textBox1.Text); //声明一个byte类型数组,存放要发送的值。通过gb2312编码转换为特殊字符
byte[] sendbuf = Encoding.GetEncoding("gb2312").GetBytes(this.textBox2.Text.ToString()); //实例化类IPEndPoint,指定ip地址以及端口号 IPEndPoint ep = new IPEndPoint(broadcast, 11000); //发送信息
s.SendTo(sendbuf, ep);
} private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text != "")
{
Send();
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + textBox2.Text + "\r\n";
textBox2.Clear();
}
else
{
MessageBox.Show("输入内容为空!");
}
}
服务器 fwq = new 服务器(); private void Form1_Load(object sender, EventArgs e)
{
//获取本机ip
textBox1.Text = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
thread = new Thread(new ThreadStart(fwq.StartListener));
thread.Start();
thread1 = new Thread(new ThreadStart(jiankong));
thread1.Start();
//textBox1.Text= Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
//MessageBox.Show(fwq.StartListener());
} private void button2_Click(object sender, EventArgs e)
{
if (fwq.strInfo != null)
{
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + fwq.strInfo + "\r\n";
} } private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Application.ExitThread();
fwq.boo = false;
thread.Abort();
thread1.Abort();
this.Dispose();
}
string val = null;
public delegate void SetTextHandler(string text); public void jiankong()
{
bool b = true;
while (b)
{
if (fwq.strInfo != null&&fwq.strInfo!=val)
{
if (textBox3.InvokeRequired)
{
SetTextHandler d = new SetTextHandler(SetText);
//this.invoke(d, new object[] { fwq.strInfo });
this.Invoke(d, new object[] { fwq.strInfo });
val = fwq.strInfo;
}
else
{
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + fwq.strInfo + "\r\n";
fwq.strInfo = null;
val = fwq.strInfo;
} }
}
}
void SetText(string zhi)
{
textBox3.Text += Convert.ToString(DateTime.Now) + "\r\n" + fwq.strInfo + "\r\n";
}
}
}
这个是窗体代码
谢谢各位朋友。
解决方案 »
- asp.NET,设置cookies失效时间不起作用,关闭浏览器就自动失效了??
- 想自己开发一个TEXTBOX得控件,遇到了问题
- [WPF]怎么在样式里定义Grid的行列
- web应用中和remoting的结合?
- 包含定长字符串的结构体的定义,奇怪的现象
- StringBuilder要怎么回收?
- fastreport连续打印条码,中间会停顿下,请问如何解决
- 大虾帮我,我实在是看不出下面的程序有什么问题呀
- 能否在GridView中添加一列全是TextBox列?
- 各位谁做过日历控件??
- 关于asp.net系统登录页面点击登陆后屏蔽浏览器弹出“记住密码”
- win7 中把文字大小设到中等(125%)后vs2010显示不正常的问题。
有时候有别的线程正在运行没有结束也会导致这种情况
System.Environment.Exit(0); //结束进程时,关闭所有线程试试看
Environment.Exit(0);