Is Socket wrapper TCP/IP's transport layer? - sockets

We often hear about this saying:
Socket is a wrapper around the TCP/IP protocol.
but can make it more clarity? is Socket wrapper TCP/IP's transport layer? or every layer except application layer.

The Socket-interface is more like an API for various Internet and IPC protocols.
It is not really a protocol layer, but in implementation level, it may be used between layers.
For example, a HTTP client may use Socket-API for opening TCP connections to HTTP servers.
In the example the TCP is the transpont layer protocol. Usually TCP uses IP as network layer protocol. And HTTP is the protocol on top of TCP.
The Socket-interface uses term 'socket' for protocol end-points.
In many systems, a file descriptor type that is created by using socket() or accept() functions is called a 'socket'.

Related

What application layer protocol is being used when using sockets?

When using web browsers, the application layer protocol being used is HTTP. Whereas I frequently use sockets to create a connection to a server and pass along strings, a frequently used example in Python could be
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
clientsocket.send('hello')
What application layer protocol is being used when sending the string 'hello' with this basic example?
No specific application layer protocol is used in your case. An application layer protocol is some kind of standard how messages are exchanged on top of TCP/UDP whatever transport layer. These standards are defined so that different implementations can interact with each other by just implementing the specific standard.
You can also use sockets without using a standardized application layer protocols but instead just make up what kind of messages you send by your own - and this is exactly what you did.

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's the difference of HTTP Programming and Socket Programming? [duplicate]

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.

what's the differenct amone a tcp connection, socket connection and a http connection?

It seems that many "connection" concept exist. such as socket connection, tcp connection and http connection.
so, what the difference?
They're different degrees of specialization: an HTTP connection is a type of TCP connection is a type of socket connection.
For two entities to communicate they need a hook to each other to pass messages across - These hooks are sockets. We can say they are identities of the process. Communication b/w these sockets would be over a socket connection.
Now, how does one send messages and receive messages? Who decides to open a connection request to a socket, how does one close. Many questions arise isn't? Here comes the your TCP. By following the rules put forth by TCP, applications engage in a message exchange using TCP protocol over the sockets. Now you can refer to them as TCP connection.
Now, it need not be just stream of data bytes that the two applications exchange using the TCP protocol and b/w the two sockets. Applications can define their own protocol - Like - I am looking for a content - do you have it? - Yes, here it is. One such applications defined protocol is HTTP. The web page of SO that you used your browser to read is over HTTP- TCP - and two sockets belonging to SO server and your browser.

iPhone Native system routines(datagram-socket-type)

Sockets are full-duplex communication
channels between processes either
local to the same host machine or
where one process is on a remote host.
Unlike pipes, in which data goes in
one direction only, sockets allow
processes both to send and receive
data. NSFileHandle facilitates
communication over stream-type sockets
by providing mechanisms run in
background threads that accept socket
connections and read from sockets.
NSFileHandle currently handles only
communication through stream-type
sockets. If you want to use datagrams
or other types of sockets, you must
create and manage the connection using
native system routines.
The process on one end of the
communication channel (the server)
starts by creating and preparing a
socket using system routines. These
routines vary slightly between BSD and
non-BSD systems, but consist of the
same sequence of steps:
Create a stream-type socket of a
certain protocol.
Bind a name to the socket.
Adding itself as an observer of
NSFileHandleConnectionAcceptedNotification.
Sending acceptConnectionInBackgroundAndNotify
to this file handle object.
This method accepts the connection in the
background, creates a new NSFileHandle
object from the new socket descriptor,
and posts an NSFileHandleConnectionAcceptedNotification.
Now I saw Michael answer .
About the differences between “stream-type” socket and a “datagram” socket type
Do you have iPhone implementation example for native system routines(datagram-socket-type)?
Ok first I found what I needed, with CFSocket API which will allow me to implement UDP Synchronization.
CFSocket API
Sockets are the most basic level of network communications. A socket acts in a similar manner to a telephone jack. It allows you to connect to another socket (either locally or over a network) and send data to that socket.
The most common socket abstraction is BSD sockets. CFSocket is an abstraction for BSD sockets. With very little overhead, CFSocket provides almost all the functionality of BSD sockets, and it integrates the socket into a run loop. CFSocket is not limited to stream-based sockets (for example, TCP), it can handle any type of socket.
You could create a CFSocket object from scratch using the CFSocketCreate function, or from a BSD socket using the CFSocketCreateWithNative function. Then, you could create a run-loop source using the function CFSocketCreateRunLoopSource and add it to a run loop with the function CFRunLoopAddSource. This would allow your CFSocket callback function to be run whenever the CFSocket object receives a message.
Regardless I found AsyncSocket API.
CocoaAsyncSocket supports TCP and UDP. The AsyncSocket class is for TCP, and the AsyncUdpSocket class is for UDP. Each class is described below.
AsyncSocket is a TCP/IP socket networking library that wraps CFSocket and CFStream. It offers asynchronous operation, and a native cocoa class complete with delegate support. Here are the key features:
Queued non-blocking reads and writes, with optional timeouts. You tell it what to read or write, and it will call you when it's done.
Automatic socket acceptance. If you tell it to accept connections, it will call you with new instances of itself for each connection. You can, of course, disconnect them immediately.
Delegate support. Errors, connections, accepts, read completions, write completions, progress, and disconnections all result in a call to your delegate method.
Run-loop based, not thread based. Although you can use it on main or worker threads, you don't have to. It calls the delegate methods asynchronously using NSRunLoop. The delegate methods include a socket parameter, allowing you to distinguish between many instances.
Self-contained in one class. You don't need to muck around with streams or sockets. The class handles all of that.
Support for TCP streams over IPv4 and IPv6.
The library is public domain, originally written by Dustin Voss. Now available in a public setting to allow and encourage its continued support.
AsyncUdpSocket is a UDP/IP socket networking library that wraps CFSocket. It works almost exactly like the TCP version, but is designed specifically for UDP. This includes queued non-blocking send/receive operations, full delegate support, run-loop based, self-contained class, and support for IPv4 and IPv6.
CocoaAsyncSocket
And here is the CFSocket Reference
CFSocket Reference