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?