A socket is one end of a two-way communications link between two programs running on the network.
Creation of sockets is done using the socket() system call.
int socket(int address_family, int socket_type, int proto_family);
address_family defines the type of addresses we want this socket to use.
socket_type could be the type of interaction (and type or protocol) we want to use.
proto_family selects which protocol we want to socket to use. We will usually leave this value as 0 (or the constant PF_UNSPEC on some systems), and let the system choose the most suitable protocol for us. As for the protocol itself, In the Internet address family, a socket type of SOCK_STREAM will cause the protocol type to be set to TCP. A socket type of SOCK_DGRAM (Datagram socket) will cause the protocol type to be set to UDP.
The socket system call returns a file descriptor which will be used to reference the socket in later requests by the application program. If the call fails, however (due to lack of resources) the value returned will be negative (note that file descriptors have to be non-negative integers).
As an example, suppose that we want to write a TCP application. This application needs at least one socket in order to communicate across the Internet, so it will contain a call such as this:
int s; /* descriptor of socket */
s = socket(AF_INET, SOCK_STREAM, 0);
How to Associate a connection with Socket?
After a socket is created, it still needs to be told between which two end points it will communicate. It needs to be bound to a connection. There are two steps to this binding. The first is binding the socket to a local address. The second is binding it to a remote (foreign) address.
Binding to a local address could be done either explicitly, using the bind() system call, or implicitly, when a connecting is established. Binding to the remote address is done only when a connection is established. To bind a socket to a local address, we use the bind() system call, which is defined as follows:
int bind(int socket, struct sockaddr *address, int addrlen);
There are 4 possible variations of address binding that might be used when binding a socket in the Internet address family.
The first is binding the socket to a specific address, i.e. a specific IP number and a specific port. This is done when we know exactly where we want to receive messages. Actually this form is not used in simple servers, since usually these servers wish to accept connections to the machine, no matter which IP interface it came from.
The second form is binding the socket to a specific IP number, but letting the system choose an unused port number. This could be done when we don't need to use a well-known port.
The third form is binding the socket to a wild-card address called INADDR_ANY (by assigning it to the sockaddr_in variable), and to a specific port number. This is used in servers that are supposed to accept packets sent to this port on the local host, regardless of through which physical network interface the packet has arrived (remember that a host might have more than one IP address).
The last form is letting the system bind the socket to any local IP address and to pick a port number by itself. This is done by not using the bind() system call on the socket. The system will make the local bind when a connection through the socket is established, i.e. along with the remote address binding. This form of binding is usually used by clients, which care only about the remote address (where they connect to) and don't need any specific local port or local IP address. However, there are exceptions here too.
How to send and receive data from sockets?
int read(int socket, char *buffer, int buflen);
int write(int socket, char *buffer, int buflen);
The return value may be:
0 - The connection was closed by the remote host.
-1 - The read system call was interrupted, or failed for some reason.
n - The read system call put 'n' bytes into the buffer we supplied it with.
what are different types of Socket?
Stream Sockets, Datagram Sockets, Raw Sockets, Packet Socket.
what is ICMP?
Internet control message protocol is used by hosts and gateway to send notifications of datagram problems to sender. It uses echo test to check whether it is responding or not.
what is Router? How to Design a router?
Router is used to route packets from one network to other.
what is Gateway? How to Design a gateway?
Conversion from one protocol to another.
what is network protocol time?
The Network Time Protocol (NTP) is a protocol for synchronising the clocks of computer systems over packet-switched, variable-latency data networks. NTP uses UDP as its transport layer. It is designed particularly to resist the effects of variable latency.
what situation packet goes into infinite loop?
If ttl value in IP header is not properly decremented at every router.
what protocol is used to retrieve mails?
POP3 and IMAP4 are used to retrieve mails.
IMAP4 stores a copy of message on the server whereas POP3 does not.
what is IP tunneling? how it works?
IP tunneling (IP encapsulation) is a technique to encapsulate IP datagram within IP datagrams, which allows datagrams destined for one IP address to be wrapped and redirected to another IP address. IP encapsulation is now commonly used in Extranet, Mobile-IP, IP-Multicast, tunneled host or network.
what is tcp 3 way handshaking?
Client starts active open after passive open in server side in TCP. The client sends SYNC, then the server send SYNC+ACK, last client send ACK.
What are the different type of networking working devices?
Also called a regenerator, it is an electronic device that operates only at physical layer. It receives the signal in the network before it becomes weak, regenerates the original bit pattern and puts the refreshed copy back in to the link.
These operate both in the physical and data link layers of LANs of same type. They divide a larger network in to smaller segments. They contain logic that allow them to keep the traffic for each segment separate and thus are repeaters that relay a frame only the side of the segment containing the intended recipent and control congestion.
They relay packets among multiple interconnected networks (i.e. LANs of different type). They operate in the physical, data link and network layers. They contain software that enable them to determine which of the several possible paths is the best for a particular transmission.
They relay packets among networks that have different protocols (e.g. between a LAN and a WAN). They accept a packet formatted for one protocol and convert it to a packet formatted for another protocol before forwarding it. They operate in all seven layers of the OSI model.
what is frame relay? which layer it comes?
Frame relay is a packet switching technology, it comes in data link layer.
what are the names of PDU in higher layers?
Transport layer: segment.
Network Layer: packet.
Data link layer: frames.
Physical layer: bits/ bytes.