1

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

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40

3 Answers3

3

You can do this in the following way.

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!");
 }

}
}

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

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.

Kanishka Dilshan
  • 724
  • 2
  • 10
  • 19
1

You should use the HttpListener class, or create a self-hosted ASP.Net Web API project.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

I think you need a something like Comet, check this example using Comet

r4nd0m
  • 36
  • 3