Jayesh
Jayesh

Reputation: 3951

Socket Programming in ASP.NET?

Can we do Socket Programming in ASP.NET/WCF? Like the service listens on a port for incoming requests. All the clients from outside the network also publish/listen on that ip:port

Whenever the service writes anything on the port, all the clients get that thing without polling.

Is something like this possible with ASP.NET/WCF?

Thanks

Upvotes: 6

Views: 17338

Answers (4)

MrBoJangles
MrBoJangles

Reputation: 12237

As alexanderb linked, there is indeed .NET socket support in the System.Net.Sockets namespace. As I just completed, with a colleague, a WCF web service that communicates with a socket-based service in Korea. We're simply sending some info and getting some info back, quick, tidy. Here's an example of some sockety code:

const string ipAddressString = "xxx.xxx.xxx.xxx";// replace with correct IP address
IPAddress ipAddress = IPAddress.Parse(ipAddressString);
const int portNum = 1234;// replace with correct port
IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, portNum);

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(remoteEndPoint);
string sendString = "some stuff you want to send";
byte[] bytes = Encoding.UTF8.GetBytes(sendString.ToString());
client.Send(bytes);
byte[] receiveBuffer = new byte[128];
client.Receive(receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None);
string bufferString = Encoding.GetEncoding(949).GetString(receiveBufferSize);
client.Shutdown(SocketShutdown.Both);
client.Close();

try-catches and such have been omitted to keep this as "simple" and socket-focused as possible. The key takeaways here are the Socket construction, Connect(), Send(), and Receive(). Also, Shutdown() and Close(). Note that before about three days ago, I thought a socket was that thing on the wall you plug stuff into, so this should be fairly rudimentary. But hey, it works!

Upvotes: 2

Charles Boyung
Charles Boyung

Reputation: 2481

What you are trying to do would not work even if you really could do socket programming in Asp.Net. Asp.Net is not continuously "running" like a service is. It shuts down after a period of inactivity (no web requests made) and starts up again with new web requests. Your socket code would only run from when a web request made until that inactivity timeout occurs.

Upvotes: 1

Alexander Beletsky
Alexander Beletsky

Reputation: 19821

If you are talking about WCF/ASP.NET, those two are much "higher" above the socket level. Answering you question - yes, you can do socket programming with .NET framework.

http://msdn.microsoft.com/en-us/library/system.net.sockets.aspx

EDIT

BTW, I smell something wrong then hear "sockets.. cloud", you are probably missing something. Taking into account avaliable techlologices for distributed/networking programming doing socket programming nowadays seems illogical.

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190907

You don't need WCF or ASP.NET for that. You might look up UDP socket programming with .NET.

Upvotes: 0

Related Questions