Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
tcp_full_duplex_client.c File Reference

Client-side implementation of TCP Full Duplex Communication More...

#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for tcp_full_duplex_client.c:

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 For macro definitions related to the creation of sockets For definitions to allow for the porting of BSD programs.
 

Functions

void error ()
 Utility function used to print an error message to stderr.
 
int main ()
 Main function.
 

Detailed Description

Client-side implementation of TCP Full Duplex Communication

Author
NVombat
See also
tcp_full_duplex_server.c

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

Macro Definition Documentation

◆ PORT

#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 macro definitions related to the creation of sockets For definitions to allow for the porting of BSD programs.

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

Function Documentation

◆ error()

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.

Returns
void
53{
54 perror("Socket Creation Failed");
55 exit(EXIT_FAILURE);
56}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit

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

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.

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

Connects the client to the server address using the socket descriptor This enables the two to communicate and exchange data

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 send data and after doing so sleeps for 5 seconds to wait for the parent to receive data

The parent process is used to receive data and after doing so sleeps for 5 seconds to wait for the child to send 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

63{
64 /** Variable Declarations */
65 uint32_t
66 sockfd; ///< socket descriptors - Like file handles but for sockets
67 char sendbuff[1024],
68 recvbuff[1024]; ///< character arrays to read and store string data
69 /// for communication
70
71 struct sockaddr_in
72 server_addr; ///< basic structures for all syscalls and functions that
73 /// deal with internet addresses. Structures for handling
74 /// internet addresses
75
76 /**
77 * The TCP socket is created using the socket function.
78 *
79 * AF_INET (Family) - it is an address family that is used to designate the
80 * type of addresses that your socket can communicate with
81 *
82 * SOCK_STREAM (Type) - Indicates TCP Connection - A stream socket provides
83 * for the bidirectional, reliable, sequenced, and unduplicated flow of data
84 * without record boundaries. Aside from the bidirectionality of data flow,
85 * a pair of connected stream sockets provides an interface nearly identical
86 * to pipes.
87 *
88 * 0 (Protocol) - Specifies a particular protocol to be used with the
89 * socket. Specifying a protocol of 0 causes socket() to use an unspecified
90 * default protocol appropriate for the requested socket type.
91 */
92 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
93 {
94 error();
95 }
96
97 /**
98 * Server Address Information
99 *
100 * The bzero() function erases the data in the n bytes of the memory
101 * starting at the location pointed to, by writing zeros (bytes
102 * containing '\0') to that area.
103 *
104 * We bind the server_addr to the internet address and port number thus
105 * giving our socket an identity with an address and port where it can
106 * listen for connections
107 *
108 * htons - The htons() function translates a short integer from host byte
109 * order to network byte order
110 *
111 * htonl - The htonl() function translates a long integer from host byte
112 * order to network byte order
113 *
114 * These functions are necessary so that the binding of address and port
115 * takes place with data in the correct format
116 */
117 bzero(&server_addr, sizeof(server_addr));
118 server_addr.sin_family = AF_INET;
119 server_addr.sin_port = htons(PORT);
120 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
121
122 printf("Client is running...\n");
123
124 /**
125 * Connects the client to the server address using the socket descriptor
126 * This enables the two to communicate and exchange data
127 */
128 connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
129
130 printf("Client is connected...\n");
131
132 /**
133 * Communication between client and server
134 *
135 * The bzero() function erases the data in the n bytes of the memory
136 * starting at the location pointed to, by writing zeros (bytes
137 * containing '\0') to that area. The variables are emptied and then
138 * ready for use
139 *
140 * The fork function call is used to create a child and parent process
141 * which run and execute code simultaneously
142 *
143 * The child process is used to send data and after doing so
144 * sleeps for 5 seconds to wait for the parent to receive data
145 *
146 * The parent process is used to receive data and after doing so
147 * sleeps for 5 seconds to wait for the child to send data
148 *
149 * The server and client can communicate indefinitely till one of them
150 * exits the connection
151 *
152 * Since the exchange of information between the server and client takes
153 * place simultaneously this represents FULL DUPLEX COMMUNICATION
154 */
155 pid_t pid;
156 pid = fork();
157
158 if (pid == 0) /// Value of 0 is for child process
159 {
160 while (1)
161 {
162 bzero(&sendbuff, sizeof(sendbuff));
163 printf("\nType message here: ");
164 fgets(sendbuff, 1024, stdin);
165 send(sockfd, sendbuff, strlen(sendbuff) + 1, 0);
166 printf("\nMessage sent!\n");
167 sleep(5);
168 // break;
169 }
170 }
171 else /// Parent Process
172 {
173 while (1)
174 {
175 bzero(&recvbuff, sizeof(recvbuff));
176 recv(sockfd, recvbuff, sizeof(recvbuff), 0);
177 printf("\nSERVER: %s\n", recvbuff);
178 sleep(5);
179 // break;
180 }
181 }
182
183 /// Close Socket
184 close(sockfd);
185 printf("Client is offline...\n");
186 return 0;
187}
PID Controller.
Definition pid.c:31
#define PORT
For the type in_addr_t and in_port_t For structures returned by the network database library - format...
Definition tcp_full_duplex_client.c:44
void error()
Utility function used to print an error message to stderr.
Definition tcp_full_duplex_client.c:52
Here is the call graph for this function: