




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学习项目投资管理办法
- 基金公司账户管理办法
- 福州地铁保护管理办法
- 药品生产合成管理办法
- 赴美陪同人员管理办法
- 就业不知资金管理办法
- 学位论文送审管理办法
- 药品注册管理办法目地
- 眉县扶贫资金管理办法
- 能源费用使用管理办法
- 中国热射病诊断与治疗指南(2025版)
- 公共艺术装置项目管理流程
- 2025年下半年佛山市南海区建筑工程质量检测站招考编外工作人员易考易错模拟试题(共500题)试卷后附参考答案
- GB/T 45610-2025煤矸石回填塌陷区复垦技术规程
- 二级注册建造师继续教育题库附答案
- 中医基础执业医师考试试题及答案
- 夏季安全生产八防
- 2025-2030年中国写字楼行业市场深度调研及前景趋势与投资研究报告
- 人教版(2024)七年级下册英语Unit 8 Once upon a Time单元集体备课教案(共5课时)
- 上海2025年上海市第一批面向西藏籍毕业生招聘笔试历年参考题库附带答案详解
- 诊所托管合同协议
评论
0/150
提交评论