简体   繁体   English

通过套接字在C#和Js之间发送二进制数据

[英]Sending binary data by socket between C# and Js

I'm trying to send binary data between server ( C# ) application and client (js-application -- made by WebSocket ). 我正在尝试在服务器( C# )应用程序和客户端(js应用程序-由WebSocket制造)之间发送二进制数据。 Connection between server and client is established and handshake is OK. 服务器与客户端之间的连接已建立,握手可以。 Messages from client are receiving by server, but when I'm trying to send binary data to client, the event "onmessage" doesn't work. 来自客户端的消息正在由服务器接收,但是当我尝试向客户端发送二进制数据时,事件"onmessage"不起作用。 This is fragments of my C# code. 这是我的C#代码的片段。 Sending binary data in "sendFile" function. "sendFile"函数中发送二进制数据。

class Listener
{
    private IPAddress ip;
    private int port;
    private TcpListener server;
    private TcpClient client;
    private NetworkStream stream;
    private bool isSuccHandshaked;

    public Listener()
    {
        ip = IPAddress.Loopback;
        port = 8080;
        server = new TcpListener(ip, port);
        isSuccHandshaked = false;
    }

    private void makeHandshake()
    {
        //...   
    }

    private String decodeMessage(Byte[] bytes)
    {
       //...
    }

    private void sendFile()
    {
        Byte[] dataToSend = File.ReadAllBytes("test.txt");
        stream.Write(datatosend, 0, datatosend.Length);
        stream.Flush();
    }

    public void startListen()
    {
        server.Start();
        Console.WriteLine("Server has started on {0}. Port: {1}. {2}Waiting for a connection...", ip, port, Environment.NewLine);

        client = server.AcceptTcpClient();
        Console.WriteLine("A client connected.");

        stream = client.GetStream();
        while (!isSuccHandshaked)
        {
            makeHandshake();
        }

        while (true)
        {
            if (client.Available > 0)
            {
                Byte[] bytes = new Byte[client.Available];
                stream.Read(bytes, 0, bytes.Length);
                String message = decodeMessage(bytes);
                sendFile();
            }
         }
    }
}

} }

and js-code: 和js代码:

 var address = 'ws://localhost:8080'; var socket = new WebSocket( address ); socket.binaryType = "arraybuffer"; socket.onopen = function () { alert( 'handshake successfully established. May send data now...' ); socket.send( "Aff" ); }; socket.onclose = function () { alert( 'connection closed' ); }; socket.onmessage = function ( evt ) { console.log( "Receive message!" ); console.log( "Got ws message: " + evt.data ); } 

Maybe there is some peculiar properties in receiving data with WebSocket protocol? 使用WebSocket协议接收数据时可能有一些特殊的属性吗? Wich useful approaches to send binary data from C# code to js you can recommend? 您会推荐哪种有用的方法将binary数据从C#代码发送到js

A websocket is not a raw TCP socket. Websocket不是原始的TCP套接字。 It uses HTTP negotiation and its own framing protocol you have to comply with. 它使用HTTP协商和您必须遵守的自己的成帧协议。 If you are interested in writing your own server in C# take a look at this . 如果您有兴趣使用C#编写自己的服务器,请查看this

However if you only want to use them, you can either use the default Microsoft implementation with IIS, or you can use one of the many standalone websockets components. 但是,如果只想使用它们,则可以将默认的Microsoft实现与IIS一起使用,也可以使用许多独立的websocket组件之一。 I maintain one named WebSocketListener . 我维护一个名为WebSocketListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM