in Linux Write two C programs (client and server) for number guessing, as descri
ID: 3768733 • Letter: I
Question
in Linux Write two C programs (client and server) for number guessing, as described below.
1. Server makes named pipe.
2. Server waits for client.
3. Client writes to server’s pipe with message made with its process id.
4. When server reads from client, it folks new process. Child process starts step 5, and parent process
goes to step 2.
5. Server makes random number1 (0~99).
6. Server makes named pipes for two-way communication.
7. Client receives number2 (0~99) from user.
8. Client writes number2 to server.
9. Server reads number 2 from client. Then it examines followings:
A. If number2 < random number1, server writes “number is less than random number”.
Client reads and prints this message, and go to step 6.
B. If number2 > random number1, server writes “number is greater than random
number”. Client reads and prints this message, and go to step 6.
C. If number2 == random number1, server writes “number is correct in X times”, and
go to step 10 (X is trial number).
10. Client reads “number is correct in X times” message from server, prints it, and exit.
11. If user inputs ctrl-c, server process closes and deletes named pipe then exits.
When running programs, you may run a server in either in foreground or background process.
You should run more than two clients in multiple terminals at the same time.
Explanation / Answer
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
/// CLIENT
int main(int argc, char *argv[])
{
printf("This is the client program ");
int sockfd;
int len, rc ;
struct sockaddr_in address;
int result;
char ch = 'A';
//Create socket for client.
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Socket create failed. ") ;
return -1 ;
}
//Name the socket as agreed with server.
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(7734);
len = sizeof(address);
result = connect(sockfd, (struct sockaddr *)&address, len);
if(result == -1)
{
perror("Error has occurred");
exit(-1);
}
while ( ch < 'Y') {
//Read and write via sockfd
rc = write(sockfd, &ch, 1);
printf("write rc = %d ", rc ) ;
if (rc == -1) break ;
read(sockfd, &ch, 1);
printf("Char from server = %c ", ch);
//if (ch == 'A') sleep(5) ; // pause 5 seconds
}
close(sockfd);
exit(0);
}
SERVER
Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
/// SERVER
int main(int argc, char *argv[])
{
//Declaring process variables.
int server_sockfd, client_sockfd;
int server_len ;
int rc ;
unsigned client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//Remove any old socket and create an unnamed socket for the server.
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htons(INADDR_ANY);
server_address.sin_port = htons(7734) ;
server_len = sizeof(server_address);
rc = bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
printf("RC from bind = %d ", rc ) ;
//Create a connection queue and wait for clients
rc = listen(server_sockfd, 5);
printf("RC from listen = %d ", rc ) ;
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
printf("after accept()... client_sockfd = %d ", client_sockfd) ;
while(1)
{
char ch;
printf("server waiting ");
//Accept a connection
//client_len = sizeof(client_address);
//client_sockfd = accept(server_sockfd, (struct sockaddr *) &client_address, &client_len);
//printf("after accept()... client_sockfd = %d ", client_sockfd) ;
//Read write to client on client_sockfd
rc = read(client_sockfd, &ch, 1);
printf("RC from read = %d ", rc ) ;
if (ch=='X') break ;
ch++;
write(client_sockfd, &ch, 1);
}
printf("server exiting ");
//close(client_sockfd);
close(client_sockfd);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.