TcpListener与TcpClient通讯程序.docx

上传人:b****2 文档编号:517265 上传时间:2023-04-29 格式:DOCX 页数:11 大小:15.54KB
下载 相关 举报
TcpListener与TcpClient通讯程序.docx_第1页
第1页 / 共11页
TcpListener与TcpClient通讯程序.docx_第2页
第2页 / 共11页
TcpListener与TcpClient通讯程序.docx_第3页
第3页 / 共11页
TcpListener与TcpClient通讯程序.docx_第4页
第4页 / 共11页
TcpListener与TcpClient通讯程序.docx_第5页
第5页 / 共11页
TcpListener与TcpClient通讯程序.docx_第6页
第6页 / 共11页
TcpListener与TcpClient通讯程序.docx_第7页
第7页 / 共11页
TcpListener与TcpClient通讯程序.docx_第8页
第8页 / 共11页
TcpListener与TcpClient通讯程序.docx_第9页
第9页 / 共11页
TcpListener与TcpClient通讯程序.docx_第10页
第10页 / 共11页
TcpListener与TcpClient通讯程序.docx_第11页
第11页 / 共11页
亲,该文档总共11页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

TcpListener与TcpClient通讯程序.docx

《TcpListener与TcpClient通讯程序.docx》由会员分享,可在线阅读,更多相关《TcpListener与TcpClient通讯程序.docx(11页珍藏版)》请在冰点文库上搜索。

TcpListener与TcpClient通讯程序.docx

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();

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2