已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
组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;using 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 delegate 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(); IPEndPoint 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 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); /receiveMsg.AppendText(stringdata + rn); public void showMsg(string msg) /在线?程里?以?安2全?方?式?调用?控?件t if (receiveMsg.InvokeRequired) MyInvoke _myinvoke = new MyInvoke(showMsg); receiveMsg.Invoke(_myinvoke, 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(我说: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:using 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 SetPort 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 / / 应|用?程序的?主入?口点?。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new F
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中政治选择性必修三《逻辑与思维》12.2 逆向思维的含义与作用教学设计
- 2026年(完整版)安全生产教育培训试题含答案(完整版)
- 2026年国家电网公司华中分部人员招聘考试备考题库及详细答案解析
- 2026年安全工程师-安全生产专业实务(道路运输安全)考试历年模拟题含答案
- 2026年病区探视陪护人员院感管理试题(含答案)
- 小学四年级道德与法治教学设计:探秘家乡风俗 厚植文化情怀
- 2026年湖北省储备粮油管理有限公司人员招聘笔试备考题库及详细答案解析
- 2026年哈尔滨住房建设发展集团有限责任公司人员招聘考试模拟试题及详细答案解析
- 2026年云南电信人员招聘笔试参考题库及详细答案解析
- 2026年北京市烟草专卖局人员招聘考试参考题库及详细答案解析
- 木材加工厂安全培训课件
- DGTJ08-2065-2020 住宅二次供水技术标准 附条文说明
- 微型小说教学课件
- GB/T 13029.1-2025船舶电气装置第1部分:电缆的选择和安装
- 高等数学(上册)(慕课版)课件 第1章 习题课
- 合唱课题申报书
- 2025年八年级上学期地理早背晚默资料
- 云南民族大学附属高级中学2026届高三上学期联考(一)地理试卷(含解析)
- 中药材采购合同协议模板
- 企业内部控制系统模板
- 底商出租管理办法
评论
0/150
提交评论