最新c# tcp调试助手源代码.doc

上传人:1595****071 文档编号:33806861 上传时间:2022-08-12 格式:DOC 页数:43 大小:205.50KB
返回 下载 相关 举报
最新c# tcp调试助手源代码.doc_第1页
第1页 / 共43页
最新c# tcp调试助手源代码.doc_第2页
第2页 / 共43页
点击查看更多>>
资源描述

《最新c# tcp调试助手源代码.doc》由会员分享,可在线阅读,更多相关《最新c# tcp调试助手源代码.doc(43页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、精品资料c# tcp调试助手源代码. c# tcp 调试助手源代码以下是winform tcp 调试助手源代码,调试通过,可进行简单应用using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;/添?加新?的?名?称?空?间?引皔用?using System.IO;using System.Net;usi

2、ng System.Net.Sockets;using System.Threading;/using System.Timers;namespace TCP调獭?试?助手? public partial class Form1 : Form /*服务?端?*/ private int serve_portNum=13 ; private TcpListener serve_listener = null; /服务?器与?客户机之?间?的?连?接状痢?态? private bool bserve_Connected = false; /服务?器侦听瑈线?程 private Thread tse

3、rve_AcceptMsg = null; /网?络?访?问的?基础?数簓据Y流? private NetworkStream nStream_serve = null; /创洹?建读取?器 private TextReader tReader_serve = null; /创洹?建编括?写器 private TextWriter wReader_serve = null; /*客户端?*/ private int Client_portNum = 13; /客户端?连?接 private TcpClient client_client = null; /客户机与?服务?器之?间?的?连?接状

4、痢?态? private bool bclient_Connected = false; /客户机侦听瑈线?程 private Thread tclient_AcceptMsg = null; /网?络?访?问的?基础?数簓据Y流? private NetworkStream nStream_client= null; /创洹?建读取?器 private TextReader tReader_client = null; /创洹?建编括?写器 private TextWriter wReader_client = null; /接收?、发?送数簓据Y池?长度 private const int

5、 bytelength = 1024; /服务?器接收?池? byte serve_rec_buff = new bytebytelength; /服务?器发?送池? byte serve_send_buff = new bytebytelength; /客户端?接收?池? byte client_rec_buff = new bytebytelength; /客户端?发?送池? byte client_send_buff = new bytebytelength; public Form1() InitializeComponent(); rb_xs_zf.Checked = true; r

6、b_fs_zf.Checked = true; rb_fwq.Checked = true; cb_fs_cf.Checked = false; btn_Qidong.Enabled = false; /timer1=new System.Windows.Forms.Timer(); timer1.Stop(); timer2.Stop(); private void textBox1_TextChanged(object sender, EventArgs e) private void label2_Click(object sender, EventArgs e) private voi

7、d textBox1_TextChanged_1(object sender, EventArgs e) /服务?器启?动 private void Qidong_btn_Click(object sender, EventArgs e) serve_portNum = int.Parse(tbx_lport.Text); string strlocalip = cBox_localip.Text; IPAddress localaddr = null; localaddr = IPAddress.Parse(strlocalip); /listener = new TcpListener(s

8、trlocalip, serve_portNum); /IPAddress localaddr = host.AddressList0; /string ipAddress = host.AddressList0.ToString(); /tbx_Local_IP.Text = ipAddress; serve_listener = new TcpListener(localaddr, serve_portNum); lab_serve.Text = 服务?器正y在等台?待鋣连?接.; serve_listener.Start(); /创洹?建侦听瑈线?程 tserve_AcceptMsg =

