c – How to exchange information in full duplex mode?

Note the two codes below:

client.c:

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

#define SRV_ADDR "127.0.0.1"
#define SRV_PORT "9009"

int make_socket (void) {

int ecode, sockfd;

struct addrinfo * results = NULL, hints;

memset (& tips, 0, sizeof (struct addrinfo));

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

if ((ecode = getaddrinfo (SRV_ADDR, SRV_PORT, & tips, & results)))! = 0) {

sockfd = -1;

} else {

struct addrinfo * it = NULL;

for (it = results; it! = NULL; it = it-> ai_next) {

if (sockfd = socket (it-> ai_family, it-> ai_socktype, it-> ai_protocol)) == - 1) {

Carry on
}

if (connect (sockfd, it-> ai_addr, it-> ai_addrlen) == 0) {

pause
}

close (sockfd);
}

freeaddrinfo (results);

if (it == NULL) {

sockfd = -1;
}
}

return sockfd;
}

get_msg short (char * msg, size_t n) {

int v;

if (fgets (msg, n, stdin)! = NULL) {

size_t len ​​= strlen (msg);

msg[len-1]= & # 39;  0 & # 39 ;; // delete the  n

rv = 0;

} else {

rv = -1;
}

back goes;
}

int main (void) {

int sockfd = make_socket ();

if (sockfd! = - 1) {

go short;
msg char[101];

printf (" n");

make {

make {

printf ("You>");

if ((rv = get_msg (msg, 101)) !! = 0) {

printf (" n * Can not read!");
printf (" n | .__ Rewrite the message.  n  n");
}

send (sockfd, msg, 101, 0);

} while (rv! = 0);

if (strcmp (msg, "! exit")! = 0) {

rec (sockfd, msg, 101.0);

printf ("Strange:% s  n", msg);
}

} while (strcmp (msg, "! exit")! = 0);

close (sockfd);
}

returns 0;
}

server.c:

#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 

int make_socket (void) {

int sockfd;
struct addrinfo * results = NULL, hints;

memset (& tips, 0, sizeof (struct addrinfo));

hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

if (getaddrinfo (NULL, "9009", & tips, & results)! = 0) {

sockfd = -1;

} else {

struct addrinfo * it = NULL;

for (it = results; it! = NULL; it = it-> ai_next) {

if (sockfd = socket (it-> ai_family, it-> ai_socktype, it-> ai_protocol)) == - 1) {

Carry on
}

if (bind (sockfd, it-> ai_addr, it-> ai_addrlen) == 0) {

pause
}

close (sockfd);
}

freeaddrinfo (results);

if (it == NULL) {

sockfd = -1;

} else {

if (listen (sockfd, 1) == - 1) {

sockfd = -1;
}
}
}

return sockfd;
}

get_msg short (char * msg, size_t n) {

int v;

if (fgets (msg, n, stdin)! = NULL) {

size_t len ​​= strlen (msg);

msg[len-1]= & # 39;  0 & # 39 ;; // delete the  n

rv = 0;

} else {

rv = -1;
}

back goes;
}

int main (void) {

int sockfd, csockfd;

if ((sockfd = make_socket ())! = - 1) {

if ((csockfd = accept (sockfd, NULL, NULL)) = 1) {

go short;
msg char[101];

printf (" n");

make {

rec (csockfd, msg, 101.0);

printf ("Strange:% s  n", msg);

if (strcmp (msg, "! exit")! = 0) {

make {

printf ("You>");

if ((rv = get_msg (msg, 101)) !! = 0) {

printf (" n * Can not read!");
printf (" n | .__ Rewrite the message.  n  n");
}

send (csockfd, msg, 101.0);

} while (rv! = 0);
}

} while (strcmp (msg, "! exit")! = 0);

close (csockfd);
}

close (sockfd);
}

returns 0;
}

Notice now the execution of both:

Customer:

t-002 @ localhost: ~ / Documents / C $ gcc -Wall client.c -o client
t-002 @ localhost: ~ / Documents / C $ ./client

You> hello
Strange: I can not speak now :(
You> Okay, my friend :(
Bizarre :! Exit

Server:

t-002 @ localhost: ~ / Documents / C $ gcc -Wall server.c -o server
t-002 @ localhost: ~ / Documents / C $ ./server

Strange: hello
You> I can not speak now :(
Odd: agree, my friend :(
You>! Exit

Did you notice that the communication between the server and the client was double, but not simulated? Well, this form of data exchange is called half-duplex. A more practical example is the walkie-talkies:

walkie-talkies

Here we observe the following: Whereas a walkie-talkies the other can only hear, that is to say that the transmission of data is done according to time. It's almost the same as my server and my client up there, but it was pre-established there who spoke first and it was necessary to wait for the other one to answer for able to answer.

So,, I come with the following doubt: It is possible to establish a connection between a client and a server written / programmed in C where the data is exchanged in a simulation double or bidirectional, that is to say in mode. full duplex?