Wednesday, May 6, 2009

Cycle 3: Implement a Subnet of FTP using TCP/IP :: Part 1

This look tough..where will we go? thanx to the Unix for the concept of "socket".so lets us detail every thing abt socket.

Socket

Essentially, a socket is an abstraction for network communication, just as a file is
an abstraction for file system communication.In general, an application that performs net-
work input and output needs to perform the five basic functions : open, close, read, write, and control.

Sockets are a basic component of interprocess and intersystem communication. A socket is an endpoint of communication to which a name can be bound. It has a type and one or more associated processes.

The concept of socket was created by the Berkeley Unix team.By extending the file/file descriptor concept, it is easy to visualize communicating across a network. To communicate across a network, a connection (socket) to the network must be opened. Once it’s opened, data is read from or written to the socket. When communication is finished, the connection to the network is closed and the resources used by the socket are released.

Sockets can be used in two ways. Once created, a socket can wait for an incoming connection, or it can initiate a connection to another socket on a remote host, or even its own local host in some cases. A socket used by a client program to initiate a connection to a server is known as an active socket. A socket that acts as a server and waits for an incoming connection is known as a passive socket.

Using Socket

The sequence of function calls for two simple socket applications: a client and a
server is shown below.



On the client side, the application creates the socket with socket(), calls
connect() to connect to the server, and then interacts with the server by using
write() to send requests to the server and read() to read responses from the
server. When the client is finished, it calls close().

On the server side, the application creates the socket with socket(), and then
calls bind() to specify which local address and port to use, and then proceeds to
call listen() to set the length of the connection queue. The connection queue is
the number of requests that the application should hold while waiting for the
server to process any current request. Once the connection queue is set, the
server calls accept() and waits until the next connection request arrives from the
client. When the request arrives, the server uses read() and write() to read the
request and write the response. When finished, the server calls close() to termi-
nate the connection to the client and returns to the accept() function, where it
waits for the next connection request from a client.

The socket() function allow for the possibility that other protocols might be used instead of
TCP/IP .

int socketpair(int domain, int type, int protocol)

Family --> Protocol or address family (AF_INET for TCP/IP; AF_UNIX for internal)
Type --> Type of service (SOCK_STREAM for TCP; SOCK_DGRAM for UDP)
Protocol --> The protocol number to use, typically a zero (0) to use the default for a
given family and type.

A call to socket(), then, for the purposes of opening a connection to another
host and communicating with TCP, would look like this:

mySocket = socket(PF_INET, SOCK_STREAM, 0);

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.