TCP实验关于C#的CS编程_第1页
TCP实验关于C#的CS编程_第2页
TCP实验关于C#的CS编程_第3页
TCP实验关于C#的CS编程_第4页
TCP实验关于C#的CS编程_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论