简体   繁体   English

JavaScript和C#之间的TCP通信

[英]tcp communication between javascript and c#

I have a C# app that implement a server ,I want this app to be able to get messages from the browser. 我有一个实现服务器的C#应用​​,我希望该应用能够从浏览器获取消息。 how can I send tcp messages using javascript that c# can read? 如何使用C#可以读取的JavaScript发送TCP消息? (obviously I can change the c# code and the javascript/angular code) (显然,我可以更改c#代码和javascript /角度代码)

UPDATE - this is the c# code 更新-这是C#代码

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 这是javascript代码

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#? 如何在C#中获取消息正文?

只需创建标头和消息格式(如ipv4)并将其发送到端口即可。

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

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