TcpListener与TcpClient通讯程序.docx
《TcpListener与TcpClient通讯程序.docx》由会员分享,可在线阅读,更多相关《TcpListener与TcpClient通讯程序.docx(11页珍藏版)》请在冰点文库上搜索。
TcpListener与TcpClient通讯程序
TcpListener与TcpClient通讯程序
TcpListener与TcpClient通讯程序
分类:
c#学习笔记|标签:
构造通讯设计代码stem
2007-03-0910:
10阅读(492)评论(0)
TcpListener服务器:
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Text;
usingSystem.Threading;
namespaceTcp
{
///<summary>
///Form1的摘要说明。
///</summary>
publicclassForm1:
System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.TextBoxtextBoxPort;
privateSystem.Windows.Forms.GroupBoxgroupBox1;
privateSystem.Windows.Forms.RichTextBoxrichTextBoxOut;
privateSystem.Windows.Forms.GroupBoxgroupBox2;
privateSystem.Windows.Forms.RichTextBoxrichTextBoxInput;
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.StatusBarstatusBar1;
privateSystem.Windows.Forms.StatusBarPanelstatusBarPanel1;
privateSystem.Windows.Forms.ButtonbutDisconn;
privateSystem.Windows.Forms.ButtonbutSendMsg;
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
privateTcpListenertcplisten;
privateSocketconnSocket;
privateboolcheck=true;
privateNetworkStreamnetStream;
publicForm1()
{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
//
//TODO:
在InitializeComponent调用后添加任何构造函数代码
//
}
///<summary>
///清理所有正在使用的资源。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
.........windows窗体设计器生成代码
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
{
try
{
intport=System.Convert.ToInt32(this.textBoxPort.Text);
tcplisten=newTcpListener(port);
tcplisten.Start();
this.statusBarPanel1.Text="现处于待机状态......";
Threadthread=newThread(newThreadStart(ReceMsg));
thread.Start();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
this.button1.Enabled=false;
this.butDisconn.Enabled=true;
this.butSendMsg.Enabled=true;
}
privatevoidReceMsg()
{
while(true)
{
connSocket=tcplisten.AcceptSocket();
if(connSocket.Connected)
{
this.statusBarPanel1.Text="与客户建立连接!
";
Threadthreadread=newThread(newThreadStart(round));
threadread.Start();
}
}
}
privatevoidround()
{
while(check)
{
byte[]read=newbyte[1024];
netStream=newNetworkStream(connSocket);
netStream.Read(read,0,read.Length);
stringRecemsg=Encoding.Unicode.GetString(read);
this.richTextBoxOut.AppendText(Recemsg+"\r\n");
}
}
privatevoidbutDisconn_Click(objectsender,System.EventArgse)
{
try
{
connSocket.Close();
this.tcplisten.Stop();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
this.button1.Enabled=true;
this.butDisconn.Enabled=false;
this.butSendMsg.Enabled=false;
}
privatevoidbutSendMsg_Click(objectsender,System.EventArgse)
{
byte[]write=newbyte[1024];
netStream=newNetworkStream(connSocket);
stringSendmsg=this.richTextBoxInput.Text+"\r\n";
write=Encoding.Unicode.GetBytes(Sendmsg.ToCharArray());
netStream.Write(write,0,write.Length);
netStream.Flush();
}
}
}
TcpClient客户端;
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Text;
usingSystem.Threading;
namespacetcpc
{
///<summary>
///Form1的摘要说明。
///</summary>
publicclassForm1:
System.Windows.Forms.Form
{
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.TextBoxtextBoxip;
privateSystem.Windows.Forms.Labellabel2;
privateSystem.Windows.Forms.TextBoxtextBoxport;
privateSystem.Windows.Forms.GroupBoxgroupBox1;
privateSystem.Windows.Forms.GroupBoxgroupBox2;
privateSystem.Windows.Forms.ButtonbutConn;
privateSystem.Windows.Forms.ButtonbutDisconn;
privateSystem.Windows.Forms.ButtonbutSendMsg;
privateSystem.Windows.Forms.StatusBarstatusBar1;
privateSystem.Windows.Forms.StatusBarPanelstatusBarPanel1;
privateSystem.Windows.Forms.RichTextBoxrichTextBoxout;
privateSystem.Windows.Forms.RichTextBoxrichTextBoxinput;
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
privateboolcheck=true;
privateTcpClienttclient;
privateNetworkStreamnetStream;
publicForm1()
{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();
//
//TODO:
在InitializeComponent调用后添加任何构造函数代码
//
}
///<summary>
///清理所有正在使用的资源。
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!
=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
........windows窗体设计器生成代码
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}
privatevoidbutConn_Click(objectsender,System.EventArgse)
{
try
{
intport=System.Convert.ToInt32(this.textBoxport.Text,10);
tclient=newTcpClient();
tclient.Connect(this.textBoxip.Text,port);
Threadthreadread=newThread(newThreadStart(ReceMsg));
threadread.Start();
}
catch
{
MessageBox.Show("无法连接!
");
}
this.butConn.Enabled=false;
this.butDisconn.Enabled=true;
this.butSendMsg.Enabled=true;
}
privatevoidReceMsg()
{
while(check)
{
byte[]read=newbyte[1024];
netStream=tclient.GetStream();
netStream.Read(read,0,read.Length);
stringRecemsg=Encoding.Unicode.GetString(read);
this.richTextBoxout.AppendText(Recemsg+"\r\n");
}
}
privatevoidbutDisconn_Click(objectsender,System.EventArgse)
{
try
{
tclient.Close();
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
this.butConn.Enabled=true;
this.butDisconn.Enabled=false;
this.butSendMsg.Enabled=false;
}
privatevoidbutSendMsg_Click(objectsender,System.EventArgse)
{
byte[]write=newbyte[1024];
netStream=tclient.GetStream();
stringSendmsg=this.richTextBoxinput.Text+"\r\n";
write=Encoding.Unicode.GetBytes(Sendmsg.ToCharArray());
netStream.Write(write,0,write.Length);
netStream.Flush();
}
}
}