版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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年广东省信宜市高三生物上册期末考试模拟测试卷(培优A卷)附答案
- 2026年河北省南宫市高一化学上册期末考试模拟考试卷含完整答案【有一套】
- 湖北省武汉市汉阳2026届中考适应性考试物理试题含解析
- 2026湖南永州市潇湘能源集团招聘员工5人笔试历年参考题库附带答案详解
- 2026浙江温州市洞头人才发展有限公司招聘2人(营业员)笔试历年参考题库附带答案详解
- 2026浙江杭州华枫创业服务发展有限公司招聘合同制员工拟聘用人员笔试历年参考题库附带答案详解
- 2026年河北省河间市高一化学上册期末考试模拟测试卷及完整答案(名师系列)
- 2026河南郑州市金水人才集团有限公司法律复议岗招聘5人笔试历年参考题库附带答案详解
- 2025年黑龙江省齐齐哈尔市铁锋区某国企招聘工作人员若干人备考题库及答案详解(易错题)
- 2026河北廊坊市文安县京南文化旅游事业发展有限公司招聘1人笔试历年参考题库附带答案详解
- 2025年交通运输概论考试试题及答案
- 五下道法 全册必背120个考点26春
- 天津中考:历史高频考点总结
- 2026年地铁站务员面试常见问题
- 2026苏教版(新教材)小学科学二年级下册《探秘玩具》单元综合测试卷及答案(2套)
- 2026年中央安全生产考核巡查明查暗访清单
- DB11∕T 2503-2025 地理标志产品质量要求 京白梨
- 2026及未来5年中国清洁供热行业全景调研及竞争格局预测报告
- GB/T 44067.4-2025工业互联网平台技术要求及测试方法第4部分:边缘计算平台
- 分子诊断设备技师精准操作能力标准
- 2025年华南理工综评面试题库及答案
评论
0/150
提交评论