0

I'm trying to send binary data between server (C#) application and client (js-application -- made by 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. This is fragments of my C# code. Sending binary data in "sendFile" function.

    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:

    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 are some peculiar properties in receiving data with WebSocket protocol? Which useful approaches to send binary data from C# code to js you can recommend?

1 Answer 1

1

A websocket is not a raw TCP socket. It uses HTTP negotiation and its own framing protocol you have to comply with. If you are interested in writing your own server in C# take a look at 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. I maintain one named WebSocketListener.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, @vtortola, I make a correct frame, while sending and it starts work well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.