• 基于C#net4.5websocket客户端与服务端


    只支持win8以上系统以及windows server2012以上系统

    最近在研究视频传输给浏览器,然后使用H5标签解码。视频流采用websocket传输。所以研究了一下C#的websocket。

    首先使用HttpListener进行侦听,HttpListener监听需要启动管理员权限才能运行,或者注册该端口,注册如下:

    已管理员身份运行cmd.exe 输入下面两个命令
    netsh http delete urlacl url=http://127.0.0.1:8080/

    netsh http add urlacl url=http://127.0.0.1:8080/  user=dell

    *******************websocket服务端****************************************

    第一步:创建HttpListener类,并启动监听:

    var listener = new HttpListener();
    listener.Prefixes.Add("http://10.10.13.140:8080/");
    listener.Start();
    第二步:等待连接

    var context = listener.GetContext();
    第三步:接收websocket

    var wsContext = await context.AcceptWebSocketAsync(null);
    var ws = wsContext.WebSocket;
    Console.WriteLine("WebSocket connect");
    第四步:开始异步接收数据

    //接收数据
    var wsdata = await ws.ReceiveAsync(abuf, cancel);
    Console.WriteLine(wsdata.Count);
    byte[] bRec = new byte[wsdata.Count];
    Array.Copy(buf, bRec, wsdata.Count);
    Console.WriteLine(Encoding.Default.GetString(bRec));
    第五步:释放资源

    //注意,使用完,记得释放,不然会有内存泄漏
    ws.Dispose();
    *******************websocket客户端****************************************

    这里使用ClientWebSocket类进行

    第一步:创建ClientWebSocket

    ClientWebSocket webSocket = new ClientWebSocket();
    第二步:建立websocket连接

    await webSocket.ConnectAsync(new Uri("ws://10.10.13.140:8080/"), cancellation);
    Console.WriteLine(111);
    第三步:发送数据

    //发送数据
    await webSocket.SendAsync(new ArraySegment<byte>(bsend), WebSocketMessageType.Binary, true, cancellation);

    await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "1", cancellation);
    第四步:释放资源
    //释放资源
    webSocket.Dispose();

    为了方便大家学习,整理了一下服务端和客户端的代码,采用C# net4.5 vs2017开发环境

    连接如下:

    点击打开链接

    *************--------------备注---------------******************************************

    发现win7下无法运行,参考

    https://msdn.microsoft.com/en-us/library/system.net.websockets.clientwebsocket(v=vs.110).aspx

    关键部分如下:

    Some of the classes and class elements in the System.Net.WebSockets namespace are supported on Windows 7, Windows Vista SP2, and Windows Server 2008. However, the only public implementations of client and server WebSockets are supported on Windows 8 and Windows Server 2012. The class elements in the System.Net.WebSockets namespace that are supported on Windows 7, Windows Vista SP2, and Windows Server 2008 are abstract class elements. This allows an application developer to inherit and extend these abstract class classes and class elements with an actual implementation of client WebSockets.

    ---------------------


    原文:https://blog.csdn.net/g0415shenw/article/details/80365554

  • 相关阅读:
    MSSQL Rebuild(重建)索引
    网络爬虫原理
    Twitter Storm:单机环境的安装与配置
    1079 回家
    Win2003 Server磁盘配额揭密之补遗篇
    Win2003 Server磁盘配额揭密之启用篇
    编译mapnik(win7 环境下vs2008编译mapnik 0.7.1 成功)
    Writing a Windows Shell Extension(marco cantu的博客)
    Android学习之一:Cygwin简介
    Linux 进程间通信(一)
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/9872483.html
Copyright © 2020-2023  润新知