Sunday, August 24, 2008

Sockets

Sockets

A socket is a bidirectional communication device that can be used to communicate with another process on the same machine or with a process running on other machines.

Sockets are the only interprocess communication that permit communication between processes on different computers. Internet programs such as Telnet, rlogin, FTP, talk, and the World Wide Web use sockets.

For example, you can obtain the WWW page from a Web server using the Telnet program because they both use sockets for network communications.

To open a connection to a WWW server at www.nidhish.co.nr, use
telnet
www.nidhish.co.nr 80.The magic constant 80 specifies
a connection to the Web server programming running
www.nidhish.co.nr instead of some other process.

Try typing GET / after the connection is established.This sends a message through the socket to the Web server, which replies by sending the home page’s HTML source and then closing the connection.

Networks

Most network application can be divided into two pieces: a client and a server. A client is the side that initiates the communication process, where as the server responds to incoming client requests.





There are numerous network protocols, such as Netbios, RPC (Remote Procedure Call), DCOM, Pipe, IPC (Inter-process Communication) that can be used for the Comm Link. We will only look at TCP/IP. In particular we will look at IPv4 since this is widely implemented by many socket vendors.

TCP Transmission Control Protocol

Although TCP can be implemented to work over any transport protocol, it's usually synonymous with IP. TCP is a connection -oriented stream protocol (like a telephone call). TCP communication happens using a handshake process, where each data that is sent is acknowledge by the recipient within the time of TCP’s timer value. TCP provides many services such as data reliability, error checking, and flow control. If a data packet is corrupt or lost (not acknowledged), TCP will retransmitted the data from the client side automatically. Because the route a packet takes can be many, one packet may arrive before the one sent earlier. As data packets arrive, it is the job of TCP to assemble the packets into the proper order. This is shown below with a factious network topology layout, where the data packet takes (n) number of hops to get from the source to the destination. On a bigger network like the Internet, there are many routes a data packet can take to arrive at its final destination.

Network Hop Topology




Byte Ordering

There are two types of memory byte ordering in use today that are very much machine dependent. They are known as little-endian and big-endian, because of this we have to be very careful how we interpret numerical data. If we do not take into account the endiannes, the numerical data we read will be corrupt.





When working with numeric data, one needs to convert from machine (host) byte order to network byte order when sending data (write-op), and then from network byte order to machine byte order when retrieving data (read-op). The APIs to make the conversion are:

htons()and htonl()
//host to network

uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);


ntohs() and ntohl()
//network to host

uint16_t ntohs(uint16_t net16bitvalue);
unit32_t ntohl(unit32_t net32bitvalue);

0 comments: