与使用JavaScript的浏览器C#通信 [英] C# communication with browser using JavaScript

查看:181
本文介绍了与使用JavaScript的浏览器C#通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从JavaScript传递消息到C#应用程序,我可以使用PHP和tcpListner在C#中(但使用PHP需要服务器来托管)要做到这一点,我需要localhost通过浏览器(使用JavaScript或任何其他沟通C#应用程序可能的方式),浏览器需要将消息传递给应用程序在同一matchine运行

How to pass messages to C# application from JavaScript, I'm able to do this with PHP and tcpListner in c#(but using PHP need server to host), i need localhost communicate c# application with browser(using javaScript or any other possible way), browser need to pass messages to application running on same matchine

您可以建议这个适当的方法与样本

can you suggest appropriate method for this with sample

推荐答案

您可以通过以下方式做到这一点。

You can do this in the following way.

第1步:你有创建一个监听器。 的TcpListener 类或的HttpListener可用于开发听者在.NET中。这个代码展示了如何实现一个TCP监听器

step 1 : you have to create a Listener. TcpListener class or HttpListener in .net can be used to develop the listener. This code shows how to implement a TCP listener.

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

//Author : Kanishka 
namespace ServerSocketApp
{
class Server
{
 private TcpListener tcpListn = null;
 private Thread listenThread = null;
 private bool isServerListening = false;

 public Server()
 {
  tcpListn = new TcpListener(IPAddress.Any,8090);
  listenThread = new Thread(new ThreadStart(listeningToclients));
  this.isServerListening = true;
  listenThread.Start();
 }

 //listener
 private void listeningToclients()
 {
  tcpListn.Start();
  Console.WriteLine("Server started!");
  Console.WriteLine("Waiting for clients...");
  while (this.isServerListening)
  {
   TcpClient tcpClient = tcpListn.AcceptTcpClient();
   Thread clientThread = new Thread(new ParameterizedThreadStart(handleClient));
   clientThread.Start(tcpClient);
  }

 }

 //client handler
 private void handleClient(object clientObj)
 {
  TcpClient client = (TcpClient)clientObj;
  Console.WriteLine("Client connected!");

  NetworkStream stream = client.GetStream();
  ASCIIEncoding asciiEnco = new ASCIIEncoding();

  //read data from client
  byte[] byteBuffIn = new byte[client.ReceiveBufferSize];
  int length = stream.Read(byteBuffIn, 0, client.ReceiveBufferSize);
  StringBuilder clientMessage = new StringBuilder("");
  clientMessage.Append(asciiEnco.GetString(byteBuffIn));

  //write data to client
  //byte[] byteBuffOut = asciiEnco.GetBytes("Hello client! \n"+"You said : " + clientMessage.ToString() +"\n Your ID  : " + new Random().Next());
  //stream.Write(byteBuffOut, 0, byteBuffOut.Length);
  //writing data to the client is not required in this case

  stream.Flush();
  stream.Close();
  client.Close(); //close the client
 }

 public void stopServer()
 {
  this.isServerListening = false;
  Console.WriteLine("Server stoped!");
 }

}
}



第2步:您可以将参数传递给创建的服务器作为一个GET请求。您可以使用JavaScript或HTML表单来传递参数。 。像jQuery和Dojo的JavaScript库将使它更容易使Ajax请求

Step 2 : you can pass parameters to the created server as a GET request. You can use either JavaScript or HTML Forms to pass parameters. JavaScript libraries like jQuery and Dojo will make it easier to make ajax requests.

http://localhost:8090?id=1133

您必须修改上面的代码检索参数发送一个GET请求。
我推荐使用的HttpListener 而不是的TcpListener

You have to modify the above code to retrieve the parameters send as a GET request. I recommend to use HttpListener instead of TcpListener

一旦你的听力部分剩余完成部分是刚刚处理来自请求所检索的参数

Once you have done with the listening part rest part is just processing the retrieved parameters from the request.

这篇关于与使用JavaScript的浏览器C#通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