当前位置:文档之家› 使用socket进行通信程序设计

使用socket进行通信程序设计

使用socket进行通信程序设计
使用socket进行通信程序设计

使用socket进行通信程序设计姓名:

学号:

专业:

2015年10月30日

引言:

“一切皆Socket!”。这话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket。当前是信息时代,网络的快速普及势不可挡,各种新兴应用也如雨后春笋般,层出不穷。利用socket通信拥有即时通信功能的网络应用——聊天室,也因其为用户提供了实时性对话的渠道,深受青睐。在本课程设计中,我个人选择C#语言实现了一个界面友好的网络聊天室,包括服务器端和客户端两个程序,可以支持多人进行文字聊天。

基本原理:

1、客户机/服务器模式

在TCP/IP网络中两个进程间相互作用的主机模式是客户机/服务器模式(Client/Server model)。该模式的建立基于以下两点:1、非对等作用;2、通信完全是异步的。客户机/服务器模式在操作过程中采取的是主动请示方式:首先服务器方要先启动,并根据请示提供相应服务:(过程如下)

①打开一个通信通道(端口)并告知本地主机,并在某一个公认地址上接收客户请求;

②等待客户请求到达该端口;

③接收到重复服务请求,处理该请求并发送应答信号;

④返回第二步,等待另一客户请求;

⑤关闭服务器。

客户方:

①打开一个通信通道,并连接到服务器所在主机的特定端口;

②向服务器发送服务请求报文,等待并接收应答;继续提出请求……

③请求结束后关闭通信通道并终止。

2、套接字

套接字是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。可以将套接字看作不同主机间的进程进行双向通信的端点,它构成了单个主机内及整个网络间的编程界面。套接字存在于通信域中,通信域是为了处理一般的线程通过套接字通信而引进的一种抽象概念。套接字通常和同一个域中的套接字交换数据(数据交换也可能穿越域的界限,但这时一定要执行某种解释程序)。各种进程使用这个相同的域互相之间用Internet协议簇来进行通信。

套接字可以根据通信性质分类,这种性质对于用户是可见的。应用程序一般仅在同一类的套接字间进行通信。不过只要底层的通信协议允许,不同类型的套接字间也照样可以通信。套接字有两种不同的类型:流套接字和数据报套接字。

套接字工作原理:

要通过互联网进行通信,你至少需要一对套接字,其中一个运行于客户机端,我们称之为ClientSocket,另一个运行于服务器端,我们称之为ServerSocket。

根据连接启动的方式以及本地套接字要连接的目标,套接字之间的连接过程可以分为三个步骤:服务器监听,客户端请求,连接确认。

所谓服务器监听,是服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态。

所谓客户端请求,是指由客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。

所谓连接确认,是指当服务器端套接字监听到或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户端,一旦客户端确认了此描述,连接就建立好了。而

服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。

3、C# Socket

在C#中提供了两种网络服务,一种是Socket类,另一种是TcpListener(服务器),TcpClient(客户端);

Socket 类为网络通信提供了一套丰富的方法和属性。Socket 类允许您使用ProtocolType枚举中所列出的任何一种协议执行异步和同步数据传输。

https://www.doczj.com/doc/5614820293.html, Framework为应用程序访问Internet提供了分层的、可扩

展的以及受管辖的网络服务,其名字空间https://www.doczj.com/doc/5614820293.html,和https://www.doczj.com/doc/5614820293.html,.Sockets

包含丰富的类可以开发多种网络应用程序。.Net类采用的分层结构允许应用程

序在不同的控制级别上访问网络,开发人员可以根据需要选择针对不同的级别编制程序,这些级别几乎囊括了Internet的所有需要--从socket套接字到普通的请求/响应,更重要的是,这种分层是可以扩展的,能够适应Internet不断扩展的需要。

抛开ISO/OSI模型的7层构架,单从TCP/IP模型上的逻辑层面上看,.Net 类可以视为包含3个层次:请求/响应层、应用协议层、传输层。WebReqeust和WebResponse 代表了请求/响应层,支持Http、Tcp和Udp的类组成了应用协议层,而Socket类处于传输层。

传输层位于这个结构的最底层,当其上面的应用协议层和请求/响应层不能满足应用程序的特殊需要时,就需要使用这一层进行Socket套接字编程。

而在.Net中,https://www.doczj.com/doc/5614820293.html,.Sockets 命名空间为需要严密控制网络访问的开发人员提供了 Windows Sockets (Winsock) 接口的托管实现。https://www.doczj.com/doc/5614820293.html, 命名空间中的所有其他网络访问类都建立在该套接字Socket实现之上,如TCPClient、TCPListener 和 UDPClient 类封装有关创建到 Internet 的 TCP 和 UDP 连接的详细信息;NetworkStream类则提供用于网络访问的基础数据流等,常见的许多Internet服务都可以见到Socket的踪影,如Telnet、Http、Email、Echo 等,这些服务尽管通讯协议Protocol的定义不同,但是其基础的传输都是采用的Socket。

其实,Socket可以象流Stream一样被视为一个数据通道,这个通道架设在应用程序端(客户端)和远程服务器端之间,而后,数据的读取(接收)和写入(发送)均针对这个通道来进行。

可见,在应用程序端或者服务器端创建了Socket对象之后,就可以使用Send/SentTo方法将数据发送到连接的Socket,或者使用Receive/ReceiveFrom

方法接收来自连接Socket的数据;

针对Socket编程,.NET 框架的 Socket 类是 Winsock32 API 提供的套接字服务的托管代码版本。其中为实现网络编程提供了大量的方法,大多数情况下,Socket 类方法只是将数据封送到它们的本机 Win32 副本中并处理任何必要的

安全检查。

使用socket其间用到的方法/函数有:

Socket.Connect方法:建立到远程设备的连接

public void Connect(EndPoint remoteEP)(有重载方法)

Socket.Send 方法:从数据中的指示位置开始将数据发送到连接的Socket。

public int Send(byte[], int, SocketFlags);(有重载方法)

Socket.SendTo 方法将数据发送到特定终结点。

public int SendTo(byte[], EndPoint);(有重载方法)

Socket.Receive方法:将数据从连接的 Socket 接收到接收缓冲区的特

定位置。

public int Receive(byte[],int,SocketFlags);

Socket.ReceiveFrom方法:接收数据缓冲区中特定位置的数据并存储终结点。

public int ReceiveFrom(byte[], int, SocketFlags, ref EndPoint);

Socket.Bind 方法:使 Socket 与一个本地终结点相关联:

public void Bind( EndPoint localEP );

Socket.Listen方法:将 Socket 置于侦听状态。

public void Listen( int backlog );

Socket.Accept方法:创建新的 Socket 以处理传入的连接请求。

public Socket Accept();

Socket.Shutdown方法:禁用某 Socket 上的发送和接收

public void Shutdown( SocketShutdown how );

Socket.Close方法:强制 Socket 连接关闭

public void Close();

TcpClient 类基于 Socket 类构建,这是它能够以更高的抽象程度提供 TCP 服务的基础。因此许多应用层上的通讯协议,比如FTP(File Transfers Protocol)文件传输协议、HTTP(Hypertext Transfers Protocol)超文本传输协议等都直接创建在TcpClient等类之上。

TcpClient类,TcpListener类提供了一些简单的方法,用于在各模式下通过网络来连接、发送和接收流数据。为使TcpClient连接并交换数据,使用TCP ProtocolType创建的TcpListener或Socket必须侦听是否有传入的连接请求。可以使用下面两种方法之一连接到该侦听器:

(1)创建一个TcpClient,并调用三个可用的Connect方法之一。

(2)使用远程主机的主机名和端口号创建TcpClient。此构造函数将自动尝试一个连接。

TcpClient和TcpListener使用NetworkStream类表示网络。使用GetStream 方法返回网络流,然后调用该流的Read和Write方法。NetworkStream不拥有协议类的基础套接字,因此关闭它并不影响套接字。

TCPClient 类使用 TCP 从 Internet 资源请求数据。TCP 协议建立与远程终结点的连接,然后使用此连接发送和接收数据包。TCP 负责确保将数据包发送到终结点并在数据包到达时以正确的顺序对其进行组合。

