Sunday, March 11, 2012

Sockets overview

Sockets:Use it or Not 
Many of us have listened about socket  in our programming language but we may not have given enough attention to it.
Some basic questions comes in mind when we listen about socket :-
What is socket? Why should I use socket ? What are  the benefits of using socket ? Should I use socket or not in my coding ? Is there other alternatives to socket programming ? What are the other ways ,using which I can do coding  without need of using sockets?  And One important question, I haven't used sockets till yet in my programming career,so why should i bother of using it or pay attention to understand the process of socket communication?


These are some of questions which came to my mind also, when i first started doing some programming using sockets.So,I started doing research about this topic and found it very interesting . It is basically the base of  network communication in our programming world.

The socket concept is not specific to any programming language, it is basically same concept in c#,java,c++ and other known programming languages for network communication.

Here in this post ,I will talk about sockets in c#.
So ,
Q -What is socket?
Ans-Sockets is a method for communication between a client program and a server program in a network. A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API). The most common sockets API is the Berkeley  interface for sockets. Sockets can also be used for communication between processes within the same computer.


That's the answer I found from a site which I think will give you a good definition to understand sockets and its use. Also ,refer to link for sockets in .net for further understanding of socket APIs in .net.

For other above questions answers, I would say we have WCF  in .net for network communication.Internally WCF uses sockets for network communication. So, we can say WCF is just a wrapper on socket programming  which gives us many more  facilities than network communication e.g security , reliability and other options when doing communication between services and clients.

Sending  and Receiving data on network (Communication)
Before sending data using sockets on network we need to convert data into streams.Here comes the other concepts serialization and deserialization in our programming language to convert data into stream and converted stream back into data objects. FYI, In WCF we have DataContractSerialization and in web services we use normally XmlSerialization. I will not go further into serialization and deserialization. You can easily found topics on serialization and deserialization by searching on Google but I am sharing msdn link on serialization and deserialization in .net.

So,our purpose to know what is socket and why should we use it.. is almost complete.
For more information on sockets, how they communicate and what type of data can be passed from one socket to another ,please refer the link.

Next.. in this socket series , I will post  a simple example of web server using socket... (And also a simple comparison  of our web server code with IIS.. web server.)


Please share your views on this topic and let me know .. what more you think .. i should add in this socket series.