当前位置:首页 > 开发教程 > .net教程 >

TCP / IP Server客户端实例第1部分

时间:2016-03-09 09:00 来源:互联网 作者:源码搜藏 收藏

在这个系列中,我们将要在看TCP IP,客户端/服务器和机会一个巨大的世界这方面的知识开辟了我们,从聊天和视频会议,有形无形的进程通信的东西,甚至是服务管理。 但是,今天,让我们的框架完成 下载 C#(91.2 KB) VB.NET(85.7 KB) 评级 div id=yourRati

在这个系列中,我们将要在看TCP IP,客户端/服务器和机会一个巨大的世界这方面的知识开辟了我们,从聊天和视频会议,有形无形的进程通信的东西,甚至是服务管理。但是,今天,让我们的框架完成

 
C#(91.2 KB)VB.NET(85.7 KB)
 
 
<div id="yourRating" "="" style="border: 0px; font-weight: inherit; font-style: inherit; margin: 0px; outline: 0px; padding: 0px; float: left;">
 
 
 
 
 
 
(16)
65593次
添加到收藏夹数
的Visual Studio 2012
2013年12月18日
MS-LPL
TCP / IP Server客户端实例第1部分 TCP / IP Server客户端实例第1部分 TCP / IP Server客户端实例第1部分 TCP / IP Server客户端实例第1部分 TCP / IP Server客户端实例第1部分
 
英语
C# VBWin32的Windows窗体WPF微软Azure.NET框架Visual Basic .NET中VB.Net.NET Framework 4.0中视窗将军Windows Phone的C#语言VB.NET语言功能的WinForms视窗8Visual Studio的2012的Windows Phone 8Windows应用商店应用
互操作控制动画图形C# 数据绑定异步编程安全GDI +身份验证ASP.NET文件系统类库用户界面游戏Windows窗体系列化体系结构和设计多线程导航微软Azure数据访问线程PowerShell的图片定制媒体自定义控件Web服务Windows窗体控件2D图形性能搜索VB.Net并行编程图像处理代码示例图片廊印刷影像控制台窗口.NET 4影像绘画如何UI设计一般C#可重用的代码文件系统网络Windows PowerShell中图像优化一般来说Windows 8中Windows窗体控件C#语言功能语言样本图形功能音频和视频设备和传感器Windows Web服务用户体验BitmapImage的Windows应用商店的应用程序加载图像


1      引言

在这个系列中,我们将要在看TCP IP,客户端/服务器和机会一个巨大的世界这方面的知识开辟了我们,从聊天和视频会议,有形无形的进程通信的东西,甚至是服务管理。但是,今天,让我们的框架内进行。

2      号楼示例

样品被内置在Visual Studio 2012旗舰版为x86的使用.NET Framework 4,我们将使用的NuGet包,一些第三方库的目标应用。所有这一切都将被充分解释你确保你的应用程序的最后汇编将是无忧无虑。哦! 和示例代码冗长的评论,所以你应该在工作中的代码做什么都没问题。

3      说明

沟通是我们所有人的根本。但是,这也是很多程序的基础。当应用程序使用它进行通信的数据库,连接当浏览器连接到一个网站,它传达和广大发生在互联网上是沟通确实通过TCP通信。在第一个例子中,我们将编写一个客户端和“回声服务器”,它只是你发送的邮件回复 - 很像ping命令。这听起来并不多,但它是我们从开始的基础。
TCP / IP Server客户端实例第1部分


4      创建我们的应用

打开Visual Studio 2012,并创建一个新项目。调用项目ClientApp。现在点击ClientApp项目在Solution Explorer并添加一个名为ServerApp一个新项目。这使得它很好的一个简单的为我们分开的客户端和服务器的逻辑和代码。
1.       相应地更改表格文本服务器和客户端。
2.       服务器是一个稍微复杂一些比客户端,所以我们会先设计出

4.1    服务器

·         添加StatusStrip中的服务器表格
Ø    两个Label添加到StatusStrip中并设置其文本属性到
§  连接数
§  0
Ø    更改第二个标签名称lblNumberOfConnections
·         一个RichTextBox添加到窗体,并设置它的停靠属性设置为Fill
·         名称RichTextBox的rtbServer

4.2    客户端

·         一个RichTextBox添加到窗体,并设置它的停靠属性设置为Fill
·         名称RichTextBox的rtbClient

4.3    我们的代码

好了,现在我们已经创建了前端,可以格式化你RichTextBoxes喜欢的任何方式。我们需要开始我们的编码。同样,我们将与服务器启动。

4.4    服务器代码

我们的服务器做了三件事
·         它会等待直到客户端连接
 

 
 

C#
 
 private void ListenForClients() 
        { 
            this.tcpListener.Start(); 
 
            while (true// Never ends until the Server is closed. 
            { 
                //blocks until a client has connected to the server 
                TcpClient client = this.tcpListener.AcceptTcpClient(); 
 
                //create a thread to handle communication  
                //with connected client 
                connectedClients++; // Increment the number of clients that have communicated with us. 
                lblNumberOfConnections.Text = connectedClients.ToString(); 
 
                Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
                clientThread.Start(client); 
            } 
        }
·          它回复与收到了同样的信息(回声)
 
 
C#
 
private void HandleClientComm(object client) 
        { 
            TcpClient tcpClient = (TcpClient)client; 
            NetworkStream clientStream = tcpClient.GetStream(); 
 
            byte[] message = new byte[4096]; 
            int bytesRead; 
 
            while (true) 
            { 
                bytesRead = 0; 
 
                try 
                { 
                    //blocks until a client sends a message 
                    bytesRead = clientStream.Read(message, 04096); 
                } 
                catch 
                { 
                    //a socket error has occured 
                    break; 
                } 
 
                if (bytesRead == 0) 
                { 
                    //the client has disconnected from the server 
                    connectedClients--; 
                    lblNumberOfConnections.Text = connectedClients.ToString(); 
                    break; 
                } 
 
                //message has successfully been received 
                ASCIIEncoding encoder = new ASCIIEncoding(); 
 
                // Convert the Bytes received to a string and display it on the Server Screen 
                string msg = encoder.GetString(message, 0, bytesRead); 
                WriteMessage(msg); 
 
                // Now Echo the message back 
 
                Echo(msg, encoder, clientStream); 
            } 
 
            tcpClient.Close(); 
        }
 
 

4.5    客户端代码

我们的客户做了三件事
·         它连接到服务器
 
C#

					
 client.Connect(serverEndPoint);
·          它发出了一个消息
 
 
C#

					
NetworkStream clientStream = client.GetStream(); 
 
            ASCIIEncoding encoder = new ASCIIEncoding(); 
            byte[] buffer = encoder.GetBytes(msg); 
 
            clientStream.Write(buffer, 0, buffer.Length); 
            clientStream.Flush(); 
 
            // Receive the TcpServer.response. 
 
            // Buffer to store the response bytes. 
            Byte[]  data = new Byte[256];
 
C#
 //字符串存储响应ASCII码表示。 
   
 
            // Read the first batch of the TcpServer response bytes. 
            Int32 bytes = clientStream.Read(data, 0, data.Length); 
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); 
             
            rtbClient.AppendText(Environment.NewLine + "From Server: " + responseData);
 
 
 

4.5.1    源代码文件

·          源代码ClientApp:Form1.cs中-主界面我们的客户端应用程序 
·          源代码ServerApp:Form1.cs中-主界面我们的服务器应用程序 
 
在接下来的这个系列,我们将开始做有趣的事情!


.net教程阅读排行

最新文章