从名字上就可以看出,TcpClient类专为客户端设计,它为 TCP 网络服务提供客户端连接。TcpClient提供了通过网络连接、发送和接收数据的简单方法。

基于以上,本次实验中我使用了TcpClient类和TcpListener。

主要数据结构:

主程序分成两部分,在服务器端由于要实现多用户通信,所以建立一个Client类用于存放各个连接服务器端客户的信息。该类的主要结构如下:class Client

{

public TcpClient client { get; private set; }

public BinaryReader br { get; private set; }

public BinaryWriter bw { get; private set; }

public string clientName { get; set; }

public Client(TcpClient client){}

public void Close(){}

}

其余程序部分没有特别需要说明的数据结构,都是有关于socket通信的,在上面部分已经详述,在这里不作赘述。

主要程序流程:

服务器和客户端之间面向连接的基于套接字的系统调用时序图如下图所示

图1 Socket通信流程图

程序设计:

(一)服务端设计

1、利用VS2012应用程序向导生成C#程序框架。

2、设计界面如下:

设置属性如下:

namespace chatserver

{

partialclass FServer

{

///

/// Required designer variable.

///

private https://www.doczj.com/doc/5614820293.html,ponentModel.IContainer components = null;

///

/// Clean up any resources being used.

///

///true if managed resources should be disposed; otherwise, false.

protectedoverridevoid Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows Form Designer generated code

///

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

///

privatevoid InitializeComponent()

{

https://www.doczj.com/doc/5614820293.html,bel1 = new https://www.doczj.com/doc/5614820293.html,bel();

this.txtIPInfo = new System.Windows.Forms.TextBox();

https://www.doczj.com/doc/5614820293.html,bel2 = new https://www.doczj.com/doc/5614820293.html,bel();

this.txtPortInfo = new System.Windows.Forms.TextBox();

this.btnServerStart = new System.Windows.Forms.Button();

https://www.doczj.com/doc/5614820293.html,bel3 = new https://www.doczj.com/doc/5614820293.html,bel();

this.richTxtMessage = new System.Windows.Forms.RichTextBox(); this.btnStopServer = new System.Windows.Forms.Button();

https://www.doczj.com/doc/5614820293.html,bel4 = new https://www.doczj.com/doc/5614820293.html,bel();

this.richTxtOnlineUser = new System.Windows.Forms.RichTextBox(); this.SuspendLayout();

//

// label1

//

https://www.doczj.com/doc/5614820293.html,bel1.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,bel1.Location = new System.Drawing.Point(33, 24);

https://www.doczj.com/doc/5614820293.html, = "label1";

https://www.doczj.com/doc/5614820293.html,bel1.Size = new System.Drawing.Size(41, 12);

https://www.doczj.com/doc/5614820293.html,bel1.TabIndex = 0;

https://www.doczj.com/doc/5614820293.html,bel1.Text = "IP地址";

//

// txtIPInfo

//

this.txtIPInfo.Location = new System.Drawing.Point(80, 21); https://www.doczj.com/doc/5614820293.html, = "txtIPInfo";

this.txtIPInfo.Size = new System.Drawing.Size(94, 21);

this.txtIPInfo.TabIndex = 1;

this.txtIPInfo.Text = "127.0.0.1";

//

// label2

//

https://www.doczj.com/doc/5614820293.html,bel2.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,bel2.Location = new System.Drawing.Point(219, 24);

https://www.doczj.com/doc/5614820293.html, = "label2";

https://www.doczj.com/doc/5614820293.html,bel2.Size = new System.Drawing.Size(95, 12);

https://www.doczj.com/doc/5614820293.html,bel2.TabIndex = 2;

https://www.doczj.com/doc/5614820293.html,bel2.Text = "端口号(0-65535)";

//

// txtPortInfo

//

this.txtPortInfo.Location = new System.Drawing.Point(320, 20);

https://www.doczj.com/doc/5614820293.html, = "txtPortInfo";

this.txtPortInfo.Size = new System.Drawing.Size(79, 21);

this.txtPortInfo.TabIndex = 3;

this.txtPortInfo.Text = "1";

//

// btnServerStart

//

this.btnServerStart.Location = new System.Drawing.Point(433, 17); https://www.doczj.com/doc/5614820293.html, = "btnServerStart";

this.btnServerStart.RightToLeft = System.Windows.Forms.RightToLeft.No; this.btnServerStart.Size = new System.Drawing.Size(80, 25);

this.btnServerStart.TabIndex = 4;

this.btnServerStart.Text = "启动服务器";

https://www.doczj.com/doc/5614820293.html,eVisualStyleBackColor = true;

this.btnServerStart.Click += new

System.EventHandler(this.btnServerStart_Click);

//

// label3

//

https://www.doczj.com/doc/5614820293.html,bel3.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,bel3.Location = new System.Drawing.Point(219, 64);

https://www.doczj.com/doc/5614820293.html, = "label3";

https://www.doczj.com/doc/5614820293.html,bel3.Size = new System.Drawing.Size(53, 12);

https://www.doczj.com/doc/5614820293.html,bel3.TabIndex = 5;

https://www.doczj.com/doc/5614820293.html,bel3.Text = "聊天内容";

//

// richTxtMessage

//

this.richTxtMessage.Location = new System.Drawing.Point(221, 89); https://www.doczj.com/doc/5614820293.html, = "richTxtMessage";

this.richTxtMessage.RightToLeft = System.Windows.Forms.RightToLeft.No; this.richTxtMessage.Size = new System.Drawing.Size(397, 303);

this.richTxtMessage.TabIndex = 6;

this.richTxtMessage.Text = "";

//

// btnStopServer

//

this.btnStopServer.FlatStyle = System.Windows.Forms.FlatStyle.System; this.btnStopServer.Location = new System.Drawing.Point(543, 17); https://www.doczj.com/doc/5614820293.html, = "btnStopServer";

this.btnStopServer.Size = new System.Drawing.Size(80, 25);

this.btnStopServer.TabIndex = 10;

this.btnStopServer.Text = "停止服务器";

https://www.doczj.com/doc/5614820293.html,eVisualStyleBackColor = true;

this.btnStopServer.Click += new

System.EventHandler(this.btnStopServer_Click);

//

// label4

//

https://www.doczj.com/doc/5614820293.html,bel4.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,bel4.Location = new System.Drawing.Point(26, 64);

https://www.doczj.com/doc/5614820293.html, = "label4";

https://www.doczj.com/doc/5614820293.html,bel4.Size = new System.Drawing.Size(77, 12);

https://www.doczj.com/doc/5614820293.html,bel4.TabIndex = 11;

https://www.doczj.com/doc/5614820293.html,bel4.Text = "当前在线用户";

//

// richTxtOnlineUser

//

this.richTxtOnlineUser.Location = new System.Drawing.Point(28, 89); https://www.doczj.com/doc/5614820293.html, = "richTxtOnlineUser";

this.richTxtOnlineUser.Size = new System.Drawing.Size(146, 303); this.richTxtOnlineUser.TabIndex = 12;

this.richTxtOnlineUser.Text = "";

//

// FServer

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(648, 418);

this.Controls.Add(this.richTxtOnlineUser);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,bel4);

this.Controls.Add(this.btnStopServer);

this.Controls.Add(this.richTxtMessage);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,bel3);

this.Controls.Add(this.btnServerStart);

this.Controls.Add(this.txtPortInfo);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,bel2);

