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

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

#include <netdb.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_half_duplex_client.c:

Macros

#define PORT   8100
 For structures returned by the network database library - formatted internet addresses and port numbers 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 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 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
46{
47 perror("Socket Creation Failed");
48 exit(EXIT_FAILURE);
49}

◆ 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

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