Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7571

Problem with implementing a socket listener in asp.net web-app

I'm trying to bind the listening socket to a port and to accomplish that task i'm using the following lines of code:

    Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
    IPEndPoint ep = new IPEndPoint("localhost", 8372);
    listenSocket.Bind(ep);  
    listenSocket.Listen(backlog);

I found this code from this msdn link. I'm just copying and pasting this code and using it in a class method as shown here but it seems like i'm not able to do it !! Do i have to implement some interface or do something else ??

Please help me

Thanks in anticipation

Upvotes: 0

Views: 438

Answers (1)

Daniel Renshaw
Daniel Renshaw

Reputation: 34187

The code in the image you linked to has an error: portSocketListener is coded as if it's a method but it's missing the () parentheses that turn it into a method.

Try replacing the entire thing with:

public void portSocketListener() // <-- Here's the error
{
    Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    IPAddress hostIP = (Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];
    IPEndPoint ep = new IPEndPoint(hostIP, 8372);
    listenSocket.Bind(ep);
    listenSocket.Listen(1);
}

Upvotes: 3

Related Questions