本通讯程序没有服务端和客户端之分,局域网的计算机运行同一程序即可通信。
由于水平有限,目前版本还很菜,只可以实现基本的聊天功能和显示在线用户功能。
准备添加传送文件、聊天日志、可选参数等功能。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.net;
using system.net.sockets;
using system.text;
using system.threading;
namespace myqq
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
public bool runing=false; //标志
public udpclient listen=new udpclient(2525);
public ipendpoint end;
public ipaddress groupaddress=ipaddress.parse("255.255.255.255"); //广播地址
public int groupport=2525; //广播端口
public string machinename;
public string machineip;
public string romename;
public string romeip;
public string romecon;
private system.windows.forms.listbox box;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
private system.windows.forms.button button1;
private system.windows.forms.button button3;
private system.windows.forms.label label3;
private system.windows.forms.textbox t_rec;
private system.windows.forms.textbox t_send;
private system.windows.forms.button button2;
private system.windows.forms.button newmsg;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.box = new system.windows.forms.listbox();
this.t_rec = new system.windows.forms.textbox();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.t_send = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.label3 = new system.windows.forms.label();
this.button2 = new system.windows.forms.button();
this.newmsg = new system.windows.forms.button();
this.suspendlayout();
//
// box
//
this.box.itemheight = 12;
this.box.location = new system.drawing.point(0, 32);
this.box.name = "box";
this.box.size = new system.drawing.size(200, 352);
this.box.tabindex = 0;
//
// t_rec
//
this.t_rec.backcolor = system.drawing.systemcolors.inactiveborder;
this.t_rec.forecolor = system.drawing.color.fromargb(((system.byte)(192)), ((system.byte)(64)), ((system.byte)(0)));
this.t_rec.location = new system.drawing.point(208, 56);
this.t_rec.multiline = true;
this.t_rec.name = "t_rec";
this.t_rec.scrollbars = system.windows.forms.scrollbars.vertical;
this.t_rec.size = new system.drawing.size(328, 136);
this.t_rec.tabindex = 1;
this.t_rec.text = "";
//
// label1
//
this.label1.location = new system.drawing.point(208, 40);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(120, 16);
this.label1.tabindex = 2;
this.label1.text = "接收:";
//
// label2
//
this.label2.location = new system.drawing.point(208, 200);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(120, 16);
this.label2.tabindex = 2;
this.label2.text = "发送:";
//
// t_send
//
this.t_send.location = new system.drawing.point(208, 216);
this.t_send.multiline = true;
this.t_send.name = "t_send";
this.t_send.scrollbars = system.windows.forms.scrollbars.vertical;
this.t_send.size = new system.drawing.size(328, 112);
this.t_send.tabindex = 1;
this.t_send.text = "";
//
// button1
//
this.button1.location = new system.drawing.point(440, 344);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(75, 32);
this.button1.tabindex = 3;
this.button1.text = "发 送";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button3
//
this.button3.flatstyle = system.windows.forms.flatstyle.popup;
this.button3.location = new system.drawing.point(88, 3);
this.button3.name = "button3";
this.button3.size = new system.drawing.size(56, 23);
this.button3.tabindex = 4;
this.button3.text = "刷 新";
this.button3.click += new system.eventhandler(this.button3_click);
//
// label3
//
this.label3.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.point, ((system.byte)(134)));
this.label3.forecolor = system.drawing.color.blue;
this.label3.location = new system.drawing.point(8, 8);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(80, 16);
this.label3.tabindex = 5;
this.label3.text = "在线用户:";
//
// button2
//
this.button2.location = new system.drawing.point(360, 344);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(75, 32);
this.button2.tabindex = 6;
this.button2.text = "清 空";
this.button2.click += new system.eventhandler(this.button2_click);
//
// newmsg
//
this.newmsg.location = new system.drawing.point(208, 56);
this.newmsg.name = "newmsg";
this.newmsg.size = new system.drawing.size(328, 136);
this.newmsg.tabindex = 7;
this.newmsg.text = "有新消息了!";
this.newmsg.click += new system.eventhandler(this.newmsg_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(544, 389);
this.controls.add(this.button2);
this.controls.add(this.label3);
this.controls.add(this.button3);
this.controls.add(this.button1);
this.controls.add(this.label1);
this.controls.add(this.box);
this.controls.add(this.label2);
this.controls.add(this.t_send);
this.controls.add(this.t_rec);
this.controls.add(this.newmsg);
this.name = "form1";
this.text = "小y";
this.closing += new system.componentmodel.canceleventhandler(this.form1_closing);
this.load += new system.eventhandler(this.form1_load);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void form1_load(object sender, system.eventargs e)
{
end=new ipendpoint(groupaddress,groupport);
//初始化计算机名和端口
iphostentry myentry=dns.gethostbyname(dns.gethostname());
ipaddress myaddress=new ipaddress(myentry.addresslist[0].address);
machinename=dns.gethostname();
machineip=myaddress.tostring();
box.items.add("ip 主机名");
//开启监听线程
runing=true;
thread mythread=new thread(new threadstart(this.listenport));
mythread.start();
//发送上线信息
string sends="0&"+machineip+"&"+machinename+"&"+machineip;
sendpack(sends);
}
//侦听指定端口的广播地址udp包
public void listenport()
{
ipendpoint tempend=new ipendpoint(ipaddress.any,2525);
while(runing)
{
application.doevents();
try
{
byte[] recb=listen.receive(ref tempend);
// 检查所接收到的信息
checkmessage(recb);
}
catch(exception e)
{
messagebox.show(e.message.tostring());
break;
}
}
listen.close();
box.items.add("线程已经退出!");
runing=false;
}
//循环接收包
public void checkmessage(byte[] recbb)
{
string recstr=encoding.default.getstring(recbb);
string[] rec=recstr.split('&');
switch(rec[0])
{
case "0": //刷新
if(rec[3]==machineip)
{
if(box.findstring(rec[1]+" "+rec[2])<=0)
box.items.add(rec[1]+" "+rec[2]);
}
else if(rec[1]==rec[3])
{
//从其他机器发送过来的刷新请求
//返回自己的信息
string sendstr="0&"+machineip+"&"+machinename+"&"+rec[3];
this.sendpack(sendstr);
//如果不存在则添加该用户
if(box.findstring(rec[1]+" "+rec[2])<=0)
box.items.add(rec[1]+" "+rec[2]);
}
break;
case "1": //发言
if(rec[3]==machineip)
{
romename=rec[2];
romeip=rec[1];
romecon=rec[4];
showne();
}
break;
case "2": //传文件
break;
case "9": //确认包
if(rec[3]==machineip)
messagebox.show("信息来自:rec[2]("+rec[1]+")/r/n"+rec[4],"消息");
break;
}
}
//发送数据包到广播地址
public void sendpack(string sendstr)
{
byte[] sendb=encoding.default.getbytes(sendstr);
try
{
listen.send(sendb,sendb.length,end);
}
catch(exception e)
{
messagebox.show(e.message.tostring());
}
}
//刷新
private void button3_click(object sender, system.eventargs e)
{
box.items.clear();
box.items.add("ip 主机名");
string temp2="0&"+machineip+"&"+machinename+"&"+machineip;
this.sendpack(temp2);
}
//关闭循环
private void form1_closing(object sender, system.componentmodel.canceleventargs e)
{
runing=false;
udpclient mm=new udpclient();
ipendpoint tempipend=new ipendpoint(ipaddress.parse("127.0.0.1"),2525);
string temps="0&0&0&0&0";
byte[] sendb=encoding.default.getbytes(temps);
mm.send(sendb,sendb.length,tempipend);
}
//发送
private void button1_click(object sender, system.eventargs e)
{
if(t_send.text==null)
{
messagebox.show("不能发送空信息!");
return;
}
if(box.selecteditem==null||box.selectedindex==0)
{
messagebox.show("请先选择一个用户!");
return;
}
string send="1&"+machineip+"&"+machinename+"&"+box.selecteditem.tostring().split(' ')[0]+"&"+t_send.text;
this.sendpack(send);
}
//清空
private void button2_click(object sender, system.eventargs e)
{
t_send.text=string.empty;
t_rec.text=string.empty;
}
//单击显示接收的信息并隐藏自身
private void newmsg_click(object sender, system.eventargs e)
{
t_rec.text="消息来自"+romename+"("+romeip+")"+datetime.now.toshortdatestring();
t_rec.text+="/r/n> "+romecon;
string back="9&"+machineip+"&"+machinename+"&"+romeip+"&信息被打开!";
this.sendpack(back);
newmsg.sendtoback();
}
//当有信息时显示提示按钮
public void showne()
{
//窗体显示
if(this.windowstate!=formwindowstate.normal)
{
this.visible=true;
this.windowstate=formwindowstate.normal;
}
newmsg.bringtofront();
}
}
}
,欢迎访问网页设计爱好者web开发。
新闻热点
疑难解答