• 大小: 28KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: C#
  • 标签: C#  WebSocket  

资源简介

C# WebSocket 服务端和网页端示例,初学者必备!

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text.Regularexpressions;
using System.Security.Cryptography;

namespace WebSocketServer
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 1818;
            byte[] buffer = new byte[1024];

            IPEndPoint localEP = new IPEndPoint(IPAddress.Any port);
            Socket listener = new Socket(localEP.Address.AddressFamily SocketType.Stream ProtocolType.Tcp);

            try
            {
                listener.Bind(localEP);
                listener.Listen(10);

                Console.WriteLine(“等待客户端连接....“);
                Socket sc = listener.Accept();//接受一个连接
                Console.WriteLine(“接受到了客户端:“ + sc.RemoteEndPoint.ToString() + “连接....“);

                //握手
                int length = sc.Receive(buffer);//接受客户端握手信息
                sc.Send(PackHandShakeData(GetSecKeyAccetp(buffer length)));
                Console.WriteLine(“已经发送握手协议了....“);

                //接受客户端数据
                Console.WriteLine(“等待客户端数据....“);
                length = sc.Receive(buffer);//接受客户端信息
                string clientMsg = AnalyticData(buffer length);
                Console.WriteLine(“接受到客户端数据:“ + clientMsg);

                //发送数据
                string sendMsg = “您好,“ + clientMsg;
                Console.WriteLine(“发送数据:““ + sendMsg + “” 至客户端....“);
                sc.Send(PackData(sendMsg));

                Console.WriteLine(“演示Over!“);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        /// 
        /// 打包握手信息
        /// 

        /// Sec-WebSocket-Accept
        /// 数据包
        private static byte[] PackHandShakeData(string secKeyAccept)
        {
            var responseBuilder = new StringBuilder();
            responseBuilder.Append(“HTTP/1.1 101 Switching Protocols“ + Environment.NewLine);
            responseBuilder.Append(“Upgrade: websocket“ + Environment.NewLine);
            responseBuilder.Append(“Connection: Upgrade“ + Environment.NewLine);
            responseBuilder.Append(“Sec-WebSocket-Accept: “ + secKeyAccept + Environment.NewLine + Environment.NewLine);
            //如果把上一行换成下面两行,才是thewebsocketprotocol-17协议,但居然握手不成功,目前仍没弄明白!
            //responseBuilder.Append(“Sec-WebSocket-Accept: “ + secKeyAccept + Environment.NewLine);
            //responseBuilder.Append(“Sec-WebSocket-Protocol: chat“ + Environment.NewLine);

            return Encoding.UTF8.GetBytes(responseBuilder.ToString());
        }

        /// 
        /// 生成Sec-WebSocket-Accept
        /// 

        /// 客户端握手信息
        /// Sec-WebSock

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        1593  2016-03-02 15:35  websocketclient.html
     目录           0  2016-03-02 15:39  WebSocketServer\
     目录           0  2016-03-02 15:39  WebSocketServer\bin\
     目录           0  2016-03-02 15:39  WebSocketServer\bin\Debug\
     文件        7680  2016-03-02 15:32  WebSocketServer\bin\Debug\WebSocketServer.exe
     文件       17920  2016-03-02 15:32  WebSocketServer\bin\Debug\WebSocketServer.pdb
     文件       11600  2016-03-02 15:33  WebSocketServer\bin\Debug\WebSocketServer.vshost.exe
     文件         490  2010-03-17 22:39  WebSocketServer\bin\Debug\WebSocketServer.vshost.exe.manifest
     目录           0  2016-03-02 15:39  WebSocketServer\obj\
     目录           0  2016-03-02 15:39  WebSocketServer\obj\x86\
     目录           0  2016-03-02 15:39  WebSocketServer\obj\x86\Debug\
     文件        5869  2016-03-02 15:32  WebSocketServer\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2016-03-02 09:50  WebSocketServer\obj\x86\Debug\TempPE\
     文件         312  2016-03-02 15:33  WebSocketServer\obj\x86\Debug\WebSocketServer.csproj.FileListAbsolute.txt
     文件        7680  2016-03-02 15:32  WebSocketServer\obj\x86\Debug\WebSocketServer.exe
     文件       17920  2016-03-02 15:32  WebSocketServer\obj\x86\Debug\WebSocketServer.pdb
     文件        7584  2016-03-02 15:32  WebSocketServer\Program.cs
     目录           0  2016-03-02 15:39  WebSocketServer\Properties\
     文件        1362  2016-03-02 09:50  WebSocketServer\Properties\AssemblyInfo.cs
     文件        2451  2016-03-02 09:52  WebSocketServer\WebSocketServer.csproj

评论

共有 条评论