Thursday, March 13, 2008

SOCKETS

MEANT FOR MCA II YEAR TELANGANA UNIVERSITY NIZAMABAD

WHAT IS SOCKET
•"sockets" are a way to speak to other programs using standard Unix file descriptors.
•Unix programs do any sort of I/O, they do it by reading or writing to a file descriptor.
•A file descriptor is simply an integer associated with an open file. But that file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else. Everything in Unix is a file! So when you want to communicate with another program over the Internet, do it through a file descriptor.
•To get the file descriptor for network communication, make a call to the socket() system routine. It returns the socket descriptor, and communicate through it using the specialized send() and recv()socket calls.
• the normal read() and write() calls can also be used to communicate through the socket. but send() and recv() offer much greater control over data transmission.
INTERNET SOCKETS
•There are two types of Internet sockets? One is "Stream Sockets"; the other is "Datagram Sockets", which may be referred to as "SOCK_STREAM" and "SOCK_DGRAM", respectively.
•Datagram sockets are sometimes called "connectionless sockets".
STREAM SOCKETS
•Stream sockets are reliable two-way connected communication streams. If you output two items into the socket in the order "1, 2", they will arrive in the order "1, 2" at the opposite end. They will also be error-free.
•What uses stream sockets? the telnet application, uses stream sockets. All the characters you type need to arrive in the same order you type them, Also, web browsers use the HTTP protocol which uses stream sockets to get pages.
•How do stream sockets achieve this high level of data transmission quality? They use a protocol called "The Transmission Control Protocol", known as "TCP". TCP makes sure that data arrives sequentially and error-free.
DATA GRAM SOCKETS
•What about Datagram sockets? Why are they called connectionless? Why are they unreliable? Well, here are some facts: if you send a datagram, it may arrive. It may arrive out of order. If it arrives, the data within the packet will be error-free.
•Datagram sockets also use IP for routing, but they don't use TCP; they use the "User Datagram Protocol", or "UDP".
•Why are they connectionless? it's because you don't have to maintain an open connection as you do with stream sockets. You just build a packet, slap an IP header on it with destination information, and send it out. No connection needed.
•Datagram sockets are generally used either when a TCP stack is unavailable or when a few dropped packets here and there don't mean the end of the Universe.
• Sample applications of datagram sockets are : tftp, bootp, multiplayer games, streaming audio, video conferencing, etc.
•tftp and bootp are used to transfer binary applications from one host to another.
•tftp and similar programs have their own protocol on top of UDP. For example, the tftp protocol says that for each packet that gets sent, the recipient has to send back a packet that says, "I got it!" (an "ACK" packet.) If the sender of the original packet gets no reply in, say, five seconds, he'll re-transmit the packet until he finally gets an ACK. This acknowledgment procedure is very important when implementing reliable SOCK_DGRAM applications.
DATA ENCAPSULATION
•how networks really work, and to show some examples of how SOCK_DGRAM packets are built.
Data Encapsulation.
•Data Encapsulation, it says this: a packet is born, the packet is wrapped ("encapsulated") in a header (and rarely a footer) by the first protocol (say, the TFTP protocol), then the whole thing (TFTP header included) is encapsulated again by the next protocol (say, UDP), then again by the next (IP), then again by the final protocol on the hardware (physical) layer (say, Ethernet).
•When another computer receives the packet, the hardware strips the Ethernet header, the kernel strips the IP and UDP headers, the TFTP program strips the TFTP header, and it finally has the data.
•An ISO-OSI layered model more consistent with Unix might be:
Application Layer (telnet, ftp, etc.)
Host-to-Host Transport Layer (TCP, UDP)
Internet Layer (IP and routing)
Network Access Layer (Ethernet, ATM, or whatever)
these layers correspond to the encapsulation of the original data.
• to build a simple packet, All you have to do for stream sockets is send() the data out. All you have to do for datagram sockets is encapsulate the packet in the method of your choosing and sendto() it out. The kernel builds the Transport Layer and Internet Layer on for you and the hardware does the Network Access Layer.
DATA TYPES USED BY SOCKETS INTERFACE a socket descriptor
• A socket descriptor is Just a regular int. type

No comments: