This example contains the code that enables the worker job to receive a data buffer from the client job and echo it back.
/**************************************************************************/ /* Worker job that receives and echoes back a data buffer to a client */ /**************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <sys/socket.h> main (int argc, char *argv[]) { int rc, len; int sockfd; char buffer[80]; /*************************************************/ /* The descriptor for the incoming connection is */ /* passed to this worker job as a descriptor 0. */ /*************************************************/ sockfd = 0; /*************************************************/ /* Receive a message from the client */ /*************************************************/ printf("Wait for client to send us a message\n"); rc = recv(sockfd, buffer, sizeof(buffer), 0); if (rc <= 0) { perror("recv() failed"); close(sockfd); exit(-1); } printf("<%s>\n", buffer); /*************************************************/ /* Echo the data back to the client */ /*************************************************/ printf("Echo it back\n"); len = rc; rc = send(sockfd, buffer, len, 0); if (rc <= 0) { perror("send() failed"); close(sockfd); exit(-1); } /*************************************************/ /* Close down the incoming connection */ /*************************************************/ close(sockfd); }