Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Server-side implementation of TCP Full Duplex Communication More...
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
Macros | |
#define | PORT 10000 |
For the type in_addr_t and in_port_t For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures. More... | |
Functions | |
void | error () |
Utility function used to print an error message to stderr . More... | |
int | main () |
Main function. More... | |
Server-side implementation of TCP Full Duplex Communication
The algorithm is based on the simple TCP client and server model. However, instead of the server only sending and the client only receiving data, The server and client can both send and receive data simultaneously. This is implemented by using the fork
function call so that in the server the child process can receive data and parent process can send data, and in the client the child process can send data and the parent process can receive data. It runs an infinite loop and can send and receive messages indefinitely until the user exits the loop. In this way, the Full Duplex Form of communication can be represented using the TCP server-client model & socket programming
#define PORT 10000 |
For the type in_addr_t and in_port_t For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures.
For specific bit size values of variables Variable types, several macros, and various functions for performing input and output Variable types, several macros, and various functions for performing general functions Various functions for manipulating arrays of characters For macro definitions related to the creation of sockets For definitions to allow for the porting of BSD programs For miscellaneous symbolic constants and types, and miscellaneous functions
void error | ( | ) |
Utility function used to print an error message to stderr
.
It prints str
and an implementation-defined error message corresponding to the global variable errno
.
int main | ( | void | ) |
Main function.
Variable Declarations
< socket descriptors - Like file handles but for sockets
< character arrays to read and store string data for communication
< basic structures for all syscalls and functions that deal with internet addresses. Structures for handling internet addresses
size of address
The TCP socket is created using the socket function
AF_INET (Family) - it is an address family that is used to designate the type of addresses that your socket can communicate with
SOCK_STREAM (Type) - Indicates TCP Connection - A stream socket provides for the bidirectional, reliable, sequenced, and unduplicated flow of data without record boundaries. Aside from the bidirectionality of data flow, a pair of connected stream sockets provides an interface nearly identical to pipes
0 (Protocol) - Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type
< Error if the socket descriptor has a value lower than 0 - socket wasnt created
Server Address Information
The bzero() function erases the data in the n bytes of the memory starting at the location pointed to, by writing zeros (bytes containing '\0') to that area
We bind the server_addr to the internet address and port number thus giving our socket an identity with an address and port where it can listen for connections
htons - The htons() function translates a short integer from host byte order to network byte order
htonl - The htonl() function translates a long integer from host byte order to network byte order
These functions are necessary so that the binding of address and port takes place with data in the correct format
This binds the socket descriptor to the server thus enabling the server to listen for connections and communicate with other clients
If binding is unsuccessful
This is to listen for clients or connections made to the server
The limit is currently at 5 but can be increased to listen for more connections
It listens to connections through the socket descriptor
When a connection is found, a socket is created and connection is accepted and established through the socket descriptor
Communication between client and server
The bzero() function erases the data in the n bytes of the memory starting at the location pointed to, by writing zeros (bytes containing '\0') to that area. The variables are emptied and then ready for use
The fork function call is used to create a child and parent process which run and execute code simultaneously
The child process is used to receive data and after doing so sleeps for 5 seconds to wait for the parent to send data
The parent process is used to send data and after doing so sleeps for 5 seconds to wait for the child to receive data
The server and client can communicate indefinitely till one of them exits the connection
Since the exchange of information between the server and client takes place simultaneously this represents FULL DUPLEX COMMUNICATION
Value of 0 is for child process
Parent process
Close socket