Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between socket programming and Http programming

People also ask

What is the difference between HTTP and WS?

WebSocket is an event-driven protocol, which means you can actually use it for truly realtime communication. Unlike HTTP, where you have to constantly request updates, with websockets, updates are sent immediately when they are available.

Does HTTP use socket?

Http is a protocol built on top of sockets. When you use Http, you're using a higher level of abstraction on top of sockets. You're still using sockets.

Is socket faster than HTTP?

Websocket is said to be faster than http because it provides full duplex communication. So, both client and server can communicate at the same time.

Is network programming and socket programming same?

Yeah, it's true that network programming requires networking technology while on the other hand socket programming is a subset of network programming. Most current network programming is done either using sockets directly, or using various other layers on top of sockets.


HTTP is an application protocol. It basically means that HTTP itself can't be used to transport information to/from a remote end point. Instead it relies on an underlying protocol which in HTTP's case is TCP.

enter image description here

You can read more about OSI layers if you are interested.

Sockets on the other hand are an API that most operating systems provide to be able to talk with the network. The socket API supports different protocols from the transport layer and down.

That means that if you would like to use TCP you use sockets. But you can also use sockets to communicate using HTTP, but then you have to decode/encode messages according to the HTTP specification (RFC2616). Since that can be a huge task for most developers we also got ready clients in our developer frameworks (like .NET), for instance the WebClient or the HttpWebRequest classes.


With HTTP you use high-level HTTP protocol(that works on top of a socket). It's session-less which means you send text request like GET google.com and receive text or binary data in return, after that connection is closed(in HTTP 1.1 persistent connections are available)

MSDN example:

public static void Main (string[] args)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

    Console.WriteLine ("Content length is {0}", response.ContentLength);
    Console.WriteLine ("Content type is {0}", response.ContentType);

    // Get the stream associated with the response.
    Stream receiveStream = response.GetResponseStream ();

    // Pipes the stream to a higher level stream reader with the required encoding format. 
    StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

    Console.WriteLine ("Response stream received.");
    Console.WriteLine (readStream.ReadToEnd ());
    response.Close ();
    readStream.Close ();
} 

With sockets you go on the level lower and actually control the connection and send/receive raw bytes.

Example:

var remoteEndpoint=new IPEndPoint(IPAddress.Loopback, 2345);
var socket = new Socket(remoteEndpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteEndpoint);
socket.Send(new byte[] {1, 2, 3, 4});

HTTP Connection

  • HTTP connection is a protocol that runs on a socket.
  • HTTP connection is a higher-level abstraction of a network connection.
  • With HTTP connection the implementation takes care of all these higher-level details and simply send HTTP request (some header information) and receive HTTP response from the server.

Socket Connection

  • Socket is used to transport data between systems. It simply connects two systems together, an IP address is the address of the machine over an IP based network.
  • With socket connection you can design your own protocol for network connection between two systems.
  • With Socket connection you need to take care of all the lower-level details of a TCP/IP connection.

for two endpoints to be able to talk to each other they should both follow a set of rules. in computer these set of rules is called protocol.

for example for an endpoint like browser and for another like a web server they should both follow a set of rules or protocol called http to be able to communicate and trade information . so in the world wide web and this kind of communications only those who talk based on this http protocol could successfully talk to each other.

socket is just an endpoint. it could follow http protocol to come in a communication in www as a client requesting a page or it could act as a server listening to connections. or maybe it could follow another set of rules or protocols like ssh, ftp and communicate in other ways.

now in socket programming you could make a socket , bind it to an ip address and a port number to act as a port number and tell it to follow http , ssh ,ftp or whatever you want based on the communications that you want to use your socket for.


HTTP programming or HTTP request is used for loosely coupling and platform-neutral language technology communication where as socket programming is used where system has language specification protocol