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

Server-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_server.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

Server-side implementation of TCP Half Duplex Communication

Author
NVombat
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

< character arrays to read and store string data for communication

< asic 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

< 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

Domain/Family to be used

Port to be used

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

First the SERVER is prompted to type a message which is read from stdin and then sent over the connection that was established - the socket

  • to be received by the client (send())

The SERVER then waits for the client to reply. It then receives the reply in the string variable and displays it (recv())

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

Send message

Receive Message

Close socket

56{
57 /** Variable Declarations */
58 uint32_t sockfd,
59 conn; ///< socket descriptors - Like file handles but for sockets
60 char server_msg[10000],
61 client_msg[10000]; ///< character arrays to read and store string data
62 /// for communication
63 struct sockaddr_in server_addr,
64 client_addr; ///< asic structures for all syscalls and functions that
65 /// deal with internet addresses. Structures for handling
66 /// internet addresses
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(); ///< Error if the socket descriptor has a value lower than 0 -
87 /// socket wasnt created
88 }
89
90 /**
91 * Server Address Information
92 *
93 * The bzero() function erases the data in the n bytes of the memory
94 * starting at the location pointed to, by writing zeros (bytes
95 * containing '\0') to that area
96 *
97 * We bind the server_addr to the internet address and port number thus
98 * giving our socket an identity with an address and port where it can
99 * listen for connections
100 *
101 * htons - The htons() function translates a short integer from host byte
102 * order to network byte order
103 *
104 * htonl - The htonl() function translates a long integer from host byte
105 * order to network byte order
106 *
107 * These functions are necessary so that the binding of address and port
108 * takes place with data in the correct format
109 */
110 bzero(&server_addr, sizeof(server_addr));
111 server_addr.sin_family = AF_INET; /// Domain/Family to be used
112 server_addr.sin_port = htons(PORT); /// Port to be used
113 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
114
115 printf("Server is running...\n");
116
117 /**
118 * This binds the socket descriptor to the server thus enabling the server
119 * to listen for connections and communicate with other clients
120 */
121 if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
122 {
123 error(); /// If binding is unsuccessful
124 }
125
126 /**
127 * This is to listen for clients or connections made to the server
128 *
129 * The limit is currently at 5 but can be increased to listen for
130 * more connections
131 *
132 * It listens to connections through the socket descriptor
133 */
134 listen(sockfd, 5);
135
136 printf("Server is listening...\n");
137
138 /**
139 * When a connection is found, a socket is created and connection is
140 * accepted and established through the socket descriptor
141 */
142 conn = accept(sockfd, (struct sockaddr *)NULL, NULL);
143
144 printf("Server is connected...\n");
145
146 /**
147 * Communication between client and server
148 *
149 * The bzero() function erases the data in the n bytes of the memory
150 * starting at the location pointed to, by writing zeros (bytes
151 * containing '\0') to that area. The variables are emptied and then
152 * ready for use
153 *
154 * First the SERVER is prompted to type a message which is read from
155 * stdin and then sent over the connection that was established - the socket
156 * - to be received by the client (send())
157 *
158 * The SERVER then waits for the client to reply. It then receives the reply
159 * in the string variable and displays it (recv())
160 *
161 * The server and client can communicate till one of them exits the
162 * connection
163 *
164 * Since the exchange of information between the server and client take
165 * place one at a time this represents HALF DUPLEX COMMUNICATION
166 */
167 while (1)
168 {
169 bzero(&server_msg, sizeof(server_msg));
170 bzero(&client_msg, sizeof(client_msg));
171
172 /// Send message
173 printf("\nEnter message here: ");
174 fgets(server_msg, 10000, stdin);
175 send(conn, server_msg, strlen(server_msg) + 1, 0);
176
177 /// Receive Message
178 recv(conn, client_msg, sizeof(client_msg), 0);
179 printf("\nClient Message: %s\n", client_msg);
180 }
181
182 /// Close socket
183 close(sockfd);
184 printf("Server is offline...\n");
185 return 0;
186}
#define PORT
For structures returned by the network database library - formatted internet addresses and port numbe...
Definition tcp_half_duplex_server.c:37
void error()
Utility function used to print an error message to stderr.
Definition tcp_half_duplex_server.c:45
Here is the call graph for this function: