以下是同步聊天程序的客户端源代码。其中的一些过程可以参考服务器端源码
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.threading;
using system.text;
namespace 聊天_socket_client
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.label label4;
private system.windows.forms.label label3;
private system.windows.forms.label label2;
private system.windows.forms.label label1;
private system.windows.forms.button btnstop;
private system.windows.forms.button btnsend;
private system.windows.forms.textbox txtport;
private system.windows.forms.textbox txtserver;
private system.windows.forms.richtextbox rtbsend;
private system.windows.forms.richtextbox rtbreceive;
private system.windows.forms.statusbar statusbar1;
private system.windows.forms.button btnconnect;
private ipaddress hostipaddress;
private ipendpoint server;
private socket sock;
/// <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.label4 = new system.windows.forms.label();
this.label3 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.label1 = new system.windows.forms.label();
this.btnstop = new system.windows.forms.button();
this.btnsend = new system.windows.forms.button();
this.btnconnect = new system.windows.forms.button();
this.txtport = new system.windows.forms.textbox();
this.txtserver = new system.windows.forms.textbox();
this.rtbsend = new system.windows.forms.richtextbox();
this.rtbreceive = new system.windows.forms.richtextbox();
this.statusbar1 = new system.windows.forms.statusbar();
this.suspendlayout();
//
// label4
//
this.label4.location = new system.drawing.point(16, 152);
this.label4.name = "label4";
this.label4.size = new system.drawing.size(64, 23);
this.label4.tabindex = 22;
this.label4.text = "发送信息:";
//
// label3
//
this.label3.location = new system.drawing.point(16, 64);
this.label3.name = "label3";
this.label3.size = new system.drawing.size(64, 23);
this.label3.tabindex = 21;
this.label3.text = "接收信息:";
//
// label2
//
this.label2.location = new system.drawing.point(216, 16);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(64, 23);
this.label2.tabindex = 20;
this.label2.text = "监听端口:";
//
// label1
//
this.label1.location = new system.drawing.point(16, 16);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(56, 23);
this.label1.tabindex = 19;
this.label1.text = "服务器:";
//
// btnstop
//
this.btnstop.location = new system.drawing.point(256, 256);
this.btnstop.name = "btnstop";
this.btnstop.tabindex = 18;
this.btnstop.text = "关闭连接";
this.btnstop.click += new system.eventhandler(this.btnstop_click);
//
// btnsend
//
this.btnsend.location = new system.drawing.point(144, 256);
this.btnsend.name = "btnsend";
this.btnsend.tabindex = 17;
this.btnsend.text = "发送信息";
this.btnsend.click += new system.eventhandler(this.btnsend_click);
//
// btnconnect
//
this.btnconnect.location = new system.drawing.point(32, 256);
this.btnconnect.name = "btnconnect";
this.btnconnect.tabindex = 16;
this.btnconnect.text = "请求连接";
this.btnconnect.click += new system.eventhandler(this.btnconnect_click);
//
// txtport
//
this.txtport.location = new system.drawing.point(288, 16);
this.txtport.name = "txtport";
this.txtport.size = new system.drawing.size(48, 21);
this.txtport.tabindex = 15;
this.txtport.text = "19811";
//
// txtserver
//
this.txtserver.location = new system.drawing.point(72, 16);
this.txtserver.name = "txtserver";
this.txtserver.tabindex = 14;
this.txtserver.text = "127.0.0.1";
//
// rtbsend
//
this.rtbsend.location = new system.drawing.point(80, 152);
this.rtbsend.name = "rtbsend";
this.rtbsend.size = new system.drawing.size(264, 96);
this.rtbsend.tabindex = 13;
this.rtbsend.text = "";
//
// rtbreceive
//
this.rtbreceive.location = new system.drawing.point(80, 56);
this.rtbreceive.name = "rtbreceive";
this.rtbreceive.size = new system.drawing.size(264, 96);
this.rtbreceive.tabindex = 12;
this.rtbreceive.text = "";
//
// statusbar1
//
this.statusbar1.location = new system.drawing.point(0, 287);
this.statusbar1.name = "statusbar1";
this.statusbar1.showpanels = true;
this.statusbar1.size = new system.drawing.size(360, 22);
this.statusbar1.tabindex = 23;
this.statusbar1.text = "statusbar1";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(360, 309);
this.controls.add(this.statusbar1);
this.controls.add(this.label4);
this.controls.add(this.label3);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.controls.add(this.btnstop);
this.controls.add(this.btnsend);
this.controls.add(this.btnconnect);
this.controls.add(this.txtport);
this.controls.add(this.txtserver);
this.controls.add(this.rtbsend);
this.controls.add(this.rtbreceive);
this.name = "form1";
this.text = "聊天程序-客户端";
this.closing += new system.componentmodel.canceleventhandler(this.form1_closing);
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void btnconnect_click(object sender, system.eventargs e)
{
try
{
hostipaddress=ipaddress.parse(txtserver.text);
}
catch{messagebox.show("请输入正确的ip地址格式。");}
try
{
server=new ipendpoint(hostipaddress,int32.parse(txtport.text));
sock=new socket(addressfamily.internetwork,sockettype.stream,protocoltype.tcp);
sock.connect(server);
statusbar1.text="与主机"+txtserver.text+"端口"+txtport.text+"连接成功";
thread thread=new thread(new threadstart(threadproc));
thread.start();
}
catch(exception ee){messagebox.show(ee.message);}
}
private void threadproc()
{
while(true)
{
byte[] byterecv=new byte[256];
sock.receive(byterecv,byterecv.length,0);
string strrecv=encoding.bigendianunicode.getstring(byterecv);
rtbreceive.appendtext(strrecv+"\r\n");
}
}
private void btnsend_click(object sender, system.eventargs e)
{
try
{
string strsend ="client—>"+rtbsend.text+"\r\n";
byte[] bytesend = encoding.bigendianunicode.getbytes(strsend);
sock.send(bytesend,bytesend.length,0);
}
catch{messagebox.show("连接尚未建立,无法发送.");}
}
private void btnstop_click(object sender, system.eventargs e)
{
try
{
sock.close();
statusbar1.text="与主机"+txtserver.text+"端口"+txtport.text+"断开连接";
}
catch{messagebox.show("连接尚未建立,关闭无效");}
}
private void form1_closing(object sender, system.componentmodel.canceleventargs e)
{
try
{
sock.close();
}
catch{}
}
}
}