difference between socket programming and Http pro

2019-01-16 01:09发布

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

4条回答
地球回转人心会变
2楼-- · 2019-01-16 01:34

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});
查看更多
相关推荐>>
3楼-- · 2019-01-16 01:41

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.
查看更多
Ridiculous、
4楼-- · 2019-01-16 01:49

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.

查看更多
家丑人穷心不美
5楼-- · 2019-01-16 01:51

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

查看更多
登录 后发表回答