Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi experts,
i am creating a web application where i need to listen to port, if data comes write it to label.
below is my code but it's not working at all. i have written code for console application that is working fine, and the same i have to do with web application. below is my code

public partial class _Default : System.Web.UI.Page
    static int SERV_TCP_PORT = 8080;
    static string SERV_HOST_ADDR = "";
    static IPAddress ipAd = IPAddress.Parse(SERV_HOST_ADDR);

    protected void Page_Load(object sender, EventArgs e)
        Thread t = new Thread(new ThreadStart(ListenData));

    public void ListenData()
        TcpListener myList = new TcpListener(ipAd, SERV_TCP_PORT);
        /* Start Listeneting at the specified port */
        Console.WriteLine("The server is running at port ..." + SERV_TCP_PORT);
        Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
        Console.WriteLine("Waiting for a connection.....");
        Socket socket = myList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + socket.RemoteEndPoint);
        byte[] bytes = new byte[7];
        int a = socket.Receive(bytes);
        Label1.Text = Encoding.ASCII.GetString(bytes).ToString();
        //  Label1.Text = "xx";

please guide me... ASAP

What I have tried:

i have tried the above written code but it is not working, throw exception:
<pre>Only one usage of each socket address (protocol/network address/port) is normally permitted
F-ES Sitecore 10-Jul-17 8:26am    
This code isn't going to work anyway, is a request\response technology, in order to have a response you can write to (eg update labels) you need a request from a webbrowser, whereas your code is responding to data coming in on a socket.

You'll need to implement some kind of polling mechanism where the client has js that repeatedly asks the server code if its label needs to be updated, or maybe look at something like Signalr. As it stands, this solution does not fit with architecture.
CodeWraith 10-Jul-17 8:57am    
True, but there may be an alternative. It may work to store the listener in a cache where it does not get destroyed when the page is unloaded. Now it can keep listening between page requests and can be retrieved any time the page is requested.

Now we only need a good place for the callback method that receives the incoming data and stores it in a way that allows to retrieve it whenever it is needed. And last we also need a scrap of code that reliably cleans up the listener when it's not needed anymore.

It may not be intended to be used this way, but I think I could pull it off without any sinister unsafe hacks.
F-ES Sitecore 10-Jul-17 9:02am    
My response kinda assumed you'd done all that. IIS can still stop your app at any time it wants though, you can't force your app to always be running.
CodeWraith 10-Jul-17 9:07am    
I'm not the OP. :-)

Windows also could shut down the console application any time it wants to, just for updates. Actually, it's a very good argument against Windows when you can't afford some service to be down.
F-ES Sitecore 10-Jul-17 10:33am    
I know you're not the OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900