• 基于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));

    第五步:释放资源

    1  //注意,使用完,记得释放,不然会有内存泄漏
    2 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);

    第三步:发送数据

    1 //发送数据
    2  
    3 await webSocket.SendAsync(new ArraySegment<byte>(bsend), WebSocketMessageType.Binary, true, cancellation);
    4  
    5  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.


  • 相关阅读:
    逻辑地址、线性地址、物理地址
    查找已知字符串子串
    替换字符串中的空格为%20
    资本的奥秘
    net::ERR_CONNECTION_RESET的处理方法
    SQL Server数据库从低版本向高版本复制数据库
    中式思维的五大逻辑缺陷(转)
    1年PK12年,中国式教育完败(转载)
    有关衣服的想法
    jquery邮箱自动补全
  • 原文地址:https://www.cnblogs.com/yanglang/p/9695883.html
Copyright © 2020-2023  润新知