hw2sol - CSCI 415 Computer Networks Homework 2 Solution...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSCI 415 Computer Networks Homework 2 Solution Saad Mneimneh Computer Science Hunter College of CUNY Problem 1 Consider the following server and client C++ code that we saw in class: server.c #include <iostream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> using std::cout; using std::cin; const unsigned short int port = 5432; const int max_pending = 10; const int max_len = 256; int main() { sockaddr_in address; //address sockaddr_in client_address; //client address char message[max_len]; int s; int new_s; int len; //build address memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_addr.s_addr = htonl(INADDR_ANY); address.sin_port = htons(port); //setup passive open if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) { cout<<"error in socket"; return 0; } //bind socket to address if (bind(s, (sockaddr *)&address, sizeof(address)) < 0) { cout<<"error in bind"; return 0; } if (listen(s, max_pending) < 0) { cout<<"error in listen"; return 0; } //wait for connection, then receive message socklen_t size = sizeof(sockaddr_in); while (1) { if ((new_s = accept(s, (sockaddr *)&client_address, &size)) < 0) { cout<<"error in accept"; return 0; } while (len = recv(new_s, message, sizeof(message), 0)) { cout<<message<<"\n"; } close(new_s); } } client.c #include <iostream> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> using std::cout; using std::cin; const unsigned short int port = 5432; int main() { int s; sockaddr_in address; //address to connect to memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(port); address.sin_addr.s_addr = htonl(INADDR_ANY); //my IP address //active open if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) { cout<<"error in socket"; return 0; } //connect if (connect(s, (sockaddr *)&address, sizeof(address)) < 0) { cout<<"error in connect"; close(s); return 0; } char message[256]; while(cin.getline(message, 256, \n)) { if (strlen(message) == 0) break; send(s,message,strlen(message)+1,0); } close(s); return 0; } (a) Obtain the code for both the server and the client (you can cut and paste from pdf) and compile it using a C++ compiler (e.g. g++ on Unix). Start one server and one client in separate windows. Start the server rst; otherwise, the client will not be able to connect to the server and will report an error and exit. The client should accept messages from the keyboard (you) and sends them to the server. The server just echoes what you send. To exit the client just type an empty message (hit Enter). (b) While the client is running, start 10 other clients. What happens to these clients? Do their connect()s fail, or time out, or succeed? Do any other calls block? Now let the rst client exit. What happens? Try this with the server value max pending set to 1 instead....
View Full Document

Page1 / 11

hw2sol - CSCI 415 Computer Networks Homework 2 Solution...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online