110,499
社区成员
发帖
与我相关
我的任务
分享
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";
}
}
}
这个是窗体代码