9、 new Thread(new ThreadStart(this.AcceptMessage); tserve_AcceptMsg.Start(); btn_Qidong.Enabled = false; /客户端?连?接服务?器 private void lianjie_btn_Click(object sender, EventArgs e) string hostName = tbx_serveIP.Text; Client_portNum = Convert.ToInt32(tbx_sport.Text); try client_client = new TcpClient(hostN

10、ame, Client_portNum); if (client_client.Connected) /创洹?建侦听瑈线?程 tclient_AcceptMsg = new Thread(new ThreadStart(this.Client_AcceptMessage); tclient_AcceptMsg.Start(); btn_lianjie.Enabled = false; lab_client.Text = ok!?客户机连?接上?服务?器:阰 + hostName + ,?端?口号?:阰 + tbx_sport.Text; catch lab_client.Text = !?客户

11、机没?有瓺连?接上?服务?器:阰 + hostName + ,?端?口号?:阰 + tbx_sport.Text; /服务?器发?送字?符?数簓据Y public void serve_sendstr(string strsend) try /richTextBox2_KeyPress()和AcceptMessage() /都?将?向richTextBox1写字?符?,?可能访?问有瓺冲?突?,? /所以?,?需要癮多线?程互¥斥a lock (this) tx_serve_rec.AppendText(服务?器:阰 + strsend + n); /客户机聊?天?信?息写入?网?络?流?,?

12、以?便?服务?器接收? wReader_serve.WriteLine(strsend); /清?理?当獭?前缓o冲?区?数簓据Y,?使?所有瓺缓o冲?数簓据Y写入?基础?设?备? wReader_serve.Flush(); catch MessageBox.Show(无T法?与?客户机通?信?!); /服务?器发?送字?节数簓组哩?数簓据Y public void serve_sendhex(string InputString) #region 增?加字?符?处鋦理? string HexStrSplit = InputString.Split( ); int len = HexStrS

13、plit.Length; byte arr = new bytelen; for (int ii = 0; ii arr.Length; ii+) arrii = Convert.ToByte(HexStrSplitii, 16); string strtem = ; for (int ii = 0; ii arr.Length; ii+) strtem+= String.Format( 0x0:X2 , arrii); #endregion try /richTextBox2_KeyPress()和AcceptMessage() /都?将?向richTextBox1写字?符?,?可能访?问有

14、瓺冲?突?,? /所以?,?需要癮多线?程互¥斥a lock (this) tx_serve_rec.AppendText(服务?器:阰 + strtem + n); /客户机聊?天?信?息写入?网?络?流?,?以?便?服务?器接收? nStream_serve.Write(arr,0,arr.Length); /清?理?当獭?前缓o冲?区?数簓据Y,?使?所有瓺缓o冲?数簓据Y写入?基础?设?备? /nStream_serve.Flush(); catch MessageBox.Show(无T法?与?客户机通?信?!); /服务?器发?送数簓据Y private void button1_C

15、lick(object sender, EventArgs e) if (bserve_Connected) if (cb_fs_cf.Checked = true) timer1.Stop(); timer1.Interval = Convert.ToInt32(tbx_fs_time.Text); timer1.Start(); else timer1.Stop(); if (rb_fs_zf.Checked = true) serve_sendstr(tx_serve_send.Text); else if (rb_fs_hex.Checked = true) serve_sendhex

16、(tx_serve_send.Text); else MessageBox.Show(未与?客户机建立?连?接,?不?能通?信?。); /定时骸?发?送 private void ontime(object sender, EventArgs e) if (cb_fs_cf.Checked = true) if (rb_fs_zf.Checked = true) serve_sendstr(tx_serve_send.Text); else if (rb_fs_hex.Checked = true) serve_sendhex(tx_serve_send.Text); else timer1.

17、Stop(); private void Save_SheZhi_btn_Click(object sender, EventArgs e) /退?出? /关?闭?窗洹?体?时骸?断?开a连?接,?并终?止1线?程(辍?否?则,?VS调獭?试?程序将?仍?处鋦于?运?行D状痢?态?)????? private void button2_Click(object sender, EventArgs e) try serve_listener.Stop(); tserve_AcceptMsg.Abort(); catch Close(); private void rb_fwq_CheckedCh

18、anged(object sender, EventArgs e) private void rb_khd_CheckedChanged(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e) private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) /扫?描本?机IP private void btn_localip_Click(object sender, EventArgs e) String hos

19、tName = Dns.GetHostName(); IPHostEntry host; host = Dns.GetHostEntry(hostName); / int length = host.AddressList.Length; string srttemp; /IPAddress localaddr = null; cBox_localip.Items.Clear(); foreach (IPAddress ip in host.AddressList) srttemp = ip.ToString(); cBox_localip.Items.Add(srttemp); if (sr

20、ttemp.StartsWith(192.168.) /tbx_Local_IP.Text = srttemp; /localaddr = ip; cBox_localip.Text = srttemp; btn_Qidong.Enabled = true; /处鋦理猯ab或text字?符?串?文?字?显?示?的?委托狣, public delegate void LabTextDelgate(string strtext); /服务?器处鋦理猯ab文?字?显?示?的?委托狣调獭?用?函数簓 public void wstr_lab(string strtext) lab_serve.Text

21、 = strtext; /服务?器处鋦理?服务?器接收?文?字?显?示?的?委托狣调獭?用?函数簓 public void wstr_txt(string strtext) /lab_serve.Text = strtext; tx_serve_rec.AppendText(客户机:阰 + strtext + n); /客户机处鋦理猯ab文?字?显?示?的?委托狣调獭?用?函数簓 public void wstr_lab_client(string strtext) lab_client.Text = strtext; /客户机处鋦理?服务?器接收?文?字?显?示?的?委托狣调獭?用?函数簓

22、public void wstr_txt_client(string strtext) /lab_serve.Text = strtext; tx_client_rec.AppendText(服务?器:阰 + strtext + n); /处鋦理猼ext字?节数簓组哩?文?字?显?示?的?委托狣, public delegate void byteTextDelgate(byte showbyte); /处鋦理?服务?器接收?字?节数簓组哩?文?字?显?示?的?委托狣调獭?用?函数簓 public void wstr_hex(byte showbyte) /lab_serve.Text = s

23、trtext; string strtem=; /foreach(byte btem in showbyte) / / strtem+=string.Format( 0x0,x2,btem); / for (int i = 0; i showbyte.Length; i+) if (showbytei = 0) break; else strtem += string.Format( 0x0,2:X, showbytei); tx_serve_rec.AppendText(客户机:阰 + strtem + n); /处鋦理?客户机接收?字?节数簓组哩?文?字?显?示?的?委托狣调獭?用?函数簓

24、 public void wstr_hex_client(byte showbyte) /lab_serve.Text = strtext; string strtem = ; /foreach (byte btem in showbyte) / / /strtem += string.Format( 0x0,x2,btem); / strtem += string.Format(0x0,2:X,btem); / if (btem = 0) / break; / for (int i = 0; i showbyte.Length; i+) if (showbytei = 0) break; e

25、lse strtem += string.Format( 0x0,2:X, showbytei); tx_client_rec.AppendText(服务?器:阰 + strtem+n); /服务?器侦听瑈并显?示?信?息 public void AcceptMessage() /接受酣?客户机的?连?接请?求 TcpClient client = serve_listener.AcceptTcpClient(); if (client != null) bserve_Connected = true; /this.lab_serve.Text = 与?客户 + client.ToString

26、() + 成功|建立?连?接。; string strtem = 与?客户 + client.ToString() + 成功|建立?连?接。; /lab_serve. LabTextDelgate writelab = new LabTextDelgate(wstr_lab); this.Invoke(writelab, strtem); nStream_serve = client.GetStream(); /nStream_serve.Read( /读字?节流? tReader_serve = new StreamReader(nStream_serve); /tReader_serve.

27、re /写字?节流? wReader_serve = new StreamWriter(nStream_serve); string sTemp; /临?时骸?存?储洹?读取?的?字?符?串? while (bserve_Connected) if (rb_xs_zf.Checked=true) try /nStream_serve = client.GetStream(); /读字?节流? /tReader_serve = new StreamReader(nStream_serve); /连?续?从洙?当獭?前流?中D读取?字?符?串?直至结束? sTemp = tReader_serve

28、.ReadLine(); if (sTemp.Length != 0) /richTextBox2_KeyPress()和AcceptMessage() /都?将?向richTextBox1写字?符?,?可能访?问有瓺冲?突?,? /所以?,?需要癮多线?程互¥斥a lock (this) /tx_serve_rec.Text = 客户机:阰 + sTemp + n + tx_serve_rec.Text; / this.tx_serve_rec.Invoke(new MethodInvoker( /delegate this.tx_serve_rec.AppendText(客户机:阰 + sTemp + n ); /); LabTextDelgate writelab = new LabTextDelgate(wstr_txt); this.Invoke(writelab, sTemp); catch tserve_AcceptMsg.Abort(); MessageBox.Show(无T法?与?客户机通?信?。); else if (rb_xs_hex.Checked=true)/读取?16进?制?数簓据Y并显?示? try /nStream_serve = client.GetStream();

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