版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、c# tcp调试助手源代码以下是winform tcp调试助手源代码,调试通过,可进行简单应用usi ngSystem;usi ngusi ngSystem.Co mponen tModel;usi ngSystem.Data;usi ngSystem.Drawi ng;usi ngSystem.Li nq;usi ngSystem.Text;usi ng/添?加6新?的?名?称?空?间?引皔用?usi ngSystem.IO;usi ngSystem.Net;usi ngusi ngSystem.Thread ing;/us ing System.Timers;names paceTCP 调
2、獭?试?助u手?p ublic p artialclass Form1 : Form/*月服务?端 ?*/privateint serve_portNum=13 ;null ;p rivate Tcp Liste ner serve_liste ner =/服 务?器-与?客户§机u之间?的?连?接6状痢?态?Privatebool bserve_Connected = false ;/服务?器+侦i听瑈线?程Private Thread tserve_AcceptMsg = null ;/网?络?访?问e的?基U础?数簓据Y流i ?P rivate Networkstream n
3、Stream_servenull ;/创洹?建读a取?器一p rivateTextReader tReader_serve =null ;/创洹?建编括?写器+privateTextWriter wReader_serve =null ;/* 客1户 § 端?*/Privateint Client_portNum = 13;/客户§端?连?接6p rivate Tcp Clie nt clie nt_clie nt =null ;/客户§机u与服 务?器-之?间?的?连?接o状痢?态?privatebool bclient_Connected = false ;/
4、客户§机u侦i听瑈线程iprivateThread tclient_AcceptMsg =null ;/网?络?访?问e的?基U础?数簓据Y流i ?P rivate NetworkStream n Stream_clie nt=null ;/创洹?建读a取?器+p rivate TextReader tReader_clie nt =null ;/创洹?建编括?写器+private TextWriter wReader_client =null ;/接o收?、0发?送数簓据丫池?长O度0Privateconst int bytelength = 1024;/服务?器-接o收?池?by
5、te serve_rec_buff = new byte bytelength;/服务?器+发?送甘池?byte serve_send_buff = new byte bytelength;/客户§端?接6收?池?byte client_rec_buff = new byte bytelength;/客户§端?发fe ?送门池?byte client_send_buff = new byte bytelength;public Form1()In itializeCo mponen t();rb xs zf.Checked =true ;rb fs zf.Checked =
6、true ;rb_fwq.Checked =true ;cb fs cf.Checked =false ;btn _Qido ng.E nabled =false ;timer1.St op();timer2.St op();privatevoidtextBox1_TextChanged( object sender, EventArgs e)privatevoidlabel2_Click( object sender,EventArgs e)privatevoidtextBox1_TextChanged_1( object sender,EventArgs e)Clie nt_p ortNu
7、m =Convert .Tolnt32(tbx_sport.Text);/服务?器-启?动Privatevoid Qidong_btn_Click( object sender.EventArgs e)serve_ po rtNum =int .Parse(tbx_lport.Text);stri ng strlocali p = cBox_locali p.Text;IP Address localaddr =null ;localaddr = IPAddress .P arse(strlocali p);/liste ner = new Tcp Liste ner(strlocali p,
8、 serve_ portNum);/IPAddress localaddr = host.AddressListO;/stri ng ipAddress = host.AddressList0.ToStri ng();/tbx_Local_I P.Text = ipAddress;serve listener =new Tcp Liste ner(localaddr, serve_ po rtNum);lab serve.Text ="服务?器+正y在u等台?待鋣连?接0."serve_liste ner.Start();/创洹?建侦i听瑈线?程itserve_Acce p
9、tMsg =new Thread (new Threadstart (this .AcceptMessage);tserve_Acce ptMsg.Start();btn _Qido ng.E nabled =false ;/客户§端?连?接6服务?器十privatevoid lianjie_btn_Click(object sender,EventArgs e)stri ng hostName = tbx_serve IP .Text;tryclient client =n ewTc pClie nt(hostName, Clie nt_p ortNum);if (client_c
10、lient.Connected)/创洹?建侦i听瑈线?程itclie nt_Acce ptMsg = new Thread (newThreadStart (this .Client_AcceptMessage);tclie nt_Acce ptMsg.Start();btn _lia njie.E nabled =false ;lab client.Text="ok ! ?客户§机u连?接o上?服 务?器十:阰"+ hostName+ " , ?端 ?口 u 号?:阰"+ tbx_sport.Text;catchlab client.Text
11、="! ?客户§机U没有瓺连?接6上?服 务?器r 阰"+ hostName+ " , ?端 ?口 u 号?:阰"+ tbx_sport.Text;/服务?器+发?送字?符?数簓据丫public void serve_sendstr(string strsend)try/richTextBox2_KeyPressO和 lAcceptMessageO /都?将?向EchTextBoxI 写字?符?, ?可6能u访问e有瓺冲?突?,?/所U 以?,?需6要瘾多a线程1互Y斥a lock ( this )tx_serve_rec.A ppen dTe
12、xt("服 务?器一:阰"+ strsend +"n");/客户§机u聊天?信?息0写入?网?络?流i ?, ?以?便?服务?器-接6wReader_serve.WriteLi ne(strse nd);/清?理K?当獭?前°缓O冲?区?数簓据丫,?使?所0有瓺缓O冲?数簓据丫写'入?基U础?设?备?wReader_serve.Flush();catchMessageBox.Show("无T法?与?客户§机u通?信?!");/服务?器+发?送字?节u数簓组哩?数簓据丫P ublicvoid ser
13、ve_sendhex( string InputString)#regi on增?加6 字?符 ?处锔理K ?'');string HexStrSplit = InputString.Split( int len = HexStrSplit.Length;byte arr = new byte len;for ( int ii = 0; ii < arr.Length; ii+)arrii=Convert .ToByte(HexStrSplitii, 16);stri ng strtem =""for ( int ii = 0; ii < arr
14、.Length; ii+)strtem+=String .Format( " 0x0:X2 ", arrii);#en dregi ontry/richTextBox2_KeyPressO和 lAcceptMessageO/都?将?向crichTextBox1 写字?符?, ?可6能u访问e有瓺冲?突?,?/所U 以?,?需6要瘾多a线程1互Y斥alock ( this )tx_serve_rec.A ppen dText("服 务?器一:阰"+ strtem +"n");/客户§机u聊天?信?息0写入?网?络?流i ?,
15、?以?便?服务?器-接6n Stream_serve.Write(arr,0,arr.Le ngth);/清?理K?当獭?前°缓o冲?区?数簓据丫,?使?所0有瓺缓o冲?数簓据丫写'入?基U础?设?备?/n Stream_serve.Flush();catchMessageBox.Show("无T法?与?客户§机u通?信?!");/服务?器+发?送数簓据丫P rivatevoid butt on 1_Click(object sen der,Eve ntArgs e)if (bserve_Connected)if (cb_fs_cf.Checke
16、d = true )timer1. In terval =timer1.St op();Convert .ToInt32(tbx_fs_time.Text);timer1.Start();elsetimer1.St op();if (rb_fs_zf.Checked = true )serve_se ndstr(tx_serve_se nd.Text);else if (rb_fs_hex.Checked = true )serve_se ndhex(tx_serve_se nd.Text);elseMessageBox.Show("未与?客户§机u建立i?连?接o,不孑能
17、'通?信?。£ ");/定时骸?发 ?送Privatevoid ontime( object sender,EventArgs e)if (cb_fs_cf.Checked = true )if (rb_fs_zf.Checked = true )serve_se ndstr(tx_serve_se nd.Text);else if (rb_fs_hex.Checked = true )serve_se ndhex(tx_serve_se nd.Text);elsetimerl.St op();private void Save_SheZhi_btn_Click(
18、object sender, EventArgs e)/退?岀?/关?闭?窗洹?体 ?时骸?断?开a连?接0,?并0终?止 1线?程1(辍?否?则6,?VS调獭?试?程1序6将仍?处锔于 ?运?行D状痢?态?) ?P rivatevoid butt on 2_Click(object sen der,EventArgs e)tryserve_liste ner.St opO;tserve_Acce ptMsg.Abort();catchClose();privatevoid rb_fwq_CheckedChanged( objectsender,EventArgs e)privatevoid
19、rb_khd_CheckedChanged( objectsender,EventArgs e)btn _Qido ng.E nabled =true ;private void Form1_Load( object sender, EventArgs e) private void comboBox1_SelectedIndexChanged( object sender, EventArgs e)/扫A ?描e本?机"IP private void btn_localip_Click( object sender, EventArgs e)String hostName = Dn
20、s.GetHostName();IPHostEntry host;host = Dns.GetHostEntry(hostName);string srttemp;/IPAddress localaddr = null;foreach (IPAddress ip in host.AddressList)srttemp = ip.ToString();if (srttemp.StartsWith("192.168." )/tbx_Local_IP.Text = srttemp;/localaddr = ip;cBox_localip.Text = srttemp;/处锔理K猯
21、ab或dtext字?符?串?文?字?显?示?的?委-托狣,Public delegate void LabTextDelgate( string strtext);/服务?器+处锔理K猯ab文?字 ?显?示?的?委-托狣调獭?用?函-数簓Public void wstr_lab(stri ngstrtext)lab_serve.Text = strtext;/服务?器+处锔理K?服务?器+接o收?文?字?显?示?的?委-托狣调獭?用 ?函-数簓Public void wstr_txt(stri ngstrtext)/lab_serve.Text = strtext;tx_serve_rec.A
22、 ppen dText("客户§机 u:阰 + strtext +"n");/客户§机u处锔理K猯ab文?字?显?示?的?委-托狣调獭?用?函-数簓public void wstr_lab_client(string strtext)lab_clie nt.Text = strtext;/客户§机u处锔理K?服务?器+接o收?文?字?显?示?的?委-托狣调獭?用?函-数簓Public void wstr_txt_client(stri ng strtext)/lab_serve.Text = strtext;tx_clie nt_re
23、c.A ppen dText("服 务?器一:阰"+ strtext +"n");/处锔理K猼ext字?节u数簓组哩?文?字?显?示?的?委-托狣,Public delegate void byteTextDelgate( byte showbyte);/处锔理K ?服务?器+接0收?字?节u数簓组哩?文?字?显?示?的?委-托狣调獭?用?函一数簓Public void wstr_hex( byte showbyte)/lab_serve.Text = strtext;stri ng strtem=""/foreach(byte bt
24、em in showbyte)/strtem+=stri ng.Format(" 0x0,x2,btem");/for(int i = 0; i < showbyte.Length; i+)if (showbytei = 0)break;elsestrtem +=string .Format( " 0x0,2:X", showbytei);tx_serve_rec.A ppen dText("客户§机 u:阰 + strtem +"n");/处锔理K ?客户§机u接0收字?节0数簓组哩?文?字?显?
25、示?的?委-托狣调獭?用?函-数public void wstr_hex_client(byte showbyte)/lab_serve.Text = strtext;stri ng strtem =""/foreach (byte btem in showbyte)/strtem += stri ng.Format(" 0x0,x2,btem");strtem += stri ng.Format("0x0,2:X,btem");/if (btem = 0)/break;/ for(int i = 0; i < showbyte
26、.Length; i+)if (showbytei = 0)break;elsestrtem +=string .Format(“ 0x0,2:X", showbytei);tx_clie nt_rec.A ppen dText("服务?器r 阰"+ strtem+ "n");/服务?器r侦i听瑈并0显?示?信?息0P ublicvoid AcceptMessageO/接6受酣?客户§机u的连?接6请?求6Tcp Clie nt clie nt = serve_liste ner.Acce ptTc pClie nt();if (cl
27、ient != null )bserve_Connected =true ;/this.lab_serve.Text = "与?客户§ " + die nt.ToStri ng() + "成e功建立i ?连?接6。£:string strtem ="与?客户§ " + client.ToString() +"成e功建立i ?连?接6。£"/lab_serve.LabTextDelgate writelab =new LabTextDelgate(wstr_lab);this .Invo
28、ke(writelab, strtem);nStream_serve = client.GetStream();/nStream_serve.Read(/读a字?节U流i?tReader_serve =new StreamReader(nStream_serve);/tReader_serve.re/写字?节U流i?wReader_serve =new StreamWriter (nStream_serve);stringsTemp; /临M ?时骸?存?储洹?读a取?的?字?符?串?while(bserve_Connected)if(rb_xs_zf.Checked= true )try/n
29、Stream_serve = client.GetStream();/读a字?节U流i?/tReader_serve = new StreamReader(nStream_serve);/连?续?从洙?当獭?前°流i ?中D读a取?字?符?串?直士至a结a束sTe mp = tReader_serve.ReadL in e();if (sTemp.Length != 0)/richTextBox2_KeyPress。和 lAcceptMessageO /都?将?向drichTextBox1写字?符?, ?可e能u访问e有瓺冲?突?, ?/所U以?,?需6要瘾多a线程1互Y斥alock
30、 ( this )/tx_serve_rec.Text =" 客户§机 u:阰 + sTe mp + "n" +tx_serve_rec.Text;/this.tx_serve_rec .In voke( new MethodI nvoker(/delegate this.tx_serve_rec.AppendText("客户§机u:阰"+ sTe mp + "n" ); /);LabTextDelgate writelab =n ewLabTextDelgate(wstr_txt);catchthis
31、.Invoke(writelab, sTemp);tserve_Acce ptMsg.AbortO;MessageBox.Show("无T法?与?客户§机u通?信?。£ ");else if (rb_xs_hex.Checked= true)/ 读 a取?16 进?制 ?数簓据 Y 并0显?示?try/nStream_serve = client.GetStream();/读a字?节U流i?/tReader_serve = new StreamReader(nStream_serve);/连?续?从洙?当獭?前°流i ?中D读a取?字?符?串?
32、直士至a结a束 /byte serve_rec_buff = new bytebytelength;/sTemp = nStream_serve.Read(serve_rec_buff,0,bytelength);nStream_serve.Read(serve_rec_buff,0,serve_rec_buff.Length);if (serve_rec_buff.Length != 0)/richTextBox2_KeyPress()和 lAcceptMessageO /都?将?向drichTextBox1写字?符?, ?可e能u访问e有瓺冲?突?, ?/所U以?,?需6要瘾多a线程1互Y
33、斥alock ( this )/tx_serve_rec.Text =" 客户§机 u:阰 + sTe mp + "n" +tx_serve_rec.Text;/ this.tx_serve_rec.Invoke(new MethodInvoker(/delegate this.tx_serve_rec.AppendText("客户§机u:阰" + sTemp + "n" ); /);byteTextDelgate writelab =new byteTextDelgate(wstr_hex);this
34、.Invoke(writelab, serve_rec_buff);catchtserve_Acce ptMsg.AbortO;MessageBox.Show("无T法?与?客户§机u通?信?。£ “);n Stream_serve = clie nt.GetStream();MessageBox.Show("服务?器+侦i听瑈与?显?示?线?程1退?岀?");/禁?止1当獭?前°的?发?送i与?接0收?clie nt.Close();serve_liste ner.St op();/客户§端?侦i听瑈并0显?示?信?息0P
35、ublic void Client_AcceptMessage()bclient Connected =true ;/绑悒?定客户§机u的?连?接6流i?n Stream_clie nt = clie nt_clie nt.GetStream();tReader client =/读a字?节u流i?new StreamReader(nStream_client);/tReader serve.rewReader_client =/写字?节u流i?new StreamWriter (nStream_client);String sTemp; /临m ?时骸?存?储洹?读a取?的?字?符
36、?串? while (bclient_Connected)if (rb_xs_zf.Checked = true )try/读a字?节u流i?/连?续?从洙?当獭?前°流i ?中D读a取字?符?串?直士至a结a束sTemp = tReader_client.ReadLine();if (sTemp.Length != 0)/需e要瘾多a线程1互Y斥a lock ( this )newLabTextDelgate writelab =LabTextDelgate(wstr_txt_client);this .Invoke(writelab, sTemp);catchtserve_Acc
37、e ptMsg.AbortO;MessageBox.Show("无T法鬲?与?服务?器一通?信?。£ “);else if (rb_xs_hex.Checked = true ) / 读 a 取?16 进?制?数簓据 Y并 0 显?示?tryint read num = 0;lock ( this )Array .Clear(client_rec_buff, 0, client_rec_buff.Length);readnum = n Stream_clie nt.Read(clie nt_rec_buff, 0,clie nt_rec_buff.Le ngth);if (
38、readnum != 0)/需e要瘾多a线?程1互Y斥anewlock ( this )byteTextDelgate writelab =byteTextDelgate(wstr_hex_clie nt);this .Invoke(writelab, client_rec_buff);catchtserve_Acce ptMsg.AbortO;MessageBox.Show("无T法鬲?与?服务?器一通?信?。£ “);n Stream_clie nt = clie nt_clie nt.GetStream();MessageBox.Show("客户§
39、机U侦i听瑈与显?示?线?程i退?岀?");/禁?止1当獭?前°的?发?送i与?接0收?clie nt_clie nt.Close();/客户§端?发fe ?送数簓据Yprivatevoid btn_client_send_Click(object sender,EventArgs e)if (bclient_Connected)if (cb_fs_cf.Checked = true )timer2.St op();timer2 .In terval =Con vert .ToI nt32(tbx_fs_time.Text);timer2.Start();else
40、timer2.St op();if (rb_fs_zf.Checked = true )clie nt_se ndstr(tx_clie nt_se nd.Text);else if (rb_fs_hex.Checked = true )clie nt_se ndhex(tx_clie nt_se nd.Text);elseMessageBox.Show("未与?服务?器-建立i ?连?接0,?不孑能'通?信?。£");/客户§机u违续?发fe ?送EventArgs e)Privatevoid ontime2( object sender,if (cb_fs_cf.Checked = true )if (rb_fs_zf.Checked = trueclie nt_se ndstr(tx_clie nt_se nd.Text);else if (rb_fs_hex.Checked = true )cli
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 年产xxx特种合金项目可行性研究报告(立项说明)
- 年产xx博古架项目可行性研究报告(可行性分析)
- 关于医疗护理的英文
- 护理儿科营养不良
- 招投标法律培训课件
- 一年级上册数学教案-6.3 10加几、十几加几及相应的减法 -人教版
- 二年级下册小学数学教案 九、收集数 北京版
- 审计个人未来规划
- 科研机构创新感知监测元数据-征求意见稿-编制说明
- 智力障碍教育考试题库单选题100道及答案解析
- (内容完整)六年级上册关联词复习课件
- (完整版)单板硬件调试报告
- 常用工具规格含义及使用的方法分解课件
- 化妆师技能理论考试题库大全(汇总版、600题)
- 2022新闻联播播报PPT通用模板
- 电杆组立及拉线安装单元评定表
- 长沙市历年中考数学试卷,2014-2021年长沙中考数学近八年真题汇总(含答案解析)
- 公共经济学ppt课件(完整版)
- GB∕T 3639-2021 冷拔或冷轧精密无缝钢管
- 2023版高三一轮总复习地理湘教版:第24讲 能力练3 地方性分异规律
- 教学活动:大班语言《神奇种子店》
评论
0/150
提交评论