Create a Socket server in C # and connect with Javascript

0

It turns out that I need to make a server to communicate clients with each other but in C #.

This is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;             
using System.Net.Sockets;

namespace socket
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint direccion = new IPEndPoint(IPAddress.Any, 1234);

            byte[] payload;
            try
            {
                payload = new byte[255];
                socket.Bind(direccion); 
                socket.Listen(1); 
                Console.WriteLine("Escuchando...");
                Socket listen = socket.Accept();
                int a = listen.Receive(payload, 0, payload.Length, 0);
                Array.Resize(ref payload, a);
                Console.WriteLine("Cliente dice: " + Encoding.Default.GetString(payload)); 
                Console.WriteLine("Conectado con exito");
                socket.Close();

            }
            catch (Exception error)
            {
                Console.WriteLine("Error: {0}", error.ToString());
            }
            Console.ReadLine();
        }

    }
}

Ok, up there I think everything is fine, because to connect to the server I use this Javascript code

        var Server;
        $(document).ready(function() {

            Server = new FancyWebSocket('ws://192.168.1.10:1234');

            Server.bind('open', function() {
                console.log("Cliente conectado");
            });

            Server.bind('close', function( data ) {
                console.log("Cliente desconectado");
            });

            Server.bind('message', function( payload ) {
                console.log(payload);
            });
            Server.connect();

            // ACA ES DONDE ME DA EL ERROR
            Server.send( 'message', 'Hola' );
        });

I'm using jQuery and a library called FancyWebSocket.js

Ok when I execute the JS code to connect to the socket, this response gives me on the server

And this gives me the client

The server seems to work perfectly, but now, how to send messages to the server and send them to the rest of the connected clients?

    
asked by Anthony Medina 03.12.2017 в 17:43
source

1 answer

0

Your code:

  • Opens a server socket and puts it to listen. The program is blocked until a connection arrives.

  • When the connection arrives, take the first thing that arrives (the HTTP headers) and print the message.

  • Exits the program, closing both sockets and stopping listening ...

  • By the time the JS sends the message, there is no longer anyone on the server listening.

    If you want to keep this model (single-thread, a single connection), once the connection is established, it enters a loop until the condition to exit the program is met (for example, that the WebSocket is closed).

    Additionally, in the server you are using sockets while in the client you are using websockets. Although related, they are not the same.

        
    answered by 03.12.2017 в 18:01