版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、组C/S模式的编程实例C#语言编写报告人: 实验分组: 班级: 学号: 负责内容:TCPserver 1. 实验目的 利用C#编程,通过TCP的数据连接和传输的方式,编写一个基于TCP的CS模式的简易聊天程序。2. 实验思想和方案 TCPIP协议栈。TCPIP参考模型分为四个层次:应用层,传输层,网络互连层和主机到网络层。在TCP中常用的类有:TcpListener类,TcpClient类,NetworkStream等。通讯流程图:部分源代码:using System;using System.Collections.Generic;using System.ComponentModel;us
2、ing System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Threading;namespace TCPClient public partial class Form1 : Form public Socket newclient; public bool Connected; public Thread myThread; public de
3、legate void MyInvoke(string str); public Form1() InitializeComponent(); public void Connect() byte data = new byte1024; newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string ipadd = serverIP.Text.Trim(); int port = Convert.ToInt32(serverPort.Text.Trim(); IPE
4、ndPoint ie = new IPEndPoint(IPAddress.Parse(ipadd), port); try newclient.Connect(ie); connect.Enabled = false; Connected = true; catch (SocketException e) MessageBox.Show("连¢?接¨®服¤t务?器¡Â失º¡ì败㨹 " + e.Message); return; ThreadStart
5、 myThreaddelegate = new ThreadStart(ReceiveMsg); myThread = new Thread(myThreaddelegate); myThread.Start(); public void ReceiveMsg() while (true) byte data = new byte1024; int recv = newclient.Receive(data); string stringdata = Encoding.UTF8.GetString(data, 0, recv); showMsg(stringdata + "rn&qu
6、ot;); /receiveMsg.AppendText(stringdata + "rn"); public void showMsg(string msg) /在¨²线?程¨¬里¤?以°?安ã2全¨?方¤?式º?调Ì¡Â用®?控?件t if (receiveMsg.InvokeRequired) MyInvoke _myinvoke = new MyInvoke(showMsg); receiveMsg.Invoke(_myinvo
7、ke, new object msg ); else receiveMsg.AppendText(msg); private void SendMsg_Click(object sender, EventArgs e) int m_length = mymessage.Text.Length; byte data = new bytem_length; data = Encoding.UTF8.GetBytes(mymessage.Text); int i = newclient.Send(data); showMsg("我¨°说¦Ì:
8、4;o" + mymessage.Text + "rn"); /receiveMsg.AppendText("我¨°说¦Ì:êo"+mymessage.Text + "rn"); mymessage.Text = "" /newclient.Shutdown(SocketShutdown.Both); private void connect_Click(object sender, EventArgs e) Connect(); TCPServer:us
9、ing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Sockets;using System.Threading;namespace TCPServer class Server /设¦¨¨定¡§端?口¨² static int port; int recv; public static string showip; public int SetPor
10、t get return port; set port = value; TCPClient:using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace TCPClient static class Program / <summary> / 应®|用®?程¨¬序¨°的Ì?主¡Â入¨?口¨²点Ì?。¡ê / </summary> STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业设备的安装制度
- 产品合规管理制度
- 中国师范生认证制度
- 二甲复审内审员培训课件
- 中国社会科学院世界经济与政治研究所2026年度公开招聘第一批专业技术人员6人备考题库及完整答案详解一套
- 2025-2030中国气体滚筒干燥机行业市场发展趋势与前景展望战略研究报告
- 三明市农业科学研究院关于2025年公开招聘专业技术人员备考题库及参考答案详解一套
- 2025-2030中国直流电子负载行业市场发展趋势与前景展望战略研究报告
- 中国热带农业科学院院属单位2026年第一批公开招聘工作人员备考题库有答案详解
- 2025至2030新能源电池行业竞争格局分析及未来趋势与投资机会研究报告
- 2025北京陈经纶中学高一9月月考物理(贯通班)试题含答案
- 中国铝矿行业现状分析报告
- 物业人员消防安全培训课件
- 服装销售年底总结
- 2025年大学大四(预防医学)环境卫生学阶段测试试题及答案
- 文物安全保护责任书范本
- 产房护士长年度工作业绩总结与展望
- 【初中 历史】2025-2026学年统编版八年级上学期历史总复习 课件
- 2025~2026学年黑龙江省哈尔滨市道里区第七十六中学校九年级上学期9月培优(四)化学试卷
- 2025年律师事务所党支部书记年终述职报告
- 中国脑小血管病诊治指南2025
评论
0/150
提交评论