this.Controls.Add(this.txtIPInfo);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,bel1);

https://www.doczj.com/doc/5614820293.html, = "FServer";

this.Text = "服务端";

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private https://www.doczj.com/doc/5614820293.html,bel label1;

private System.Windows.Forms.TextBox txtIPInfo;

private https://www.doczj.com/doc/5614820293.html,bel label2;

private System.Windows.Forms.TextBox txtPortInfo;

private System.Windows.Forms.Button btnServerStart;

private https://www.doczj.com/doc/5614820293.html,bel label3;

private System.Windows.Forms.RichTextBox richTxtMessage; private System.Windows.Forms.Button btnStopServer;

private https://www.doczj.com/doc/5614820293.html,bel label4;

private System.Windows.Forms.RichTextBox richTxtOnlineUser; }

}

3、设计Client类并编写

4、按照通信流程进行相应代码编写

5、进行相应测试并作相关修改

(二)客户端设计

1、依照服务端同样的方法创建客户端界面,并设置工程属性。

设置属性如下:

namespace AsyncTcpClient

{

partialclass FClient

{

///

///必需的设计器变量。

///

private https://www.doczj.com/doc/5614820293.html,ponentModel.IContainer components = null;

///

///清理所有正在使用的资源。

///

///如果应释放托管资源,为 true;否则为 false。

protectedoverridevoid Dispose(bool disposing)

{

if (disposing && (components != null))

{

components.Dispose();

}

base.Dispose(disposing);

}

#region Windows 窗体设计器生成的代码

///

///设计器支持所需的方法 - 不要

///使用代码编辑器修改此方法的内容。

///

privatevoid InitializeComponent()

{

https://www.doczj.com/doc/5614820293.html,bel1 = new https://www.doczj.com/doc/5614820293.html,bel();

this.txt_UserName = new System.Windows.Forms.TextBox();

this.btn_Login = new System.Windows.Forms.Button();

this.groupBoxOnline = new System.Windows.Forms.GroupBox();

this.FirstOnlineUser = new System.Windows.Forms.ListBox();

this.groupBoxDialog = new System.Windows.Forms.GroupBox();

this.rtb_MessageInfo = new System.Windows.Forms.RichTextBox();

this.groupBox3 = new System.Windows.Forms.GroupBox();

this.btn_SendMessage = new System.Windows.Forms.Button();

this.rtb_SendMessage = new System.Windows.Forms.RichTextBox();

https://www.doczj.com/doc/5614820293.html,belIPInfo = new https://www.doczj.com/doc/5614820293.html,bel();

https://www.doczj.com/doc/5614820293.html,bel3 = new https://www.doczj.com/doc/5614820293.html,bel();

this.txt_IPInfo = new System.Windows.Forms.TextBox();

this.txt_PortInfo = new System.Windows.Forms.TextBox();

this.rtb_StatusInfo = new System.Windows.Forms.RichTextBox();

this.groupBoxOnline.SuspendLayout();

this.groupBoxDialog.SuspendLayout();

this.groupBox3.SuspendLayout();

this.SuspendLayout();

//

// label1

//

https://www.doczj.com/doc/5614820293.html,bel1.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,bel1.Location = new System.Drawing.Point(9, 81);

https://www.doczj.com/doc/5614820293.html, = "label1";

https://www.doczj.com/doc/5614820293.html,bel1.Size = new System.Drawing.Size(53, 12);

https://www.doczj.com/doc/5614820293.html,bel1.TabIndex = 0;

https://www.doczj.com/doc/5614820293.html,bel1.Text = "用户名:";

//

// txt_UserName

//

this.txt_UserName.Location = new System.Drawing.Point(68, 78);

this.txt_https://www.doczj.com/doc/5614820293.html, = "txt_UserName";

this.txt_UserName.Size = new System.Drawing.Size(171, 21);

this.txt_UserName.TabIndex = 1;

//

// btn_Login

//

this.btn_Login.Location = new System.Drawing.Point(11, 105);

this.btn_https://www.doczj.com/doc/5614820293.html, = "btn_Login";

this.btn_Login.Size = new System.Drawing.Size(228, 33);

this.btn_Login.TabIndex = 2;

this.btn_Login.Text = "登陆";

this.btn_https://www.doczj.com/doc/5614820293.html,eVisualStyleBackColor = true;

this.btn_Login.Click += new System.EventHandler(this.btn_Login_Click); //

// groupBoxOnline

//

this.groupBoxOnline.Controls.Add(this.FirstOnlineUser);

this.groupBoxOnline.Location = new System.Drawing.Point(11, 144); https://www.doczj.com/doc/5614820293.html, = "groupBoxOnline";

this.groupBoxOnline.Size = new System.Drawing.Size(228, 200);

this.groupBoxOnline.TabIndex = 3;

this.groupBoxOnline.TabStop = false;

this.groupBoxOnline.Text = "当前在线";

//

// FirstOnlineUser

//

this.FirstOnlineUser.FormattingEnabled = true;

this.FirstOnlineUser.ItemHeight = 12;

this.FirstOnlineUser.Location = new System.Drawing.Point(1, 17); https://www.doczj.com/doc/5614820293.html, = "FirstOnlineUser";

this.FirstOnlineUser.Size = new System.Drawing.Size(216, 172); this.FirstOnlineUser.TabIndex = 0;

//

// groupBoxDialog

//

this.groupBoxDialog.Controls.Add(this.rtb_MessageInfo);

this.groupBoxDialog.Location = new System.Drawing.Point(255, 12); https://www.doczj.com/doc/5614820293.html, = "groupBoxDialog";

this.groupBoxDialog.Size = new System.Drawing.Size(385, 221); this.groupBoxDialog.TabIndex = 4;

this.groupBoxDialog.TabStop = false;

this.groupBoxDialog.Text = "对话信息";

//

// rtb_MessageInfo

//

this.rtb_MessageInfo.Location = new System.Drawing.Point(15, 20); this.rtb_https://www.doczj.com/doc/5614820293.html, = "rtb_MessageInfo";

this.rtb_MessageInfo.Size = new System.Drawing.Size(351, 195); this.rtb_MessageInfo.TabIndex = 0;

this.rtb_MessageInfo.Text = "";

//

// groupBox3

//

this.groupBox3.Controls.Add(this.btn_SendMessage);

this.groupBox3.Controls.Add(this.rtb_SendMessage);

this.groupBox3.Location = new System.Drawing.Point(255, 239); https://www.doczj.com/doc/5614820293.html, = "groupBox3";

this.groupBox3.Size = new System.Drawing.Size(385, 98);

this.groupBox3.TabIndex = 5;

this.groupBox3.TabStop = false;

this.groupBox3.Text = "发送信息";

//

// btn_SendMessage

//

this.btn_SendMessage.Location = new System.Drawing.Point(323, 41); this.btn_https://www.doczj.com/doc/5614820293.html, = "btn_SendMessage";

this.btn_SendMessage.Size = new System.Drawing.Size(56, 23);

this.btn_SendMessage.TabIndex = 3;

this.btn_SendMessage.Text = "发送";

this.btn_https://www.doczj.com/doc/5614820293.html,eVisualStyleBackColor = true;

this.btn_SendMessage.Click += new

System.EventHandler(this.btn_SendeMessage_Click);

//

// rtb_SendMessage

//

this.rtb_SendMessage.Location = new System.Drawing.Point(15, 20); this.rtb_https://www.doczj.com/doc/5614820293.html, = "rtb_SendMessage";

this.rtb_SendMessage.Size = new System.Drawing.Size(302, 71); this.rtb_SendMessage.TabIndex = 1;

this.rtb_SendMessage.Text = "";

//

// labelIPInfo

//

https://www.doczj.com/doc/5614820293.html,belIPInfo.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,belIPInfo.Location = new System.Drawing.Point(13, 13); https://www.doczj.com/doc/5614820293.html, = "labelIPInfo";

https://www.doczj.com/doc/5614820293.html,belIPInfo.Size = new System.Drawing.Size(41, 12);

https://www.doczj.com/doc/5614820293.html,belIPInfo.TabIndex = 7;

https://www.doczj.com/doc/5614820293.html,belIPInfo.Text = "IP地址";

//

// label3

//

https://www.doczj.com/doc/5614820293.html,bel3.AutoSize = true;

https://www.doczj.com/doc/5614820293.html,bel3.Location = new System.Drawing.Point(13, 45);

https://www.doczj.com/doc/5614820293.html, = "label3";

https://www.doczj.com/doc/5614820293.html,bel3.Size = new System.Drawing.Size(107, 12);

https://www.doczj.com/doc/5614820293.html,bel3.TabIndex = 8;

https://www.doczj.com/doc/5614820293.html,bel3.Text = "端口号(0-65535)";

//

// txt_IPInfo

//

this.txt_IPInfo.Location = new System.Drawing.Point(68, 10); this.txt_https://www.doczj.com/doc/5614820293.html, = "txt_IPInfo";

this.txt_IPInfo.Size = new System.Drawing.Size(171, 21);

this.txt_IPInfo.TabIndex = 9;

this.txt_IPInfo.Text = "127.0.0.1";

//

// txt_PortInfo

//

this.txt_PortInfo.Location = new System.Drawing.Point(122, 42); this.txt_https://www.doczj.com/doc/5614820293.html, = "txt_PortInfo";

this.txt_PortInfo.Size = new System.Drawing.Size(117, 21);

this.txt_PortInfo.TabIndex = 10;

this.txt_PortInfo.Text = "1";

//

// rtb_StatusInfo

//

this.rtb_StatusInfo.Location = new System.Drawing.Point(11, 350); this.rtb_https://www.doczj.com/doc/5614820293.html, = "rtb_StatusInfo";

this.rtb_StatusInfo.Size = new System.Drawing.Size(629, 56);

this.rtb_StatusInfo.TabIndex = 6;

this.rtb_StatusInfo.Text = "";

//

// FClient

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(657, 412);

this.Controls.Add(this.txt_PortInfo);

this.Controls.Add(this.txt_IPInfo);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,bel3);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,belIPInfo);

this.Controls.Add(this.rtb_StatusInfo);

this.Controls.Add(this.groupBox3);

this.Controls.Add(this.groupBoxDialog);

this.Controls.Add(this.groupBoxOnline);

this.Controls.Add(this.btn_Login);

this.Controls.Add(this.txt_UserName);

this.Controls.Add(https://www.doczj.com/doc/5614820293.html,bel1);

https://www.doczj.com/doc/5614820293.html, = "FClient";

this.Text = "客户端";

this.FormClosing += new

System.Windows.Forms.FormClosingEventHandler(this.FormClient_FormClos ing);

this.groupBoxOnline.ResumeLayout(false);

this.groupBoxDialog.ResumeLayout(false);

this.groupBox3.ResumeLayout(false);

this.ResumeLayout(false);

this.PerformLayout();

}

#endregion

private https://www.doczj.com/doc/5614820293.html,bel label1;

private System.Windows.Forms.TextBox txt_UserName;

private System.Windows.Forms.Button btn_Login;

private System.Windows.Forms.GroupBox groupBoxOnline;

private System.Windows.Forms.GroupBox groupBoxDialog;

private System.Windows.Forms.RichTextBox rtb_MessageInfo;

private System.Windows.Forms.GroupBox groupBox3;

private System.Windows.Forms.RichTextBox rtb_SendMessage;

private System.Windows.Forms.Button btn_SendMessage;

private System.Windows.Forms.ListBox FirstOnlineUser;

private https://www.doczj.com/doc/5614820293.html,bel labelIPInfo;

private https://www.doczj.com/doc/5614820293.html,bel label3;

private System.Windows.Forms.TextBox txt_IPInfo;

private System.Windows.Forms.TextBox txt_PortInfo;

private System.Windows.Forms.RichTextBox rtb_StatusInfo;

}

}

2、按照流程编写相应socket通信代码

3、与服务器端共同进行测试并作相应修改、

封装方法:

编写好并测试完成全部需求确保无误后需要对代码进行封装打包成软件。由于从VS2012开始VS2012不再直接包含封装工具,所以需要下载安装installShield。相应步骤我参照了网络上的相应教程与视频,由于installshield需要访问的国外网站无法完成注册,所以我下了CSDN上网友分享的,没有激活,属于适用,所以最后封装过程中的general information 中的ID,NAME我选择了No。具体的部署安装与本project的主题关系不大,不作赘述。

安装方法:

安装步骤如下:

点击setup.exe,然后一路next指导finish就可以。

服务器端的安装与上面相同。

安装完成后,在“开始”中搜索“chat”结果如下:

可以打开相应程序。

相关主题
文本预览
相关文档 最新文档