The difference between socket programming and Http programming

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

+62
sockets network-programming
Feb 27 '13 at 9:12
source share
4 answers

HTTP is the application protocol. This basically means that HTTP itself cannot be used to transfer information to / from a remote endpoint. Instead, it relies on the underlying protocol, which is TCP over HTTP.

enter image description here

If you are interested, you can learn more about OSI layers .

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

This means that if you want to use TCP, you are using sockets. But you can also use sockets to communicate using HTTP, but then you need to decode / encode messages according to the HTTP specification ( RFC2616 ). Since this can be a huge task for most developers, we also got ready-made clients in our developer infrastructures (e.g. .NET), for example, the WebClient or HttpWebRequest .

+61
Feb 27 '13 at 10:48
source

In HTTP, you use the high-level HTTP protocol (which runs on top of the socket). It is session-independent, which means that you send a text request, for example GET google.com , and receive text or binary data in response, after which the 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 one 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}); 
+17
Feb 27 '13 at 9:18
source

HTTP programming or an HTTP request is used to loosely connect and communicate with the neutral language technology platform , where when socket programming is used where the language of the specification protocol

+1
Nov 13 '14 at 5:42 on
source

HTTP connection

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

Connector Connection

  • Socket is used to transfer data between systems. It just connects the two systems together, the IP address is the address of the machine through the IP network.
  • With socket connections, you can create your own protocol for a network connection between two systems.
  • When connecting a Socket, you must take care of all the details below the TCP / IP connection.
0
Dec 04 '17 at 4:31 on
source



All Articles