c#tcp调试助手源代码_第1页
c#tcp调试助手源代码_第2页
c#tcp调试助手源代码_第3页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、c# tcp调试助手源代码以下是winform tcp调试助手源代码,调试通过,可进行简单应用 using System;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;/添 ?加6新?的?名?称?空?间?引皔用?using System.10;using System.Net;using System.Threading;/us ing System.Timers;namespaceTCP 调獭?试?助。手?public partial

2、 class Form1 : Form/* 月服务?端 ?*/private int serve_portNum=13 ;private TcpListener serve_listener =null ;/服 务?器+与?客丨户§机。之间?的?连?接6状痢?态?private bool bserve_Connected =false ;/服务?器+侦听瑈线?程private Thread tserve_AcceptMsg = null ;/网?络?访?问e的?基n础?数簓据丫流i ?private NetworkStream nStream_serve =null ;/创洹?建读0

3、取?器+private TextReader tReader_serve =null ;/创洹?建编括?写器+private TextWriter wReader_serve = null ;/* 客户 §端?*/private int Client_portNum = 13;/客丨户§端?连?接6private TcpClient client_client =null ;II客丨户§机。与服 务?器+之?间?的?连?接6状痢?态?private bool bclie nt_Conn ected = falseII客丨户§机。侦听瑈线程1private

4、 Thread tclient_AcceptMsg =null ;网?络?访?问e的?基n础?数簓据丫流i ?private Networkstream nStream_client=null ;/创洹?建读0取?器+privateTextReader tReader_client =null ;/创洹?建编括?写器+privateTextWriter wReader_client =null ;/接6收?、C发厉?送丨数簓据丫池?长O度0private const int bytelength = 1024;/服务?器+接6收?池?byte serve_rec_buff = new byte

5、 bytelength;/服务?器+发厉?送丨池?byte serve_send_buff = new byte bytelength;/客丨户§端?接6收?池?byte client_rec_buff = new byte bytelength;/客丨户§端?发厉?送丨池?byte client_send_buff = new byte bytelength;public Form1()In itializeComp onen t(); rb_xs_zf.Checked =true ;rb_fs_zf.Checked =true ;rb_fwq.Checked =true

6、 ;cb_fs_cf.Checked =false;btn _Qido ng.E nabled =falsetimer1.Stop();timer2.Stop();private void textBox1_TextChanged( object sender, EventArgs e)private void label2_Click( object sender, EventArgs e)private void textBox1_TextChanged_1( object sender,EventArgs e)/服务?器+启?动private void Qidong_btn_Click(

7、object sender.EventArgs e)serve_portNum =int .Parse(tbx_lport.Text);stri ng strlocalip = cBox_localip.Text;IPAddress localaddr =null ;localaddr = IPAddress.Parse(strlocalip);/liste ner = new TcpListe ner(strlocalip, serve_portNum);/IPAddress localaddr = host.AddressListO;/stri ng ipAddress = host.Ad

