Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
tcp_half_duplex_client.c File Reference

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

#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>
Include dependency graph for tcp_half_duplex_client.c:

Macros

#define PORT   8100
 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...
 

Detailed Description

Client-side implementation of TCP Half Duplex Communication

Author
Nikhill Vombatkere
See also
tcp_half_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 data but only one at a time. This is implemented by using a particular ordering of the send() and recv() functions. When one of the clients or servers is sending, the other can only receive and vice-versa. In this way, the Half Duplex Form of communication can be represented using the TCP server-client model & socket programming

Macro Definition Documentation

◆ PORT

#define PORT   8100

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

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
37{
38 perror("Socket Creation Failed");
39 exit(EXIT_FAILURE);
40}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit

Variable Declarations

< socket descriptors - Like file handles but for sockets

< basic structures for all syscalls and functions that deal with internet addresses. Structures for handling internet addresses

< Character arrays to read and store string data for communication

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

First the CLIENT receives the servers message and displays it (recv())

The CLIENT is then prompted to type in a message and send it to the server. (send())

The server and client can communicate till one of them exits the connection

Since the exchange of information between the server and client take place one at a time this represents HALF DUPLEX COMMUNICATION

Receive Message

Send Message

Close Socket

47{
48 /** Variable Declarations */
49 uint32_t
50 sockfd; ///< socket descriptors - Like file handles but for sockets
51 struct sockaddr_in
52 server_addr; ///< basic structures for all syscalls and functions that
53 /// deal with internet addresses. Structures for handling
54 /// internet addresses
55 char serverResponse[10000],
56 clientResponse[10000]; ///< Character arrays to read and store string
57 /// data for communication
58
59 /**
60 * The TCP socket is created using the socket function.
61 *
62 * AF_INET (Family) - it is an address family that is used to designate the
63 * type of addresses that your socket can communicate with
64 *
65 * SOCK_STREAM (Type) - Indicates TCP Connection - A stream socket provides
66 * for the bidirectional, reliable, sequenced, and unduplicated flow of data
67 * without record boundaries. Aside from the bidirectionality of data flow,
68 * a pair of connected stream sockets provides an interface nearly identical
69 * to pipes.
70 *
71 * 0 (Protocol) - Specifies a particular protocol to be used with the
72 * socket. Specifying a protocol of 0 causes socket() to use an unspecified
73 * default protocol appropriate for the requested socket type.
74 */
75 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
76 {
77 error();
78 }
79
80 /**
81 * Server Address Information
82 *
83 * The bzero() function erases the data in the n bytes of the memory
84 * starting at the location pointed to, by writing zeros (bytes
85 * containing '\0') to that area.
86 *
87 * We bind the server_addr to the internet address and port number thus
88 * giving our socket an identity with an address and port where it can
89 * listen for connections
90 *
91 * htons - The htons() function translates a short integer from host byte
92 * order to network byte order
93 *
94 * htonl - The htonl() function translates a long integer from host byte
95 * order to network byte order
96 *
97 * These functions are necessary so that the binding of address and port
98 * takes place with data in the correct format
99 */
100 bzero(&server_addr, sizeof(server_addr));
101 server_addr.sin_family = AF_INET;
102 server_addr.sin_port = htons(PORT);
103 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
104
105 printf("Client is running...\n");
106
107 /**
108 * Connects the client to the server address using the socket descriptor
109 * This enables the two to communicate and exchange data
110 */
111 connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
112
113 printf("Client is connected...\n");
114
115 /**
116 * Communication between client and server
117 *
118 * The bzero() function erases the data in the n bytes of the memory
119 * starting at the location pointed to, by writing zeros (bytes
120 * containing '\0') to that area. The variables are emptied and then
121 * ready for use
122 *
123 * First the CLIENT receives the servers message and displays it (recv())
124 *
125 * The CLIENT is then prompted to type in a message and send it to the
126 * server. (send())
127 *
128 * The server and client can communicate till one of them exits the
129 * connection
130 *
131 * Since the exchange of information between the server and client take
132 * place one at a time this represents HALF DUPLEX COMMUNICATION
133 */
134 while (1)
135 {
136 bzero(&serverResponse, sizeof(serverResponse));
137 bzero(&clientResponse, sizeof(clientResponse));
138
139 /// Receive Message
140 recv(sockfd, serverResponse, sizeof(serverResponse), 0);
141 printf("\nServer message: %s \n", serverResponse);
142
143 /// Send Message
144 printf("\nEnter message here: ");
145 fgets(clientResponse, 10000, stdin);
146 send(sockfd, clientResponse, strlen(clientResponse) + 1, 0);
147 }
148
149 /// Close Socket
150 close(sockfd);
151 printf("Client is offline...\n");
152 return 0;
153}
#define PORT
For structures returned by the network database library - formatted internet addresses and port numbe...
Definition: tcp_half_duplex_client.c:28
void error()
Utility function used to print an error message to stderr.
Definition: tcp_half_duplex_client.c:36
Here is the call graph for this function: