What's the difference of HTTP Programming and Socket Programming? [duplicate] - sockets

What is the difference between socket programming and Http programming? can anyone help please?

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.

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

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.

Related

Difference between Websocket and Socket and XMPP?

Help me out to understand the difference between Socket, Websockets, and XMPP protocols.
"Socket" is a term usually used to refer to some abstraction in software of what goes on in a plain TCP/IP (or equivalent) conversation. A socket is something provides reliable, point-to-point conversation in data packets between two points identified by IP numbers. Most programming languages or libraries provide something that models a socket in this sense.
Websockets is a protocol that allows socket-like communication to be initiated between a web browser and its cients, as an extension to the basic HTTP protocol. The conversation need not be strictly point-to-point, as it can pass through proxies, as HTTP can. Websocket conversation is initiated by an exchange of headers similar to HTTP.
XMPP is an XML-based messaging protocol, used by "instant" messaging-type applications.

is http based on socket?

every connection to web server need a open port(default 80), so is it correct by regarding "http is based on socket"
or can I understand by this "TCP is a protocol, Socket implemented TCP, HTTP is based on TCP, so HTTP is based on Socket"?
HTTP is an application protocol, Socket is an operating system API. This means HTTP can not be based on sockets the same as cars are not based on gasoline.
Relationship between Socket and HTTP:
Sockets can be used to implement a HTTP server/client since sockets can be used to implement any kind of TCP server/client and HTTP is an application layer protocol on top of TCP.
But note that sockets are not essential to implement HTTP, i.e. you could use any other kind of API which manages to send network packets to implement it.

What exactly is Socket

I don't know exactly what socket means.
A server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.
When the server accepts the connection, it gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client.
It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
So, socket is some class created in memory? And for every client connection there is created new instance of this class in memory? Inside socket is written the local port and port and IP number of the client which is connected. Can someone explain me more in details the definition of socket?
Thanks
A socket is effectively a type of file handle, behind which can lie a network session.
You can read and write it (mostly) like any other file handle and have the data go to and come from the other end of the session.
The specific actions you're describing are for the server end of a socket. A server establishes (binds to) a socket which can be used to accept incoming connections. Upon acceptance, you get another socket for the established session so that the server can go back and listen on the original socket for more incoming connections.
How they're represented in memory varies depending on your abstraction level.
At the lowest level in C, they're just file descriptors, a small integer. However, you may have a higher-level Socket class which encapsulates the behaviour of the low-level socket.
According to "TCP/IP Sockets in C-Practical Guide for Programmers" by Michael J. Doonahoo & Kenneth L. Calvert (Chptr 1, Section 1.4, Pg 7):
A socket is an abstraction through which an application may send
and receive data,in much the same way as an open file allows an application to read and write data to stable storage.
A socket allows an application to "plug in" to the network and communicate
with other applications that are also plugged in to the same network.
Information written to the socket by an application on one machine can be
read by an application on a different machine, and vice versa.
Refer to this book to get clarity about sockets from a programmers point of view.
A network socket is one endpoint in a communication flow between two programs running over a network.
A socket is the combination of IP address plus port number
This is the typical sequence of sockets requests from a server application in the connectionless context of the Internet in which a server handles many client requests and does not maintain a connection longer than the serving of the immediate request:
Steps to implement
At Server side
initilize socket()
--
bind()
--
recvfrom()
--
(wait for a sendto request from some client)
--
(process the sendto request)
--
sendto (in reply to the request from the client...for example, send an HTML file)
A corresponding client sequence of sockets requests would be:
socket()
--
bind()
--
sendto()
--
recvfrom()
so that you can make a pipeline connection ..
for more http://www.steves-internet-guide.com/tcpip-ports-sockets
I found this article in online.
So to put it all back together, a socket is the combination of an IP
address and a port, and it acts as an endpoint for receiving or
sending information over the internet, which is kept organized by TCP.
These building blocks (in conjunction with various other protocols and
technologies) work in the background to make every google search,
facebook post, or introductory technical blog post possible.
https://medium.com/swlh/understanding-socket-connections-in-computer-networking-bac304812b5c
Socket definition
A communication between two processes running on two computer systems can be completely specified by the association: {protocol, local-address, local-process, remote-address, remote-process} We also define a half association as either {protocol, local-address, local-process} or {protocol, remote-address, remote-process}, which specify half of a connection. This half association is also called socket, or transport address. The term socket has been popularized by the Berkeley Unix networking system, where it is "an end point of communication", which corresponds to the definition of half association.

difference between socket programming and Http programming

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

Difference between Socket Connection and XMPP Connection

I am new to these two connections. I used to work with HTTP Connection before and now I wanna move to a new type of connection. I searched about connections and got these two types: Socket Connection and XMPP Connection. What is the main difference between them? I wanna use this for a chat application so which type is better recommended? Thanks
XMPP is a protocol for communication and so is HTTP. Both XMPP and HTTP will internally use socket connections.
You are confused between application protocols and network layer.
Socket is the essential thing of any existing connections there is. If you want to use any connection that has a host point and a port, very likely (unless they write their own), they use Socket (to open a connection on a given port, send a message, and close the connection port) internally.