本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程。本产品已经成熟稳定并投入商用。
在线演示环境:https://kf.shengxunwei.com 注意:演示环境仅供演示交流与评估,不保证 7x24 小时可用。安全
文章目录列表请点击这里服务器
对于在线客服与营销系统,客服端指的是后台提供服务的客服或营销人员,他们使用客服程序在后台观察网站的被访状况,开展营销活动或提供客户服务。在本篇文章中,我将详细介绍如何在 .net core 环境下使用 TCP 通讯技术实现稳定高效与安全的客服端程序。网络
这里存在几个技术难点须要注意:框架
访客端实现的效果:tcp
访客端在手机上的效果:函数
后台客服的实现效果:oop
TcpListener类提供了简单的方法,这些方法在阻止同步模式下侦听和接受传入链接请求。 可使用 TcpClient 或 Socket 来链接 TcpListener 。 TcpListener使用 IPEndPoint 、本地 IP 地址和端口号或仅端口号建立一个。 Any为本地 IP 地址指定,若是但愿基础服务提供商为你分配这些值,则为0。 若是你选择执行此操做,则能够在 LocalEndpoint 套接字链接后使用属性来标识分配的信息。网站
使用 Start 方法开始侦听传入链接请求。 Start将排队传入的链接,直到调用 Stop 方法或已将其排入队列 MaxConnections 。 使用 AcceptSocket 或 AcceptTcpClient 从传入链接请求队列请求链接。 这两种方法将会阻止。 若是要避免阻塞,能够 Pending 先使用方法来肯定队列中是否有链接请求。
调用 Stop 方法以关闭 TcpListener 。this
下面的代码示例建立一个 TcpListener 。.net
using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; class MyTcpListener { public static void Main() { TcpListener server=null; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while(true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also use server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while((i = stream.Read(bytes, 0, bytes.Length))!=0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // Process the data sent by the client. data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("Sent: {0}", data); } // Shutdown and end connection client.Close(); } } catch(SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } }
TcpClient 类提供了在同步阻止模式下经过网络链接、发送和接收流数据的简单方法。
为了 TcpClient 链接和交换数据, TcpListener Socket 使用 TCP 建立的或 ProtocolType 必须侦听传入链接请求。 能够经过如下两种方式之一链接到此侦听器:
咱们使用下面的代码创建一个客服端链接程序:
static void Connect(String server, String message) { try { // Create a TcpClient. // Note, for this client to work you need to have a TcpServer // connected to the same address as specified by the server, port // combination. Int32 port = 13000; TcpClient client = new TcpClient(server, port); // Translate the passed message into ASCII and store it as a Byte array. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); // Get a client stream for reading and writing. // Stream stream = client.GetStream(); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); Console.WriteLine("Sent: {0}", message); // Receive the TcpServer.response. // Buffer to store the response bytes. data = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); Console.WriteLine("Received: {0}", responseData); // Close everything. stream.Close(); client.Close(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine("\n Press Enter to continue..."); Console.Read(); }
本文对使用 TCP 协议搭建客服端通讯框架进行了简要的介绍,在接下来的文章中,我将具体解构服务端程序的结构和设计、客服端程序的结构和设计,敬请关注。
在线演示环境:https://kf.shengxunwei.com 注意:演示环境仅供演示交流与评估,不保证 7x24 小时可用。
联系QQ: 279060597 联系E-mail:C5118@outlook.com