1 1716971034448
1 1716971034448
Utkarsha Subedi
Department of BSc. (Hons.) Ethical, Softwarica College of IT and E-
Commerce
Coventry University
ST4061CEM Programming and Algorithms 1
Er. Suman Shrestha
June 28, 2023
1
ACKNOWLEDGEMENT
I would like to express my heartfelt gratitude towards Er. Suman Shrestha for assigning
me a C Programming project during my First Semester. I learnt a lot of valuable things and learnt
a little more on the huge voyage one can go in C Programming. Working on this project has
made me realize the practical implications of C Programming. I learnt about new things such as
sockets, socket family, error handling and so on. Once again, I extend my gratitude for all the
guidance and mentorship he provided for all the duration of the project.
2
ABSTRACT
If we establish a connection between the client and the server using TCP, then this
protocol possesses a number of useful capabilities. For example, it is well suited for applications
that demand a high level of reliability, and it places a lower priority on transmission speed. It is a
protocol that is utilized by others, such as HTTP, HTTPs, FTP, SMTP, and Telnet. Data packets
are rearranged by TCP in the order that is given. There is a 100% assurance that the data being
transferred will arrive uncorrupted and in the same order in which it was sent. TCP implements
Flow Control and calls for the sending of three packets to establish a socket connection before it
will allow any user data to be transmitted. Both dependability and congestion control are handled
by TCP. In addition to that, it is capable of error checking and error recovery. Incorrect data
packets are resent from their point of origin to their final destination. (Shukla 2018)
3
CONTENTS
INTRODUCTION .......................................................................................................................... 5
OBJECTIVES ................................................................................................................................. 6
PROBLEM STATEMENT AND SCOPE ...................................................................................... 6
LITERATURE REVIEW ............................................................................................................... 7
METHODOLOGY ......................................................................................................................... 8
1. Client .................................................................................................................................... 8
2. Server: ................................................................................................................................ 10
Explanation of the Code................................................................................................................ 12
SERVER: .................................................................................................................................. 13
CLIENT ..................................................................................................................................... 17
FUTURE IMPROVEMENTS ...................................................................................................... 24
CONCLUSION ............................................................................................................................. 25
REFERENCE ................................................................................................................................ 26
4
AN ANALYSIS OF TCP CLIENT-SERVER
COMMUNICATION
INTRODUCTION
Transmission Control Protocol (TCP) is a network protocol that allows computers and
their applications to communicate with one another. Its purpose is to transmit data packets over a
network and guarantee their safe arrival at their destination. TCP is an integral part of the
internet protocols defined by the Internet Engineering Task Force (IETF). It is widely employed
in digital network communications and guarantees complete data delivery from beginning to end.
(Anon.a)
5
OBJECTIVES
The sole purpose of this project are as follows:
• Examine the client-server communication process using TCP.
• Analyze the current literature on TCP communication in order to identify common
obstacles and best practices.
6
LITERATURE REVIEW
A lot of things have been done using TCP client server communication including chat
applications, chat bots, file transfer networks and so on. Similarly, a lot of research and stuffs has
also been done on the topic. Richard Stevens has done a lot of research and done his research in
this field by writing different books such as ‘Unix Network Programming’, ‘TCP/IP Illustrated
Volume 1”, etc. Email communication, Web browsing, etc. are a better and deeper look into the
potential that the TCP/IP communication holds. (Anon.b)
The newest technological developments have not abandoned contemporary control systems.
Remote monitoring and maintenance is being used to increase plant uptime and productivity. A
system may be automated remotely using instructions provided through mobile devices, using
the same architecture that is used for remote monitoring of plants and processes. The remote
automation and management of routine household and factory tasks is gaining popularity.
Because of how dependable it is in transporting datagrams, TCP/IP is increasingly being used.
The effectiveness of remote monitoring and control relies on a reliable transmission method.
Today's control systems link to the system they're regulating using a variety of wireless
controllers. These smart controllers can both broadcast and receive instructions from mobile
devices using the Simple Message Service (SMS). In this setup, the SMS server is accessed
remotely through TCP/IP, which transmits the system's control data. The user's data is sent from
their mobile phone to the SMS server through the TCP/IP client software installed on their
computer, and then on to the MySQL data server. By sending SMS instructions to the system
remotely, the user may have the information he needs sent to his phone. (Anon.2020-02-
28T11:52:13+0000)
7
METHODOLOGY
1. CLIENT
Step 1: Start
Step 2: Initialize the variables needed
• ip: character pointer to represent loop back IP
• port: integer that specifies port number
• sock: integer variable for a socket
• addr and add_size: a ‘sockaddr_in’ structure that stores server address information
• buffer: an array to store sent and received data
• n: integer to store the number of bytes received
Step 3: Create a TCP socket named ‘sock’ using ‘scoket()’ function
Step 4: Check if there is any error using ‘if’ function
Step 5: Initialize and specify members and family of server.i.e, addr,add_size
Step 6: Connect to server using ‘connect’ function along with ‘sock’ function
Step 7: Print that it has been connected and be ready for connection
Step 8: Clear the ‘buffer’ by using ‘bzero’ function
Step 9: Copy message into buffer using ‘strcpy()’ function and send data using ‘send’ function
Step 10: Clear the buffer again using the ‘bzero’ functioin
Step 11: Receive the data in buffer using the ‘recv()’ function and copy it to buffer using
‘strcpy()’
Step 12: Print the response i.e. print the ‘buffer’ array
Step 13: Close the socket using close(sock) function
Step 14: Stop
8
ADDING ESSENTIAL HEADERS
CONNECTING TO SERVER
9
2. SERVER:
Step 1: Start
Step 2: Initialize the variables needed
• ip: character pointer to represent loop back IP
• port: integer that specifies port number
• server_addr and client_addr: ‘sockaddr_in’ structure that stores server address
information
• add_size: socklen_t type that stores size of server address structure
• buffer: an array to store sent and received data
Step 3: Create a TCP socket using ‘server_sock’ function.
Step 4: Check if there are any errors while creating socket using ‘if’ function.
Step 5: Initialize and specify members and family for connection i.e. on server_addr and
client_addr.
Step 6: Bind the ip address and port number using “bind” function
Step 7: Check for error client_addr if the binding was successful or not
Step 8: Start listening using “listen” function
Step 9: Enter an infinite “While” loop to handle connections with clients
Step 10: Accept the flow of client using accept function and print it
Step 11: Clear the buffer using “bzero” and receive the data using “recv” function
Step 12: Clear, copy and again send data using bzero(), strcpy() function
Step 13: Close the socket using “close()” function
Step 14: Stop
10
ADDING ESSENTIAL HEADERS
INITILIZING ESSENTIAL
VARIABLES
LISTENING TO CIENT
EXCHANGING THE
MESSAGES
11
EXPLANATION OF THE CODE
As we know that the project has two parts to it i.e., a program for client and a program for
server, the header files we need for both of them remains the same.
We are basically importing all the various libraries needing in this program.
• stdio.h: specifies variables, macros, and input/output functions.
• stdlib.h: declares type conversion, memory allocation, and utility functions.
• string.h: contains string and memory management macros, constants, etc.
• unistd.h: symbolically standard constants and types.
• arpa/inet.h: definitions for using the internet (Anon.c)
12
SERVER:
Here, we dive into the main function. We declared the needed variables:
• ip: character pointer to represent loop back IP i.e., “127.0.0.1”
• port: integer that specifies port number (here, 5566)
• server_addr: a ‘sockaddr_in’ structure that stores server address information
• client_addr: a ‘sockaddr_in’ structure that stores server address information
• add_size: socklen_t type that stores size of server address structure
• buffer: an array to store sent and received data
This is the initialization of the ‘server_sock’ variable which works for the socket.
• server_sock: an integer type variable that stores socket descriptor
13
• Socket: a function used to create socket
• AF_INET: family parameter that represents IPv4
• SOCK_STREAM: represents that the socket will be TCP
• 0: protocol parameter that indicates default protocol i.e., TCP
8
We are checking for errors here. We had created the sock variable and initialized it to be
zero. Now if there was any problem with its initialization and the socket couldn’t be created, it
enters the loop, print the error using perror and exit the program.
14
Here, we are basically using the ‘bind’ function to associate socket with an address and a
port number.
• n: integer variable used to store the result, appropriate for error checking
• server_sock: identifier that representer the specific socket
• (struct sockaddr*) &server_addr: we are casting the value of server_addr to (struct
sockaddr*)
• sizeof(server_addr): find the size to see how many times its done
If the bind is successful, the return value is 0.
So here, we are checking for errors. If any problem had arose while binding the IP and port,
the program would exit with an error. If not and the binding of the IP address and the port is
done successfully, it says its successfully done.
The listen function is telling server_sock to start listening for upcoming connections. The
maximum value is set to be 5.
15
In this while loop that never ends, the server keeps accepting the link and the flow of data
from the client.
• The ‘accept ()’ function is used to accept the incoming connections. It waits until the
client is connected and when it is, it displays that the connection has been established.
• ‘bzero’ function clears the buffer
• The ‘recv ()’ function is used to receive data into the ‘client_sock’. The received data is
stored in buffer and printed.
• The buffer is cleared again. The ‘strcpy ()’ function is a medium to copy the reply from
server to buffer.
• The ‘send ()’ function then sends the reply back to the client. After sending the message,
we have simply closed the socket for client. The client disconnects.
And finally, the program for client ends.
16
CLIENT
Here, we dive into the main function. We declared the needed variables:
• ip: character pointer to represent loop back IP i.e., “127.0.0.1”
• port: integer that specifies port number (here, 5566)
• sock: integer variable for a socket
• addr: a ‘sockaddr_in’ structure that stores Serer address information
• add_size: socklen_t type that stores size of server address structure
• buffer: an array to store sent and received data
• n: integer to store the number of bytes received
This is the initialization of the ‘sock’ variable which works for the socket.
• Sock: an integer type variable that stores socket descriptor.
• Socket: a function used to create socket.
• AF_INET: family parameter that represents IPv4.
• SOCK_STREAM: represents that the socket will be TCP.
• 0: protocol parameter that indicates default protocol i.e., TCP.
17
We are checking for errors here. We had created the sock variable and initialized it to be
zero. Now if there was any problem with its initialization and the socket couldn’t be created, it
enters the loop, print the error using perror and exit the program.
18
The connect function is to establish a connection between the client and server.
• Sock: it represents the client socket that will be used for connection
• (struct sockaddr*) &addr: it basically directs to the server for connection
• Sizeof(addr): for the size of server address
19
• ‘recv ()’ function receives the data saves it to buffer. It is then printed.
• ‘close ()’ function closes the client socket
And finally, the program for client ends.
20
EXECUTION AND OUTPUT
This code was possible to be done in Windows as it missed header files. As we know
Linux operating system is far more flexible and compatible, I did it in Linux. Let’s look into it:
I saved the file with names client.c and server.c in Kali Linux. I complied them using the
command.
gcc -o <new_name> <file_name>
• gcc: invokes GCC compiler
• -o: specifies output filename
• <new_name>: output file name which is executable
• <file_name>: C source file that we want to use
21
After creating the executable file, I first of all ran server:
After that, I compiled the client in another terminal and the following happened:
There was the communication between the client and the server. This is the output of the client.
22
This is the output of the server.
In this way, the networking between TCP client and server was accomplished.
23
FUTURE IMPROVEMENTS
This project is just a mere step into the usage of networking in C. It can be enhanced in an
uncountable way. Firstly, instead of just sharing messages that has been programmed, one could
take input messages and make it work in a full duplex way. Not just that, sharing data, files,
texts, images, videos and many more things could be done too.
• Chat application: Users will be able to have real-time conversations if a few
modifications are made.
• File transfer application: Users can send files to or get files from a server using a file
transfer program.
• Remote Desktop Application: This program allows users to connect to a remote computer
and use it as if they were in front of it.
24
CONCLUSION
In a nut shell, TCP client server communication demonstrated on Linux system is a major
demonstration of the power and versatility of C programming. We not only gained knowledge
about the fundamentals of the networking using C, but also learnt about various protocols,
transmission of data and so on. We also had a greater dive and deeper look into the power of
Linux as well. Not just that, we looked a bit into error handling, data buffering and so on. The
knowledge gained from this project will be handy in tackling more complex challenges in the
world of networking.
25
REFERENCE
1
AnonC Library - [online] available from <https://www.tutorialspoint.com/c_standard_library/stdio_h.htm>[Jun 15,
2023]
2
Shukla, Y. 2018TCP Server-Client Implementation in C. [-09-12T19:25:20+00:00 2018] available from
<https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/>[Jun 19, 2023]
3
Tomar, N. (2021) TCP Client-Server Implementation in C [online] available from <https://idiotdeveloper.com/tcp-
client-server-implementation-in-c/>[Jun 19, 2023]
26