You need to write 2 separate C programs, a client & server. The client makes a c
ID: 3839708 • Letter: Y
Question
You need to write 2 separate C programs, a client & server. The client makes a connection to the server and transmits a series of numbers. The server listens for client communication and when it has received the series of numbers, it adds them together and closes.
Program requirements:
1. The client needs to know the server IP address and port number. Determine these from the server and make sure if you run the server, followed by the client that they can communicate. If you run both on the same server, any port above 1024 is valid.
2. Client code: prompt the user for how many numbers to add together, then prompt them for each number. Conversion from string to integer must be done on the client.
3. Server code: Display each integer immediately after it is entered on the client. When the client is nished sending numbers, the server code should add the numbers together, display the total, and then shutdown. (Hint: If you establish a new connection on the server for every number transmitted, the server may need a pipe to communicate numbers from the child processes to the parent process. If you transmit all of the numbers in a single connection, you probably will not need to do this.)
4. Close all sockets on the server and client when nished. Make sure all child processes are closed.
$ ./client localhost 7500
How many numbers should I add together? 2
Enter a number: 99
Enter a number: 45
$ ./server 7500
Received number 1: 99
Received number 2: 45
Here are the numbers added together: 144
Explanation / Answer
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>
#include <pthread.h>
using namespace std;
void *task1(void *);
static int connFd;
int main(int argc, char* argv[])
{
int pId, portNo, listenFd;
socklen_t len; //store size of the address
bool loop = false;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadA[3];
if (argc < 2)
{
cerr << "Syntam : ./server <port>" << endl;
return 0;
}
portNo = atoi(argv[1]);
if((portNo > 65535) || (portNo < 2000))
{
cerr << "Please enter a port number between 2000 - 65535" << endl;
return 0;
}
//create socket
listenFd = socket(AF_INET, SOCK_STREAM, 0);
if(listenFd < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
bzero((char*) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNo);
if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}
listen(listenFd, 5);
len = sizeof(clntAdd);
int noThread = 0;
while (noThread < 3)
{
cout << "Listening" << endl;
connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
if (connFd < 0)
{
cerr << "Cannot accept connection" << endl;
return 0;
}
else
{
cout << "Connection successful" << endl;
}
pthread_create(&threadA[noThread], NULL, task1, NULL);
noThread++;
}
for(int i = 0; i < 3; i++)
{
pthread_join(threadA[i], NULL);
}
}
void *task1 (void *dummyPt)
{
cout << "Thread No: " << pthread_self() << endl;
char test[200];
bzero(test, 201);
bool loop = false;
while(!loop)
{
bzero(test, 201);
read(connFd, test,200);
string tester (test);
cout << tester << endl;
if(tester == "exit")
break;
}
cout << " Closing thread and conn" << endl;
close(connFd);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.