Question

In: Computer Science

Using C as the programming language, Write a concurrent connection-oriented server that can do something simple...

Using C as the programming language,

Write a concurrent connection-oriented server that can do something simple for connected clients. It should be able to carry out such processing for the client as many times as the client wants until the client indicates it wishes to end the session.

The server should support multiple clients (use telnet as the client in this task). Compile and run the server program.

Try and connect to it from multiple other hosts using telnet as the client program. Use netstat and ps to examine what is going on from separate observation windows on the hosts involved.

Use a distinct and visible message termination character in the server. The ampersand (&) would be fine. Also, the server should use just one buffer for reading.

Thank you... :)

Solutions

Expert Solution

Solution Using C programing Language

ServerSide Code

/**** iserver.c ****/

#include <stdio.h>

#include <stdlib.h>

#include <sys/socket.h>

#include <netinet/in.h>

#define SERVER_PORT 12345

/* Run with a number of incoming connection as argument */

int main(int argc, char *argv[])

{

int i, len, num, rc;

int listen_sd, accept_sd;

/* Buffer for data */

char buffer[100];

struct sockaddr_in addr;

/* If an argument was specified, use it to */

/* control the number of incoming connections */

if(argc >= 2)

num = atoi(argv[1]);

/* Prompt some message */

else

{

printf("Usage: %s <The_number_of_client_connection else 1 will be used>\n", argv[0]);

num = 1;

}

/* Create an AF_INET stream socket to receive */

/* incoming connections on */

listen_sd = socket(AF_INET, SOCK_STREAM, 0);

if(listen_sd < 0)

{

perror("Iserver - socket() error");

exit(-1);

}

else

printf("Iserver - socket() is OK\n");

printf("Binding the socket...\n");

/* Bind the socket */

memset(&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;

addr.sin_addr.s_addr = htonl(INADDR_ANY);

addr.sin_port = htons(SERVER_PORT);

rc = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr));

if(rc < 0)

{

perror("Iserver - bind() error");

close(listen_sd);

exit(-1);

}

else

printf("Iserver - bind() is OK\n");

/* Set the listen backlog */

rc = listen(listen_sd, 5);

if(rc < 0)

{

perror("Iserver - listen() error");

close(listen_sd);

exit(-1);

}

else

printf("Iserver - listen() is OK\n");

/* Inform the user that the server is ready */

printf("The Iserver is ready!\n");

/* Go through the loop once for each connection */

for(i=0; i < num; i++)

{

/* Wait for an incoming connection */

printf("Iteration: #%d\n", i+1);

printf(" waiting on accept()\n");

accept_sd = accept(listen_sd, NULL, NULL);

if(accept_sd < 0)

{

perror("Iserver - accept() error");

close(listen_sd);

exit(-1);

}

else

printf("accept() is OK and completed successfully!\n");

/* Receive a message from the client */

printf("I am waiting client(s) to send message(s) to me...\n");

rc = recv(accept_sd, buffer, sizeof(buffer), 0);

if(rc <= 0)

{

perror("Iserver - recv() error");

close(listen_sd);

close(accept_sd);

exit(-1);

}

else

printf("The message from client: \"%s\"\n", buffer);

/* Echo the data back to the client */

printf("Echoing it back to client...\n");

len = rc;

rc = send(accept_sd, buffer, len, 0);

if(rc <= 0)

{

perror("Iserver - send() error");

close(listen_sd);

close(accept_sd);

exit(-1);

}

else

printf("Iserver - send() is OK.\n");

/* Close the incoming connection */

close(accept_sd);

}

/* Close the listen socket */

close(listen_sd);

return 0;

}

  • Compile and link.

$ gcc -g iserver.c -o iserver

  • Run the server program.

$ ./iserver

Usage: ./iserver <The_number_of_client_connection else 1 will be used>

Iserver - socket() is OK

Binding the socket...

Iserver - bind() is OK

Iserver - listen() is OK

The Iserver is ready!

Iteration: #1

waiting on accept()

Client Side Coding:

/****** comclient.c ******/

#include <stdio.h>

#include <stdlib.h>

#include <sys/socket.h>

#include <netinet/in.h>

/* Our server port as in the previous program */

#define SERVER_PORT 12345

main (int argc, char *argv[])

{

int len, rc;

int sockfd;

char send_buf[100];

char recv_buf[100];

struct sockaddr_in addr;

if(argc !=2)

{

printf("Usage: %s <Server_name or Server_IP_address>\n", argv[0]);

exit (-1);

}

/* Create an AF_INET stream socket */

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0)

{

perror("client - socket() error");

exit(-1);

}

else

printf("client - socket() is OK.\n");

/* Initialize the socket address structure */

memset(&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;

addr.sin_addr.s_addr = htonl(INADDR_ANY);

addr.sin_port = htons(SERVER_PORT);

/* Connect to the server */

rc = connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));

if(rc < 0)

{

perror("client - connect() error");

close(sockfd);

exit(-1);

}

else

{

printf("client - connect() is OK.\n");

printf("connect() completed successfully.\n");

printf("Connection with %s using port %d established!\n", argv[1], SERVER_PORT);

}

/* Enter data buffer that is to be sent */

printf("Enter message to be sent to server:\n");

gets(send_buf);

/* Send data buffer to the worker job */

len = send(sockfd, send_buf, strlen(send_buf) + 1, 0);

if(len != strlen(send_buf) + 1)

{

perror("client - send() error");

close(sockfd);

exit(-1);

}

else

printf("client - send() is OK.\n");

printf("%d bytes sent.\n", len);

/* Receive data buffer from the worker job */

len = recv(sockfd, recv_buf, sizeof(recv_buf), 0);

if(len != strlen(send_buf) + 1)

{

perror("client - recv() error");

close(sockfd);

exit(-1);

}

else

{

printf("client - recv() is OK.\n");

printf("The sent message: \"%s\" successfully received by server and echoed back to client!\n", recv_buf);

printf("%d bytes received.\n", len);

}

/* Close the socket */

close(sockfd);

return 0;

}

Compile and link::$ gcc -g comclient.c -o comclient

if you get compilation error change the gets() to the secure version, gets_s(). Run the program and make sure you run the server program as in the previous program example.

$ ./comclient

$ ./comclient bakawali


Related Solutions

Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class...
Kindly Do the program in C++ language Object Oriented Programming. Objectives  Implement a simple class with public and private members and multiple constructors.  Gain a better understanding of the building and using of classes and objects.  Practice problem solving using OOP. Overview You will implement a date and day of week calculator for the SELECTED calendar year. The calculator repeatedly reads in three numbers from the standard input that are interpreted as month, day of month, days...
Write a simple Calculator program using Scheme programming language. It should be able to do following...
Write a simple Calculator program using Scheme programming language. It should be able to do following operations: "+", "-", " * *, "/". the format when using the calculator function should be (calculator(operand1 operation operand2)) -> (calculator(2 + 5)) should give the output of 7.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an...
Make a simple game using C++ which implements all about Object Oriented Programming (Please make an explanation which of each part in it)
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
Write a DFA simulator using the C++ programming language. Please refer to the following UDACITY website...
Write a DFA simulator using the C++ programming language. Please refer to the following UDACITY website if you do knot know C++. This is a free online tutorial for learning C++. Read your DFA from a textfile. First line contains a list of the final states (as integers), separated by a space. Rest of file contains the transitions in form: startstate, blank, symbol read, blank, tostate Prompt user for name of file. From there, program prompts the user for strings...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT