26-07-2012, 02:47 PM
Socket Programming
Socket Programming.pdf (Size: 323.01 KB / Downloads: 114)
INTRODUCTION
Internet and WWW have emerged as global ubiquitous media for communication and changed the way
we conduct science, engineering, and commerce. They are also changing the way we learn, live, enjoy,
communicate, interact, engage, etc. The modern life activities are getting completely centered around or
driven by the Internet.
To take advantage of opportunities presented by the Internet, businesses are continuously seeking new
and innovative ways and means for offering their services via the Internet. This created a huge demand
for software designers and engineers with skills in creating new Internet-enabled applications or porting
existing/legacy applications to the Internet platform. The key elements for developing Internet-enabled
applications are a good understanding of the issues involved in implementing distributed applications and
sound knowledge of the fundamental network programming models.
Client/Server Communication
At a basic level, network-based systems consist of a server , client , and a media for communication
as shown in Fig. 13.1. A computer running a program that makes a request for services is called client
machine. A computer running a program that offers requested services from one or more clients is called
server machine. The media for communication can be wired or wireless network.
Hosts Identifi cation and Service Ports
Every computer on the Internet is identifi ed by a unique, 4-byte IP address . This is typically written in
dotted quad format like 128.250.25.158 where each byte is an unsigned value between 0 and 255. This
representation is clearly not user-friendly because it does not tell us anything about the content and then it is
diffi cult to remember. Hence, IP addresses are mapped to names like www.buyya.com or www.google.com,
which are easier to remember. Internet supports name servers that translate these names to IP addresses.
In general, each computer only has one Internet address. However, computers often need to communicate
and provide more than one type of service or to talk to multiple hosts/computers at a time. For example,
there may be multiple ftp sessions, web connections, and chat programs all running at the same time. To
distinguish these services, a concept of port s, a logical access point, represented by a 16-bit integer number
is used. That means, each service offered by a computer is uniquely identifi ed by a port number. Each
Internet packet contains both the destination host address and the port number on that host to which the
message/request has to be delivered. The host computer dispatches the packets it receives to programs by
looking at the port numbers specifi ed within the packets. That is, IP address can be thought of as a house
address when a letter is sent via post/snail mail and port number as the name of a specifi c individual to
whom the letter has to be delivered.
Sockets and Socket-based Communication
Sockets provide an interface for programming networks at the transport layer. Network communication
using Sockets is very much similar to performing fi le I/O. In fact, socket handle is treated like fi le handle.
The streams used in fi le I/O operation are also applicable to socket-based I/O. Socket-based
communication is independent of a programming language used for implementing it. That means, a socket
program written in Java language can communicate to a program written in non-Java (say C or C++) socket
program.
A server (program) runs on a specifi c computer and has a socket that is bound to a specifi c port. The
server listens to the socket for a client to make a connection request (see Fig. 13.4a). If everything goes
well, the server accepts the connection (see Fig. 13.4b). Upon acceptance, the server gets a new socket
bound to a different port. It needs a new socket (consequently a different port number) so that it can
continue to listen to the original socket for connection requests while serving the connected client.