Experiment 1
Experiment 1
Aim:
To implement concurrent day-time client-server application using socket programming.
Description/Theory:
There are two major transport layer protocols to communicate between hosts :-
1. TCP :- TCP is a connection-oriented protocol, which means a connection is established and
maintained until the application programs at each end have finished exchanging messages. It
determines how to break application data into packets that networks can deliver, sends packets to
and accepts packets from the network layer, manages flow control, and—because it is meant to
provide error-free data transmission—handles retransmission of dropped or garbled packets as well
as acknowledgement of all packets that arrive.
2. UDP:- In UDP, the client does not form a connection with the server like in TCP and instead just
sends a datagram. Similarly, the server need not accept a connection and just waits for datagrams to
arrive. Datagrams upon arrival contain the address of sender which the server uses to send data to
the correct client.
A socket is a combination of IP address and port on one system. On each system a socket exists for a
process interacting with the socket on other system over the network. A combination of local socket
and the socket at the remote system is also known a ‘Four tuple’ or ‘4-tuple’. Each connection
between two processes running at different systems can be uniquely identified through their 4-
tuple.
Client
CODE:
1. server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main()
{
struct sockaddr_in sa;
int sockfd, coontfd;
char str[1025];
time_t tick;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error in creating socket\n");
exit(0);
}
else {
printf("Socket Created Successfully!\n");
}
bzero(&sa, sizeof(sa));
memset(str, '0', sizeof(str));
sa.sin_family = AF_INET;
sa.sin_port = htons(5600);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr*)&sa, sizeof(sa)) < 0) {
printf("Bind Error\n");
}
else
printf("Bind Successful!\n");
listen(sockfd, 10);
while (1) {
coontfd = accept(sockfd, (struct sockaddr*)NULL , NULL); // Accept a request from client
printf("Accepted Incoming!\n");
tick = time(NULL);
snprintf(str, sizeof(str), "%.24s\r\n", ctime(&tick)); // read sys time and write to buffer
printf("Sent Information\n");
printf("%s\n", str);
write(coontfd, str, strlen(str)); // send buffer to client
}
close(sockfd); // close the socket
return 0;
}
2. client.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main()
{
struct sockaddr_in sa;
int n, sockfd;
char buff[1025];
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error in Socket Creation!\n");
exit(0);
}
else
printf("Socket created Successfully!\n");
bzero(&sa, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(5600);
if (connect(sockfd, (struct sockaddr_in*)&sa, sizeof(sa)) < 0) {
printf("Connection failed!\n");
exit(0);
}
else
printf("Connection Established!\n");
Result/Output:
Server Side:
Client Side:
Discussion:
If we are creating a connection between client and server using TCP then it has few functionality like,
TCP is suited for applications that require high reliability, and transmission time is relatively less
critical. It is used by other protocols like HTTP, HTTPs, FTP, SMTP, Telnet. TCP rearranges data packets
in the order specified. There is absolute guarantee that the data transferred remains intact and arrives
in the same order in which it was sent. TCP does Flow Control and requires three packets to set up a
socket connection, before any user data can be sent. TCP handles reliability and congestion control. It
also does error checking and error recovery. Erroneous packets are retransmitted from the source to
the destination.
• UDP is a connectionless protocol where the server waits for a request from a client to
become active. Each connection is treated as a new one
• On a local system i.e. within the same computer, the loop back address should be used as
the argument to the client.
• The connect procedure follows the Three way handshake process to establish the
connection