CNS Assignment 8
CNS Assignment 8
Write a program using TCP socket for wired network for following
a. Say Hello to Each other
b. File transfer
c. Calculator
Theory:
Socket:
Sockets allow communication between two different processes on the same or different machines.
To be more precise, it's a way to talk to other computers using standard Unix file descriptors. In
Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an
integer associated with an open file and it can be a network connection, a text file, a terminal, or
something else.
Socket Types
There are four types sockets available to the users. The first two are most commonly used and the
last two are rarely used.
Processes are presumed to communicate only between sockets of the same type but there is no
restriction that prevents communication between sockets of different types.
• Raw Sockets − These provide users access to the underlying communication protocols,
which support socket abstractions. These sockets are normally datagram oriented, though
their exact characteristics are dependent on the interface provided by the protocol. Raw
sockets are not intended for the general user; they have been provided mainly for those
interested in developing new communication protocols, or for gaining access to some of the
more cryptic facilities of an existing protocol.
• Sequenced Packet Sockets − They are similar to a stream socket, with the exception that
record boundaries are preserved. This interface is provided only as a part of the Network
Systems (NS) socket abstraction, and is very important in most serious NS applications.
Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP)
or Internet Datagram Protocol (IDP) headers on a packet or a group of packets, either by
writing a prototype header along with whatever data is to be sent, or by specifying a default
header to be used with all outgoing data, and allows the user to receive the headers on
incoming packets.
What is socket programming?
Socket programming is a way of connecting two nodes on a network to communicate with each
other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the
other to form a connection. Server forms the listener socket while client reaches out to the server.
Header Files :
netinet/in.h Defines Internet constants and structures.
arpa/nameser.h Contains Internet name server information.
netdb.h Contains data definitions for socket subroutines.
resolv.h Contains resolver global definitions and variables.
sys/socket.h Contains data definitions and socket structures.
sys/socketvar.h Defines the kernel structure per socket and contains buffer queues.
sys/types.h Contains data type definitions.
sys/un.h Defines structures for the UNIX interprocess communication
domain.
sys/ndd_var.h Defines structures for the AIX Network Device Driver (NDD)
domain.
sys/atmsock.h Contains constants and structures for the Asynchronous Transfer
Mode (ATM) protocol in the AIX NDD domain.
Stages for server
1. Socket creation:
int sockfd = socket(domain, type, protocol)
2. Bind:
int bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
After creation of the socket, bind function binds the socket to the address and port number specified
in addr(custom data structure). In the example code, we bind the server to the localhost, hence we
use INADDR_ANY to specify the IP address.
3. Listen:
int listen(int sockfd, int backlog);
It puts the server socket in a passive mode, where it waits for the client to approach the server to
make a connection. The backlog, defines the maximum length to which the queue of pending
connections for sockfd may grow. If a connection request arrives when the queue is full, the client
may receive an error with an indication of ECONNREFUSED.
4. Accept:
int new_socket= accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
It extracts the first connection request on the queue of pending connections for the listening socket,
sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. At
this point, connection is established between client and server, and they are ready to transfer data.
Parameters
• str -- This is a pointer to the block of memory to fill.
• c -- This is the value to be set. The value is passed as an int, but the function fills the block
of memory using the unsigned char conversion of this value.
• n -- This is the number of bytes to be set to the value.
fopen:
The fopen() function shall open the file whose pathname is the string pointed to by filename, and
associates a stream with it.
The mode argument points to a string. If the string is one of the following, the file shall be opened
in the indicated mode. Otherwise, the behavior is undefined.
1. r or rb : Open file for reading.
2. w or wb : Truncate to zero length or create file for writing.
3. a or ab : Append; open or create file for writing at end-of-file.
4. r+ or rb+ or r+b : Open file for update (reading and writing).
5. w+ or wb+ or w+b: Truncate to zero length or create file for update.
6. a+ or ab+ or a+b: Append; open or create file for update, writing at end-of-file.
The htonl() function converts the unsigned integer hostlong from host byte order to network byte
order.
The htons() function converts the unsigned short integer hostshort from host byte order to network
byte order.
The ntohl() function converts the unsigned integer netlong from network byte order to host byte
order.
The ntohs() function converts the unsigned short integer netshort from network byte order to host
byte order.
Conclusion:
Successfully impleneted the TCP socket programming.