What is the difference between socket programming and Http programming? can anyone help please?
6 Answers
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.
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.

- 305,947
- 44
- 307
- 483

- 99,844
- 45
- 235
- 372
-
3So both use TCP, it's just that HTTP responds in a predefined format and socket gives data as returned from another end of a socket, right? – आनंद Feb 18 '18 at 10:23
-
4yep. TCP makes sure that everything arrives (transport layer) and HTTP tells what the web applications should do. – jgauffin Feb 19 '18 at 06:18
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});

- 6,175
- 3
- 37
- 61
-
1
-
-
-
True, but that's the details OP will discover himself along with other 95% information on this topic. – Anri Feb 27 '13 at 09:59
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.

- 32,664
- 6
- 42
- 57
-
With HTTP connection the implementation takes care of all these higher-level details ?? I think you mean lower-level details. – joedotnot Sep 13 '21 at 16:43
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.

- 81
- 4
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

- 13,446
- 4
- 42
- 72

- 35
- 2
-
1What do you mean by language? A java application can communicate with a Python application via sockets for example – Adam Hughes Sep 07 '17 at 14:34
-
I think what he means is that, usually, if you want to connect loosely coupled (or totally different) systems, HTTP is useful: a frontend built with some technology with a backed build with another technology, accessing resources on the web through URLs. Whereas usually if you are using lower level connection protocols like TCP/UDP you are probably orchestrating a communication with systems with similar technologies (e.g. different Java applications). Is this right? I am trying to understand it myself. – Nicola Amadio Aug 03 '20 at 12:24
Socket programming is a kind of middleware, residing between the application layer and the TCP layer. It's able to carry anything present in the application layer; even HTTP data.

- 340
- 1
- 12