首页 > 编程语言 >websocket与C# socket相互通信

websocket与C# socket相互通信

时间:2023-04-23 13:57:07浏览次数:47  
标签:websocket socket temp C# Length ws using byte string

web端代码就是js代码,C#有两种方式:使用第三方库,如Fleck,使用C#原生socket编程实现   web端:

<!doctype html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <title>下发网站上文件到学生机</title>
        <script type="text/javascript">
            function callDesktopReceiveFile(button) {
                var ws = null;
                if (button.innerHTML == '下发') {
                    button.innerHTML = '取消';

                    try {
                        if (ws) ws.close();
                    } catch(e) {
                        console.log(e)
                    }

                    ws = new WebSocket('ws://127.0.0.1:14567');
                    //监听是否连接成功
                    ws.onopen = function () {
                        console.log('ws连接状态[成功]:' + ws.readyState);
                        ws.send("content:receiveFile;url:http://127.0.0.1:5500/2023CQGKMNT.dat;");
                        // ws.close();
                        console.log('发送消息');
                    };

                    // 接听服务器发回的信息并处理展示
                    ws.onmessage = function (e) {
                        console.log('接收到来自服务器的消息:' + e.data);
                        // 如果服务器在业务上主动关闭连接,则此处无需再关闭
                        // if (e.data === 'roger') {
                        //     ws.close();
                        // }
                    };

                    // 监听连接关闭事件
                    ws.onclose = function () {
                        // 监听整个过程中websocket的状态
                        console.log('ws连接状态[关闭]:' + ws.readyState);
                    };

                    // 监听并处理error事件
                    ws.onerror = function (error) {
                        console.log(error);
                    };


                } else if (button.innerHTML == '取消') {
                    try {
                        if (ws) ws.close();
                    } catch(e) {
                        console.log(e)
                    }
                    button.innerHTML = '下发';
                }
            }


        </script>
    </head>
    <body>
        <div>
            <table border="1" cellspacing="0">
                <tr>
                    <th>试卷号</th>
                    <th>试卷名称</th>
                    <th>描述</th>
                    <th>操作</th>
                </tr>
                <tr>
                    <td>JCLX01</td>
                    <td>基础练习一</td>
                    <td>建账、会计期间设置、部门职员设置、银行账户设置、科目设置等</td>
                    <td><button id="btnDownload" onclick="callDesktopReceiveFile(this)">下发</button></td>
                </tr>
                <tr>
                    <td>JCLX02</td>
                    <td>基础练习二</td>
                    <td>建账、会计期间设置、部门职员设置、银行账户设置、科目设置等</td>
                    <td><button id="btnDownload" onclick="callDesktopReceiveFile(this)">下发</button></td>
                </tr>
            </table>
            
        </div>
    </body>
</html>
  C#端 方式一:使用第三方库Fleck 参考:http://chanpinxue.cn/archives/979.html   方式二:使用C#原生socket编程自行实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using U8FileTransfer.TcpHelper;
using System.Net.Sockets;
using System.Net;
using System.Security.Cryptography;

namespace CodeExperiment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Thread thread = new Thread(websocketListen);
            thread.IsBackground = true;
            thread.Start();
            
        }

        /// <summary>
        /// 解析客户端数据包,防止乱码
        /// </summary>
        /// <param name="recBytes">服务器接收的数据包</param>
        /// <param name="recByteLength">有效数据长度</param>
        /// <returns></returns>
        private static string AnalyticData(byte[] recBytes, int recByteLength)
        {
            if (recByteLength < 2) { return string.Empty; }
            bool fin = (recBytes[0] & 0x80) == 0x80; // 1bit,1表示最后一帧  
            if (!fin)
            {
                return string.Empty;// 超过一帧暂不处理 
            }
            bool mask_flag = (recBytes[1] & 0x80) == 0x80; // 是否包含掩码  
            if (!mask_flag)
            {
                return string.Empty;// 不包含掩码的暂不处理
            }
            int payload_len = recBytes[1] & 0x7F; // 数据长度  
            byte[] masks = new byte[4];
            byte[] payload_data;
            if (payload_len == 126)
            {
                Array.Copy(recBytes, 4, masks, 0, 4);
                payload_len = (UInt16)(recBytes[2] << 8 | recBytes[3]);
                payload_data = new byte[payload_len];
                Array.Copy(recBytes, 8, payload_data, 0, payload_len);
            }
            else if (payload_len == 127)
            {
                Array.Copy(recBytes, 10, masks, 0, 4);
                byte[] uInt64Bytes = new byte[8];
                for (int i = 0; i < 8; i++)
                {
                    uInt64Bytes[i] = recBytes[9 - i];
                }
                UInt64 len = BitConverter.ToUInt64(uInt64Bytes, 0);
                payload_data = new byte[len];
                for (UInt64 i = 0; i < len; i++)
                {
                    payload_data[i] = recBytes[i + 14];
                }
            }
            else
            {
                Array.Copy(recBytes, 2, masks, 0, 4);
                payload_data = new byte[payload_len];
                Array.Copy(recBytes, 6, payload_data, 0, payload_len);
            }
            for (var i = 0; i < payload_len; i++)
            {
                payload_data[i] = (byte)(payload_data[i] ^ masks[i % 4]);
            }
            return Encoding.UTF8.GetString(payload_data);
        }

        /// <summary>
        /// 打包服务器数据,防止乱码
        /// </summary>
        /// <param name="message">数据</param>
        /// <returns>数据包</returns>
        private static byte[] PackData(string message)
        {
            byte[] contentBytes = null;
            byte[] temp = Encoding.UTF8.GetBytes(message);
            if (temp.Length < 126)
            {
                contentBytes = new byte[temp.Length + 2];
                contentBytes[0] = 0x81;
                contentBytes[1] = (byte)temp.Length;
                Array.Copy(temp, 0, contentBytes, 2, temp.Length);
            }
            else if (temp.Length < 0xFFFF)
            {
                contentBytes = new byte[temp.Length + 4];
                contentBytes[0] = 0x81;
                contentBytes[1] = 126;
                contentBytes[2] = (byte)(temp.Length & 0xFF);
                contentBytes[3] = (byte)(temp.Length >> 8 & 0xFF);
                Array.Copy(temp, 0, contentBytes, 4, temp.Length);
            }
            else
            {
                // 暂不处理超长内容  
            }
            return contentBytes;
        }


        /// <summary>
        /// 客户端消息结构化
        /// message参数格式为多个key:value键值对通过分号拼接组成,示例:
        /// content:download_and_send;file-url:https://www.a.com/a.zip;
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private Dictionary<string, string> ProcessRemoteMessage(string message)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (message.Substring(message.Length - 1, 1) == ";")
            {
                message = message.Substring(0, message.Length - 1);
            }
            string[] strs = message.Split(';');
            if (strs.Length > 0)
            {
                Console.WriteLine("- - - - - - - - - - - - - - - - - - -");
                foreach (string s in strs)
                {
                    Console.WriteLine(s);
                    string[] split = s.Split(new char[] { ':' }, 2);
                    Console.WriteLine("[" + split[0] + "][" + split[1] + "]");
                    dic.Add(split[0], split[1]);
                }
                Console.WriteLine("- - - - - - - - - - - - - - - - - - -");
            }
            return dic;
        }

        private void websocketListen()
        {
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint endPoint = new IPEndPoint(IPAddress.Any, 14567);//监听端口
            server.Bind(endPoint);
            server.Listen(10); // 排队等待连接最大数量10

            // 监听多个客户端连接
            while (true)
            {
                Socket client = server.Accept();
                Console.WriteLine("有客户端连上来了");

                //接收客户端发来的HTTP-Header消息
                byte[] bytes = new byte[1024];
                int len = client.Receive(bytes);
                string strMessage = Encoding.UTF8.GetString(bytes, 0, len);
                Console.WriteLine(strMessage);

                //获取Sec-WebSocket-Key,为握手做准备
                string[] strings = strMessage.Split('\n');
                string strSecWebSocketKey = "";
                foreach (var item in strings)
                {
                    string[] strings1 = item.Split(':');
                    if (strings1[0] == "Sec-WebSocket-Key")
                    {
                        strSecWebSocketKey = strings1[1].Trim();
                    }
                }

                //生成服务端Sec-WebSocket-Accept,迎合客户端的握手请求
                byte[] secKeyBytes = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(strSecWebSocketKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
                string secKey = Convert.ToBase64String(secKeyBytes);

                // 发送给客户端完成握手(会触发Websocket的open()回调函数),针对websocket必须使用以下header
                string strHeader = "";
                strHeader += "HTTP/1.1 101 Switching Protocols" + Environment.NewLine;
                strHeader += "Upgrade: websocket" + Environment.NewLine;
                strHeader += "Connection: Upgrade" + Environment.NewLine;
                strHeader += "Sec-WebSocket-Accept: " + secKey + Environment.NewLine + Environment.NewLine;
                client.Send(Encoding.UTF8.GetBytes(strHeader));

                string remoteFileUrl = null;
                bool clientClose = false;
                // 循环接收websocket发来的消息实现双方交流
                while (!clientClose)
                {
                    //接收客户端发来的消息
                    byte[] bytes2 = new byte[1024];
                    int len2 = client.Receive(bytes2);
                    string strMessage2 = AnalyticData(bytes2, len2);
                    if (strMessage2.Length > 0)
                    {
                        Console.WriteLine("客户端发来消息:{0}", strMessage2);
                        Dictionary<string, string> messageDic = ProcessRemoteMessage(strMessage2);
                        string content = null;
                        messageDic.TryGetValue("content", out content);
                        Console.WriteLine("message content:" + content);
                        if (content == "receiveFile")
                        {
                            messageDic.TryGetValue("url", out remoteFileUrl);
                            client.Send(PackData("roger"));
                            Console.WriteLine("remoteFileUrl: " + remoteFileUrl);
                            Console.WriteLine("to do invoke download.");
                            // 关闭连接
                            client.Shutdown(SocketShutdown.Both);
                            client.Close();
                            clientClose = true;
                        }
                    }
                    else
                    {
                        Console.WriteLine("客户端关闭了连接");
                        client.Shutdown(SocketShutdown.Both);
                        client.Close();
                    }
                }
            }
        }

    }
}

 

标签:websocket,socket,temp,C#,Length,ws,using,byte,string
From: https://www.cnblogs.com/jsper/p/17346323.html

相关文章

  • TCP 三次握手过程中客户端发送的 SYN 报文的基本结构
    **以下是SYN报文示意图,展示了TCP三次握手过程中客户端发送的SYN报文的基本结构**。```SegmentHeader+----------------+------------------------------+|......
  • oracle数据库跟踪
    --数据库跟踪SELECT*FROMv$sqlwherelast_active_time>TO_DATE('2019-7-1813:17:00','yyyy-mm-ddhh24:mi:ss')ORDERBYLAST_ACTIVE_TIMEDESC;selects.sql_text,b.name,b.position,b.dup_position,b.value_string,s.LAST_ACTIVE_TIME,s.SQL_......
  • AQS中的CyclicBarrier源码解析
    packagecn.com.pep;importjava.util.concurrent.BrokenBarrierException;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.TimeoutException;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.ReentrantLock;/**......
  • MFC-CreateProcess创建一个新的进程和它的主线程并执行外部程序
     CONSTTCHARpath[]=_T("C:\\Users\\Administrator\\Desktop\\test.exe");PROCESS_INFORMATIONinfo;STARTUPINFOsi={sizeof(si)};BOOLb=CreateProcess(path,NULL,NULL,NULL,FALSE,NULL,NULL,NULL,&si,&info);/......
  • abp(net core)+easyui+efcore实现仓储管理系统——组织管理升级之下(六十二)
    Abp(netcore)+easyui+efcore实现仓储管理系统目录abp(netcore)+easyui+efcore实现仓储管理系统——ABP总体介绍(一)abp(netcore)+easyui+efcore实现仓储管理系统——解决方案介绍(二)abp(netcore)+easyui+efcore实现仓储管理系统——领域层创建实体(三) abp(netcore)+eas......
  • kubenetes flanel crash
    在工作中,由于不同的需求,需要更换CNI插件; 此场景为,卸载flannel网络,更换为calico网络; 建议:在生产场景中,建议提前规划好k8s的网络,如果中途更换网络插件,产生的影响会比较大; 卸载flannel网络步骤:#第一步,在master节点删除flannelkubectldelete-fhttps://raw.githubusercont......
  • Codeforces Round 866 (Div. 2)(A~C)
    目录A.Yura'sNewName题意思路代码B.JoJo'sIncredibleAdventures题意思路代码C.ConstructiveProblem题意思路代码A.Yura'sNewName题意在字符串\(s\)中添加"_"或者"^",使得\(s\)中的任意字符都必须为"_"或者"^^"的一部分,求最少要添加的字符数量思路当字符串......
  • 十五年程序员从零开始学C#系列之(一)——Literals 字面量
    一、概念在计算机编程中,Literals(字面量)是指在程序中直接表示数据的一种方式。它们是在代码中出现的固定值,与变量不同,它们没有名称或标识符。Literals可以用于各种数据类型,包括整数、浮点数、布尔值、字符和字符串等。下面是一些常见的Literals类型:整数(IntegerLiterals):整数L......
  • html css 怎么导入外部css
    在HTML文件中导入外部CSS样式表,可以使用<link>标签。具体步骤如下:创建一个样式表文件(通常以.css扩展名为后缀),并将所有的CSS样式规则存放在该文件中。在HTML文件中添加一个<link>标签,指定rel="stylesheet"属性和href属性,其中href属性值应该是样式表文件的U......
  • jmeter中的java请求 用httpclient写的http请求 及参数化
    首先,jmeter中的sample的原理: jmeter 中的java 请求,sample 原理,java testjmeter自带的包,把包放在类路径下面,通过反射机制,通过反射机制扫出来。 先导入五个jar包  packagecom.young.testing91;importjava.io.IOException;importorg.apache.http.client.C......