0

I have a C# app that implement a server ,I want this app to be able to get messages from the browser. how can I send tcp messages using javascript that c# can read? (obviously I can change the c# code and the javascript/angular code)

UPDATE - this is the c# code

private static TcpListener server;
    static void Main(string[] args)
    {
        server = new TcpListener(IPAddress.Parse("127.0.0.1"), 476);
        server.Start();
        TcpClient client = server.AcceptTcpClient();
        Console.WriteLine("accept spcket");

        try
        {
            NetworkStream stream = client.GetStream();
            //StreamReader read = new StreamReader(stream);
            //Console.WriteLine(read.Read());

            Byte[] b = new Byte[client.Available];
            stream.Read(b, 0, b.Length);
            string data = Encoding.UTF8.GetString(b);
            Console.WriteLine(data);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

and this is the javascript code

function send() {
let s = new WebSocket("ws://localhost:476");
alert("WebSocket is supported by your Browser!");

s.onopen = function () {
    s.send("my data");
    alert("sent");
}

how do I get the body of the messages in c#?

Yedidya kfir
  • 1,419
  • 3
  • 17
  • 32
  • 2
    I think you want to use WebSockets. If you're on the Microsoft stack look at SignalR. – Crowcoder Apr 29 '18 at 10:54
  • my problem is how to send messages in javascript that c# can read , how to get them in c# is the easy part – Yedidya kfir Apr 29 '18 at 11:02
  • It is entirely a JavaScript problem. Network communication does not care about the programming language of hte parts involved. That is one reason it is so successfull https://stackoverflow.com/questions/12407778/connecting-to-tcp-socket-from-browser-using-javascript – Christopher Apr 29 '18 at 11:07
  • From your description - regular http requests should be enough. – Evk Apr 29 '18 at 11:46

1 Answers1

-1

just create a header and message format like ipv4 and send it to the port.