0% found this document useful (0 votes)
16 views4 pages

CNS Assignment 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

CNS Assignment 8

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Aim:

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.

• Stream Sockets − Delivery in a networked environment is guaranteed. If you send through


the stream socket three items "A, B, C", they will arrive in the same order − "A, B, C".
These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is
impossible, the sender receives an error indicator. Data records do not have any boundaries.

• Datagram Sockets − Delivery in a networked environment is not guaranteed. They're


connectionless because you don't need to have an open connection as in Stream Sockets −
you build a packet with the destination information and send it out. They use UDP (User
Datagram Protocol).

• 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.

State diagram for server and client model

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)

sockfd: socket descriptor, an integer (like a file-handle)


domain: integer, communication domain e.g., AF_INET (IPv4 protocol) , AF_INET6 (IPv6
protocol)
type: communication type
SOCK_STREAM: TCP(reliable, connection oriented)
SOCK_DGRAM: UDP(unreliable, connectionless)
protocol: Protocol value for Internet Protocol(IP), which is 0. This is the same number
which appears on protocol field in the IP header of a packet.(man protocols for more details)

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.

Stages for Client


1. Socket connection: Exactly same as that of server’s socket creation
2. Connect:
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
The connect() system call connects the socket referred to by the file descriptor sockfd to the
address specified by addr. Server’s address and port is specified in addr.

memset(void *str, int c, size_t n)

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.

You might also like