0% found this document useful (0 votes)
15 views5 pages

Experiment 1

The document outlines the implementation of a concurrent day-time client-server application using socket programming, focusing on TCP and UDP protocols. It provides an algorithm for both server and client processes, along with example code for each. Additionally, it discusses the functionalities and characteristics of TCP and UDP, highlighting their differences in connection handling and reliability.

Uploaded by

Harshit Jindal
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)
15 views5 pages

Experiment 1

The document outlines the implementation of a concurrent day-time client-server application using socket programming, focusing on TCP and UDP protocols. It provides an algorithm for both server and client processes, along with example code for each. Additionally, it discusses the functionalities and characteristics of TCP and UDP, highlighting their differences in connection handling and reliability.

Uploaded by

Harshit Jindal
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/ 5

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.

The entire process can be broken down into following steps:


Algorithm:
The entire process can be broken down into following steps:
Server

1. Create UDP socket


2. Bind socket to address
3. Wait for datagram from client process and reply to client request
4. Repeat while server is active

Client

1. Create UDP socket


2. Send request to server
3. Wait for datagram from server
4. Process and reply from server
5. Close socket and exit

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");

if (n = read(sockfd, buff, sizeof(buff)) < 0) {


printf("Read Error!\n");
exit(0);
}
else {
printf("READ MESSAGE: %s\n", buff);
printf("%s\n", buff);
printf("Task Performed\n");
}
close(sockfd);
return 0;
}

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.

Finding and learning:

• 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

You might also like