websocket与C# socket相互通信
<!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>
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与C# socket相互通信的更多相关文章
- webSocket协议与Socket的区别
WebSocket介绍与原理WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex).一开始的握手需要借助HTTP请求完成. ——百度 ...
- Demo源码放送:打通B/S与C/S !让HTML5 WebSocket与.NET Socket公用同一个服务端!
随着HTML5 WebSocket技术的日益成熟与普及,我们可以借助WebSocket来更加方便地打通BS与CS -- 因为B/S中的WebSocket可以直接连接到C/S的服务端,并进行双向通信.如 ...
- Socket进程通信机制及应用
Socket通常称为“套接字”,用于描述IP地址和端口,是一个通信链的句柄.应用程序通过套接字向网络发出请求或者应答网络请求.Socket即不是一个程序,也不是一个协议,其只是操作系统提供的通信层的一 ...
- Socket进程通信机制
1.Socket通常称为“套接字”,用于描述IP地址和端口,是一个通信链的句柄. 2.应用程序通过套接字向网络发出请求或者应答网络请求. 3.Socket既不是一个程序,也不是一种协议,其只是操作系统 ...
- VC SOCKET 压缩通信学习
Server................// Server.cpp : Defines the entry point for the console application. // #inclu ...
- 【Android实战】Socket消息通信
这篇博客的内容是基于http://my.oschina.net/fengcunhan/blog/178155进行改造的.所以须要先看完这篇博客,然后再来看以下的内容. 1.须要完毕的功能是直播间的so ...
- 继承c3,网络编程,相互通信
继承: 面向对象多继承,a先找左,再找右 py2中有经典类,新式类,py3中只有新式类 py2中用ascii 编译 py3中用unicode 编译 py2: 经典类 新式类:如果自己或自己的前辈继承o ...
- wmware 怎么 跟主机相互通信
VMnet1和VMware8其实就是软件模拟出来的两块网卡提供DHCP服务,两块网卡对应VMware的两种不同的模式VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转 ...
- 打通B/S与C/S !让HTML5 WebSocket与.NET Socket公用同一个服务端!
随着HTML5 WebSocket技术的日益成熟与普及,我们可以借助WebSocket来更加方便地打通BS与CS -- 因为B/S中的WebSocket可以直接连接到C/S的服务端,并进行双向通信.如 ...
- 使用Socket进行通信
客户端通常可使用Socket的构造器来连接到指定服务器,Socket通常可使用如下两个构造器. Socket(lnetAddress/String remoteAddress , int port ...
随机推荐
- UMA插件专题(一)——基础网格创建
什么是UMA? UMA,Unity Multipurpose Avatar,是一个开源Avatar创建框架,通过它可以实现自定义人物(或任何生物)的模型,包括换装,改变身材样貌等功能. 现在先从UMA ...
- 全新TI AM62xx系列核心板上市,小小身板蕴藏巨大势能!
2011年TI推出AM335x,成为了此后市场上最受欢迎的通用工业级ARM处理器,并广泛应用于工业HMI, 医疗电子,机器人,能源,汽车等领域.随着工业4.0的发展,HMI人机交互.工业工控.医疗等领 ...
- ls和mkdir
ls 查看当前目录的内容 1) ls -l 显示详细列表 2) Ls -lh 吧文件大小以人性化的方式展开 3) ls -a 显示所有的文件,包括隐藏文件,隐藏文件是已.开头的文件 4) ll 等于 ...
- Shiro权限管理框架-@RequiresPermissions 注解 使用问题记录
背景: 需要在springboot项目里面用到shiro的权限管理,Shiro访问控制流程:先shiro认证(登录时调用) 然后 shiro授权,但是项目里面登录的功能用的公司统一的系统,所以需要&q ...
- Android笔记-跳转到相册选择图片
跳转到相册选择图片 即设置一个点击事件,点击之后即可跳转到相册进行图片的选择 具体的实现步骤: 界面很简单的啦,这里就直接将源代码放出来啦: <?xml version="1.0&qu ...
- Java数据脱敏(手机号|邮箱号|身份证号|银行卡号)
参考博客:https://blog.csdn.net/ywb201314/article/details/107762279
- DSC:数仓SQL脚本迁移的神奇工具
摘要:本文介绍的DSC工具是针对数据库切换时面临的迁移任务而开发的免安装命令行工具.目的是提供简单.快速.可靠的SQL脚本迁移服务. 本文分享自华为云社区<GaussDB(DWS)DSC工具系列 ...
- 分库分表ShardingJDBC最佳实践
1 添加依赖 <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId> ...
- 【单元测试】Junit 4(八)--junit4 内置Rule
1.0 Rules Rules允许非常灵活地添加或重新定义一个测试类中每个测试方法的行为.测试人员可以重复使用或扩展下面提供的Rules之一,或编写自己的Rules. 1.1 TestName ...
- Promise合集
Promise.all Promise.all 可以将多个 Promise 实例包装成一个新的 Promise 实例.所有的 Promise 对象都成功时返回的是一个结果数组,一旦有任何一个 Prom ...