This is a simple TCP client-server application implemented in C.
Before compiling the files, ensure that you have gcc
installed.
To compile the server and client file, use the following command:
gcc server.c -o server
gcc client.c -o client
Make sure to follow the correct order of running the programs, starting with the server and then the client.
- First, run the server program by executing the following command in the terminal:
./server
- Then, run the client program by executing the following command in another terminal window:
./client
Demo.mp4
-
<netinet/in.h>
- Contains constants and structures needed for internet domain addresses, such as
struct sockaddr_in
.
- Contains constants and structures needed for internet domain addresses, such as
-
<stdio.h>
- Provides functions for input and output, such as
printf
,scanf
,fgets
, andperror
.
- Provides functions for input and output, such as
-
<stdlib.h>
- Provides functions for memory allocation, process control, conversions, and others, such as
exit
.
- Provides functions for memory allocation, process control, conversions, and others, such as
-
<sys/types.h>
- Defines data types used in system calls, such as
socket
,bind
, andaccept
.
- Defines data types used in system calls, such as
-
<sys/socket.h>
- Contains definitions of structures needed for sockets, and declarations of functions like
socket
,bind
,listen
,accept
, andconnect
.
- Contains definitions of structures needed for sockets, and declarations of functions like
-
<string.h>
- Provides functions for manipulating arrays of characters, such as
strlen
,strcmp
,strncpy
, andbzero
.
- Provides functions for manipulating arrays of characters, such as
-
<unistd.h>
- Declares miscellaneous functions like
read
,write
, andclose
used for low-level I/O operations.
- Declares miscellaneous functions like
- Parameters:
domain
: Communication domain, e.g.,AF_INET
for IPv4 ,AF_INET6
for IPv6.type
: Communication type, e.g.,SOCK_STREAM
for TCP ,SOCK_DGRAM
for UDP.protocol
: Protocol to be used, usually0
to select the default protocol.
- Returns:
- File descriptor for the new socket, or
-1
if an error occurred.
- File descriptor for the new socket, or
- Parameters:
sockfd
: File descriptor of the socket to bind.addr
: Pointer tostruct sockaddr
containing the address to bind to.addrlen
: Size (in bytes) of the address structure that addr points to.
- Returns:
0
on success, or-1
if an error occurred.
- Parameters:
sockfd
: File descriptor of the socket to listen on.backlog
: Number of connections allowed on the incoming queue.
- Returns:
0
on success, or-1
if an error occurred.
- Parameters:
sockfd
: File descriptor of the socket to accept connections on.addr
: Pointer tostruct sockaddr
to store the address of the connecting entity.addrlen
: Pointer to a variable that stores the size of the address structure.
- Returns:
- File descriptor through which further communication takes place, or
-1
if an error occurred.
- File descriptor through which further communication takes place, or
- Parameters:
sockfd
: File descriptor of the socket to connect.addr
: Pointer tostruct sockaddr
containing the address to connect to.addrlen
: Size (in bytes) of the address structure that addr points to.
- Returns:
0
on success, or-1
if an error occurred.
- Parameters:
fd
: File descriptor to read from.buf
: Buffer to store the read data.count
: Number of bytes to read.
- Returns:
- Number of bytes read, or
-1
if an error occurred.
- Number of bytes read, or
- Parameters:
fd
: File descriptor to write to.buf
: Buffer containing the data to write.count
: Number of bytes to write.
- Returns:
- Number of bytes written, or
-1
if an error occurred.
- Number of bytes written, or
- Parameters:
fd
: File descriptor to close.
- Returns:
0
on success, or-1
if an error occurred.
- Parameters:
s
: Custom message to print before the system error message.
- Returns:
- Nothing. Prints an error message to
stderr
.
- Nothing. Prints an error message to
- Parameters:
status
: Exit status.EXIT_SUCCESS
orEXIT_FAILURE
.
- Returns:
- Nothing. Terminates the program.
- Parameters:
str
: Pointer to a buffer where the string is stored.n
: Maximum number of characters to read.stream
: File stream to read from.
- Returns:
- Pointer to the string read, or
NULL
if an error occurred.
- Pointer to the string read, or
- Parameters:
s
: Pointer to the memory area to be filled with zeroes.n
: Number of bytes to set to zero.
- Returns:
- Nothing. Sets the memory area to zero.