8、dressListO.ToStri ng();tbx_Local_IP.Text = ipAddress;serve_liste ner =new TcpListe ner(localaddr, serve_portNum);lab_serve.Text ="服 务?器+正y在d等台?待鋣连?接6."serve_liste ner.Start();/创洹?建侦听瑈线?程tserve_AcceptMsg =new Thread (new Threadstart (this .AcceptMessage);tserve_AcceptMsg.Start();btn_Qidong.

9、Enabled =false ;/客丨户§端?连?接6服务?器+private void lianjie_btn_Click(object sender,EventArgs e)stri ng hostName = tbx_serveIP.Text;Clie nt_portNum =Con vert .ToI nt32(tbx_sport.Text);tryclie nt_clie nt =n ewTcpClie nt(hostName, Clie nt_portNum);if (client_client.Connected)/创洹?建侦听瑈线?程tclient_AcceptMsg

10、 =new Thread (newThreadStart (this .Client_AcceptMessage);tclie nt_AcceptMsg.Start();btn_lianjie.Enabled =false ;+ hostNamelab_client.Text= "ok ! ?客丨户§机。连接 6 上?服 务?器十:阰"+ " , ?端 ?口。号?:阰"+ tbx_sport.Text;catchlab_client.Text= " ! ?客丨户§机。没有瓺连?接6上?服 务?器十:阰"+ host

11、Name+ " , ?端 ?口。号?:阰"+ tbx_sport.Text;/服务?器+发厉?送丨字?符?数簓据丫public void serve_sendstr(string strsend)tryrichTextBox2_KeyPress()和 iAcceptMessage()/都?将?向crichTextBox1 写字?符?, ?可0能u访问e有瓺冲?突?,?/所u 以?, ?需0要癮多0线程互Y斥alock ( this )tx_serve_rec.AppendText("服 务?器:阰"+ strsend +"n");/客

12、丨户§机。聊天 ?信?息写入?网?络?流i ?, ?以?便?服 务?器+接6 收?wReader_serve.WriteLi ne(strse nd);/清?理元?当獭?前°缓o冲?区?数簓据丫,?使?所。有瓺缓o冲?数簓据丫写 入?基u础?设E?备?wReader_serve.Flush();catchMessageBox.Show("无 T法厉?与?客丨户§机。通?信?!");/服 务?器+发厉?送丨字?节。数簓组哩?数簓据丫public void serve_sendhex( string InputString)#region增?加6

13、字?符?处鋦理元?string HexStrSplit = InputString.Split('');int len = HexStrSplit.Length;byte arr = new byte len;for ( int ii = 0; ii < arr.Length; ii+)arrii =Con vert .ToByte(HexStrSplitii, 16);string strtem =""for ( int ii = 0; ii < arr.Length; ii+)strtem+=String .Format( " 0x

14、0:X2 ", arrii);#en dregi ontryrichTextBox2_KeyPress()和 iAcceptMessage()/都?将?向crichTextBox1 写字?符?, ?可0能u访问e有瓺冲?突?,?/所u 以?, ?需0要癮多0线程互Y斥alock ( this ) tx_serve_rec.AppendText("服 务?器:阰"+ strtem +"n");/客丨户§机。聊天 ?信?息写入?网?络?流i ?, ?以?便?服务?器+接6收?n Stream_serve.Write(arr,O,arr.L

15、e ngth);/清?理元?当獭?前°缓o冲?区?数簓据丫,?使?所。有瓺缓o冲?数簓据丫写 入?基u础?设E?备?n Stream_serve.Flush();catchMessageBox.Show("无 T法厉?与?客丨户§机。通?信?!");/服务?器+发厉?送丨数簓据丫private void butt on 1_Click(object sen der, Eve ntArgs e)if (bserve_Connected)if (cb_fs_cf.Checked = true )timer1.Stop();timerl.Interval =C

16、onvert .ToInt32(tbx_fs_time.Text);timer1.Start();elsetimer1.Stop();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("未与?客丨户§机。建立i?连?接6,?不?能口通?信?。£“);/定时骸?发厉?送丨private void ontime

17、( 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);elsetimer1.Stop();private void Save_SheZhi_btn_Click( object sender,EventArgs e)/退?岀?/关?闭?窗洹?体 ?时骸?断?开a连?接6

18、,?并C终?止 1线?程1(辍?否?则o, ?VS调獭?试?程序。将仍?处鋦于?运?行。状痢?态?) ? ? ?private void butt on 2_Click(object sen der. Eve ntArgs e)tryserve_liste ner.Stop();tserve_AcceptMsg.Abort();catch Close();private void rb_fwq_CheckedChanged( object sender, EventArgsprivate void rb_khd_CheckedChanged( object sender, EventArgsp

19、rivate void Form1_Load( object sender,EventArgs e)private void comboBox1_SelectedIndexChanged( object sender,/扫A ?描0本?机uIPprivate void btn_localip_Click(object sender,EventArgs e)Str ing hostName = Dn s.GetHostName();IPHostE ntry host;host = Dn s.GetHostE ntry(hostName);e)e)EventArgs e)stri ng srtte

20、mp;/IPAddress localaddr = null;cBox_localip .I tems.Clear();foreach (IPAddress ip in host.AddressList)srttemp = ip.ToStr in g();cBox_localip .I tems.Add(srttemp);if (srttemp.StartsWith( "192.168.")tbx_Local_IP.Text = srttemp;/localaddr = ip;cBox_localip.Text = srttemp;btn_Qidong.Enabled =t

21、rue ;/处鋦理元猯ab或dtext字?符?串?文?字?显?示?的?委-托狣,public delegate void LabTextDelgate( string strtext);/服 务?器+处鋦理元猯ab文?字 ?显?示?的?委-托狣调獭?用?函-数簓public void wstr_lab( string strtext)lab_serve.Text = strtext;/服 务?器+处鋦理元?服务?器+接6收?文?字?显?示?的?委托狣调獭?用 ?函数簓public void wstr_txt( string strtext)/lab_serve.Text = strtext;t

22、x_serve_rec.AppendText("客丨户§ 机 d:阰"+ strtext +"n");/客丨户§机。处鋦理元猯ab文?字?显?示?的?委-托狣调獭?用?函-数簓public void wstr_lab_client(string strtext)lab_clie nt.Text = strtext;/客丨户§机0处鋦理N?服 务?器+接6 收?文 ?字?显?示?的?委-托狣调獭?用?函-数簓 public void wstr_txt_client(string strtext)/lab_serve.Text

23、= strtext;tx_client_rec.AppendText("服 务?器十:阰"+ strtext +"n");/处鋦理N猼ext字?节。数簓组哩?文?字?显?示?的?委-托狣,public delegate void byteTextDelgate( byte showbyte);/处鋦理元?服 务?器+接6收?字?节。数簓组哩?文?字?显?示?的?委-托狣调獭?用?函一 数簓public void wstr_hex( byte showbyte)/lab_serve.Text = strtext;string strtem="&q

24、uot;foreach(byte btem 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.AppendText("客丨户§ 机 d:阰"+ strtem +"n");/处鋦理元?客丨户

25、7;机。接6收字?节。数簓组哩?文?字?显?示?的?委-托狣调獭?用?函-数 簓public void wstr_hex_client(byte showbyte)/lab_serve.Text = strtext;string 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 (

26、int i = 0; i < showbyte.Length; i+)if (showbytei = 0)break;elsestrtem +=string .Format( " 0x0,2:X", showbytei);tx_client_rec.AppendText("服 务?器十:阰"+ strtem+ "n");/服务?器+侦1听瑈并C显?示?信?息Cpublic void AcceptMessage()/接6受酣?客丨户§机0的连?接6请?求6TcpClie nt clie nt = serve_liste

27、ner.AcceptTcpCIie nt();if (client !=null )bserve_Connected =true ;与?客丨户§ " + clie nt.ToStri ng() + "成e功建立i ?连?接6o£"string strtem ="与?客丨户§ " + client.ToString() +"成e功建立 i ?连?接 6o£"/lab_serve.LabTextDelgate writelab =new LabTextDelgate(wstr_lab);t

28、hisnvoke(writelab, strtem);n Stream_serve = clie nt.GetStream();n Stream_serve.Read(/读0字?节。流i?tReader_serve =new StreamReader(nStream_serve);tReader_serve.re/写字?节。流i?wReader_serve =new StreamWriter (nStream_serve);string sTemp; /临诡?时骸?存?储洹?读a取?的?字?符?串?while (bserve_Connected)if (rb_xs_zf.Checked= tr

29、ue )tryn Stream_serve = clie nt.GetStream();/读a字?节。流i?tReader_serve = new StreamReader( nStream_serve);/连?续?从洙?当獭?前°流:?中D读a取?字?符?串?直士至a结a束sTemp = tReader_serve.ReadL in e();if (sTemp.Length != 0)richTextBox2_KeyPress()和 iAcceptMessage()/都?将?向drichTextBox1写字?符?,?可e能u访问e有瓺冲?突?, ?/所u 以?, ?需0要癮多a线程

30、互Y斥alock ( this )tx_serve_rec.Text ="客丨户§机阰"+ sTemp + "n" +tx_serve_rec.Text;/ this.tx_serve_rec .In voke( new MethodI nvoker(客丨户§ 机 d:阰匕"+ sTemp + "n" ); /);LabTextDelgate writelab =n ewLabTextDelgate(wstr_txt);thisnvoke(writelab, sTemp);catchtserve_Acce

31、ptMsg.Abort();MessageBox.Show("无 T法厉?与?客丨户§ 机。通?信?。£ ");else if (rb_xs_hex.Checked= true )/ 读 a 取?16 进?制 ?数簓据 Y并 C 显?示?tryn Stream_serve = clie nt.GetStream();/读a字?节。流i?tReader_serve = new StreamReader( nStream_serve);/连?续?从洙?当獭?前°流:?中D读a取?字?符?串?直士至a结a束byte serve_rec_buff =

32、new bytebytele ngth;/sTemp = n Stream_serve.Read(serve_rec_buff,0,bytele ngth);n Stream_serve.Read(serve_rec_buff,0,serve_rec_buff.Le ngth);if (serve_rec_buff.Length != 0)richTextBox2_KeyPress()和 iAcceptMessage()/都?将?向drichTextBox1写字?符?, ?可e能u访问e有瓺冲?突?, ?/所u 以?, ?需0要癮多a线程互Y斥alock ( this )tx_serve_re

33、c.Text ="客丨户§机阰"+ sTemp + "n" +tx_serve_rec.Text;/this.tx_serve_rec .In voke( new Methodi nvoker(客丨户§ 机 d:阰匕"+ sTemp + "n" ); );byteTextDelgate writelab =n ewbyteTextDelgate(wstr_hex);thisnvoke(writelab, serve_rec_buff);catchtserve_AcceptMsg.Abort();Messa

34、geBox.Show("无T法厉?与?客丨户§机。通?信?。£ ");n Stream_serve = clie nt.GetStream();MessageBox.Show("服 务?器+侦听瑈与?显?示?线?程退?岀?");/禁?止1当獭?前°的?发厉?送与?接6收?clie nt.Close();serve_liste ner.Stop();/客丨户§端?侦听瑈并C显?示?信?息C/20160605 接 6收?hex 字?符?不 ?稳0定public void Client_AcceptMessage()bc

35、lient_Connected =true ;/绑悒?定客丨户§机。的?连?接6流i?n Stream_clie nt = clie nt_clie nt.GetStream();/读0字?节。流i?tReader_client =new StreamReader(nStream_client);/tReader_serve.re/写字?节。流i?wReader_client =new StreamWriter (nStream_client);string sTemp; /临诡?时骸?存?储洹?读a取?的?字?符?串?while (bclient_Connected)if (rb_

36、xs_zf.Checked = true )try/读0字?节。流i ?/连?续?从洙?当獭?前°流:?中D读0取?字?符?串?直士至0结0束sTemp = tReader_clie nt.ReadL in e();if (sTemp.Length != 0)/需o要癮多a线程互Y斥alock ( this )LabTextDelgate writelab =newLabTextDelgate(wstr_txt_clie nt);thisnvoke(writelab, sTemp);catchtserve_AcceptMsg.Abort();MessageBox.Show("

37、;无 T法厉?与?服 务?器 + 通?信?。£ ");else if (rb_xs_hex.Checked = true ) / 读 0 取?16 进?制?数簓据 Y并 C 显?示?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 (readnum != 0)/需o要癮多a线?程互Y斥a

38、lock ( this )byteTextDelgate writelab =newbyteTextDelgate(wstr_hex_clie nt);thisnvoke(writelab, client_rec_buff);catchtserve_AcceptMsg.Abort();MessageBox.Show("无 T法笳?与?服 务?器 + 通?信?。£ ");n Stream_clie nt = clie nt_clie nt.GetStream();MessageBox.Show("客丨户§机。侦听瑈与显?示?线?程退?岀?&quo

39、t;);/禁?止1当獭?前°的?发厉?送与?接6收?clie nt_clie nt.Close();/客丨户§端?发厉?送丨数簓据丫private void btn_client_send_Click(object sender,EventArgs e)if (bclient_Connected)if (cb_fs_cf.Checked = true )timer2.Stop();timer2.lnterval =Convert .ToInt32(tbx_fs_time.Text);timer2.Start();elsetimer2.Stop();if (rb_fs_zf.

40、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 ?连?接6, ?不?能口通?信?。£");/客丨户§机。违续?发厉?送丨private void ontime2( object sender,EventArgs e)if (cb_fs_cf.Checked = true )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_cl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论