Assignment description: This is a C++ project. The comms class containing the

Similar documents
Winsock Server adding Multiple clients support C++

Appendix B: NET_CON Source Code

CSCI 415 Computer Networks Homework 2 Due 02/13/08

2007 Microsoft Corporation. All rights reserved.

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

A. Basic Function Calls for Network Communications

Security impact of noexcept

CSCE 463/612 Networks and Distributed Processing Spring 2017

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Internetworking II: Network programming. April 20, 2000

Linked List using a Sentinel

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CWRU High-Speed Flexible Parts Feeder. Software Source Code

Introduction to Computer Networks

System Programming. Sockets: examples

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

Socket Programming for TCP and UDP

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Computer Network Programming

About 1. Chapter 1: Getting started with sockets 2. Remarks 2. Examples 2. How to instantiate a socket class object 2

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

PROGRAMMING IN C++ CVIČENÍ

ECE 435 Network Engineering Lecture 2

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

ECE 435 Network Engineering Lecture 2

Dr. Tim Lin ECE Department Cal Poly Pomona

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

CSE 333 Section 8 - Client-Side Networking

I/O Multiplexing. Dec 2009

Strings and Stream I/O

CS242 COMPUTER PROGRAMMING

CS321: Computer Networks Socket Programming

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CSE 333 Lecture 16 - network programming intro

Network Programming November 3, 2008

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Concurrent Servers Dec 2, 2009"

CS 351 Week Advanced UNIX Programming: Rochkind, Marc J. 1. To learn about System Interprocess Communication (IPC). 2. To learn about Sockets.

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

SOCKETLIB. Requirements

Sockets 15H2. Inshik Song

WINDOWS SOCKET PROGRAMMING

>>> SOLUTIONS <<< Answer the following questions regarding the basics of computer networks and the Internet.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

10. I/O System Library

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

Lab 0. Yvan Petillot. Networks - Lab 0 1

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Redesde Computadores(RCOMP)

Common Misunderstandings from Exam 1 Material

Distributed Systems Recitation 2. Tamim Jabban

Distributed Real-Time Control Systems. Lecture 24 Asynchronous Socket Communications

CSE 333 SECTION 6. Networking and sockets

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

Chapter 6. The Transport Layer. Transport Layer 3-1

Distributed Real-Time Control Systems. Module 26 Sockets

CSE 333 Final Exam June 6, 2017 Sample Solution

CS 640: Computer Networking

chat.h #ifndef _CHAT_H #define _CHAT_H #define LBUFFSIZE 128 #define CBUFFSIZE 331

Java Basics 5 - Sockets. Manuel Oriol - May 4th, 2006

Client-side Networking

CSE 124 Discussion Section Sockets Programming 10/10/17

EECS 123: Introduction to Real-Time Distributed Programming

Lab 1: First Steps in C++ - Eclipse

Processes and Threads

Message passing systems are popular because they support client-server interactions, where: clients send messages to servers requesting a server.

Peter Van Eeckhoutte s Blog

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

CS321: Computer Networks Introduction to Application Layer

>>> SOLUTIONS <<< Answer the following questions regarding the basics principles and concepts of networks.

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Java Support for developing TCP Network Based Programs

CONCURRENCY MODEL. UNIX Programming 2014 Fall by Euiseong Seo

C++ Namespaces, Exceptions

SOCKETS. COMP750 Distributed Systems

Midterm Exam II MCS 275 Programming Tools 14 March 2017

Introduction to C++ Systems Programming

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

Socket Programming 2007/03/28

Introduction to Socket Programming

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSCI Computer Networking: Application Layer Programming George Blankenship. Programming George Blankenship 1

Department of Computer Science

CS2141 Software Development using C/C++ C++ Basics

Direct Memory Access. Lecture 2 Pointer Revision Command Line Arguments. What happens when we use pointers. Same again with pictures

Group-A Assignment No. 6

518 Lecture Notes Week 12

Transcription:

Assignment description: This is a C++ project. The comms class containing the code that is common to both the client and server. The classes should contain the functionality required to establishing a TCP WinSock2 connection between two applications running within two processes on a single machine, one being a client and the other a server. They should also implement the functionality that will allow messages (array of char) to be exchanged between the Client and the Server. Using these classes develop a chat application that when run, requests the client enter a message which is then echoed on the server interface. A message can then be typed into the server interface and this is then echoed on the client interface. This process should repeat indefinitely until the client types QUIT, at which point both the client and server programs should terminate. The classes and application should include exception handling code, that replaces much of the original error handling statements found within the practical exercises. Pre-processor directives should be embedded within your project to enable / disable compilation of client and server code. This should allow you to develop a single project that contains both the Client and Server code. Solution: Comms class:

Comms.h #pragma once #pragma comment(lib, "ws2_32.lib") // library for winsock2 #include <winsock2.h> // winsock2 library #include <iostream> // cin/cout #include <string> // string operations #define SERVER 1 #define CLIENT 2 #define DefaultPort 8888 #define DefaultServerAddress "127.0.0.1" // local host #define BUFSIZE 100 // maximum length of text message using std::cin; using std::cout; using std::endl; class Comms { public: Comms(); // default constructor static void InitServerSocket(); // initializes socket, address, etc. static unsigned long stdcall Receive(void * pparam); // processes input messages void Send(); // sends messages protected: SOCKET serversocket; // server socket sockaddr_in addr; // address ;

Comms.cpp #include "Comms.h" Comms::Comms() { // default constructor InitServerSocket(); void Comms::InitServerSocket() { // empty function unsigned long stdcall Comms::Receive(void * pparam) { // empty function return 0; void Comms::Send() { // empty function Server class Server.h #pragma once #include "Comms.h" class Server: public Comms { public: Server(); // default constructor void InitServerSocket(); // initializes socket, binds it with address, etc. static unsigned long stdcall Receive(void* pparam); // processes input message

void Send(); // sends messages to client private: SOCKET clientsocket; // stores client's socket ; Server.cpp #include "Server.h" namespace serv { // to avoid name collisions bool IsQuitRequested = false; Server::Server() { // default constructor InitServerSocket(); void Server::InitServerSocket() { serversocket = socket(af_inet, SOCK_STREAM, 0); // create socket for internetwork: UDP, TCP, etc.; stream socket; 0 - TCP protocol addr.sin_family = AF_INET; // internetwork: UDP, TCP, etc. addr.sin_port=htons(defaultport); // set port addr.sin_addr.s_addr=inet_addr(defaultserveraddress); // set address bind(serversocket, (sockaddr *)&addr,sizeof(addr)); // binding address with socket int c = listen(serversocket,1); // waiting for connection; 1 client only excepted cout<<"server receive ready"<<endl; unsigned long stdcall Server::Receive(void* pparam) {

Server * server = (Server *)pparam; // server class, stores info about client socket char buf[bufsize]; // buffer for text message memset(buf, 0, BUFSIZE); // clear buffer try { while(recv(server->clientsocket, buf, BUFSIZE - 1, 0) > 0) { // while receive messages from client if(strcmp(buf, "QUIT") == 0) { // if QUIT received cout<<"client quit command received!"<<endl; serv::isquitrequested = true; // set flag to close all the sockets and terminate the program break; else { cout<<"client: "<<buf<<endl; // display client's message memset(buf, 0, BUFSIZE); // clear buffer throw -1; // client disconnected, throw exception catch (int a) { if (a == -1) { cout<<"client disconnected.\n"; closesocket(server->clientsocket); // close sockets closesocket(server->serversocket); exit(0); // exit server type program void Server::Send()

{ char buf[bufsize]; // buffer for messages sockaddr_in client_addr; int client_addr_size = sizeof(client_addr); fd_set sockstocheck; TIMEVAL timeout; timeout.tv_sec = 1; // period for checking new client connections while(1) { FD_ZERO(&socksToCheck); FD_SET(serverSocket, &sockstocheck); int res = select(1, &sockstocheck, 0, 0, &timeout); // organizes checking process if(res == 1) { // user connected clientsocket = accept(serversocket,(sockaddr *)&client_addr, &client_addr_size); // accept client connection cout<<"client connected.\n"<<endl; strcpy(buf, "You connected to server\n"); send(clientsocket, buf, strlen(buf), 0); // inform client about connection DWORD threadid; CreateThread(NULL,NULL, &Server::Receive, this, NULL, &threadid); // receive message from client to server in another thread thread break; while (!serv::isquitrequested) { cin.getline(buf, BUFSIZE); if (send(clientsocket, buf, strlen(buf), 0) == SOCKET_ERROR) break; // send messages from server to client

cout<<"server shutting down."<<endl; closesocket(clientsocket); // closing all the sockets closesocket(serversocket); Client class Client.h #pragma once #include "Comms.h" class Client: public Comms { public: Client(); // default constructor void InitServerSocket(); // init socket static unsigned long stdcall Receive(void* pparam); // process messages from server void Send(); // send message to server ; Client.cpp #include "Client.h" Client::Client() { // default constructor InitServerSocket(); void Client::InitServerSocket() {

serversocket = socket(af_inet, SOCK_STREAM, 0); // create socket for internetwork: UDP, TCP, etc.; stream socket; 0 - TCP protocol addr.sin_family = AF_INET; // internetwork: UDP, TCP, etc. addr.sin_port=htons(defaultport); // set port addr.sin_addr.s_addr=inet_addr(defaultserveraddress); // set address unsigned long stdcall Client::Receive(void* pparam) { SOCKET * sock = (SOCKET *)pparam; // stores info about server socket char buf[bufsize]; // buffer for text message memset(buf, 0, BUFSIZE); // clear buffer try { from server while(recv(*sock, buf, BUFSIZE - 1, 0) > 0) // while receive messages { cout<<"server: "<<buf<<endl; // display message memset(buf, 0, BUFSIZE); // clear buffer server throw "Server disconnected"; // throw exception if cannot access to catch (const char * s) { // catch server disconnected exception printf("%s\n", s); closesocket(*sock); // close socket return 1; void Client::Send()

{ try { if(connect(serversocket,(sockaddr *)&addr,sizeof(addr))!= 0) throw "Error connecting to server."; // exception catch (const char * s) { printf("%s\n", s); // report about exception return; DWORD threadid; CreateThread(NULL,NULL,&Client::Receive, &serversocket, NULL, &threadid); // receive from server in another thread thread char buf[bufsize]; // buffer for messages while(1) { cin.getline(buf, BUFSIZE); // read message from stdin if (send(serversocket, buf, strlen(buf), 0) == SOCKET_ERROR (strcmp(buf, "QUIT") == 0)) break; // send message to server closesocket(serversocket); // close server socket main.cpp (server project) #include "Client.h" #include "Server.h" #define PROJECT_TYPE SERVER // set project type

WORD wversionrequested; // needed to init sockets lib WSADATA wsadata; int err; bool InitSocketsLib(); bool ReleaseSocketsLib(); int main (int argc, char ** argv) { try { if (InitSocketsLib() == false) throw "Cannot initialize sockets library."; // exception catch (const char * s) { cout<<s<<endl; // report about exception return 1; try { if (PROJECT_TYPE == SERVER) { Server obj; obj.send(); else if (PROJECT_TYPE == CLIENT) { Client obj; obj.send(); else throw "Invalid project type."; // exception

catch (const char * s) { cout<<s<<endl; // report about exception return 1; try { if (ReleaseSocketsLib() == false) throw "Cannot release sockets library."; // exception catch (const char * s) { cout<<s<<endl; // report about exception return 1; return 0; bool InitSocketsLib() // initializes sockets library { wversionrequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsadata); return (err == 0); bool ReleaseSocketsLib() // releases socket library { err = WSACleanup(); return (err == 0);

main,cpp (client project) #include "Client.h" #include "Server.h" #define PROJECT_TYPE CLIENT // set project type WORD wversionrequested; // needed to init sockets lib WSADATA wsadata; int err; bool InitSocketsLib(); bool ReleaseSocketsLib(); int main (int argc, char ** argv) { try { if (InitSocketsLib() == false) throw "Cannot initialize sockets library."; // exception catch (const char * s) { cout<<s<<endl; // report about exception return 1; try { if (PROJECT_TYPE == SERVER) { Server obj; obj.send(); else if (PROJECT_TYPE == CLIENT) { Client obj;

else obj.send(); throw "Invalid project type."; // exception catch (const char * s) { cout<<s<<endl; // report about exception return 1; try { if (ReleaseSocketsLib() == false) throw "Cannot release sockets library."; // exception catch (const char * s) { cout<<s<<endl; // report about exception return 1; return 0; bool InitSocketsLib() // initializes sockets library { wversionrequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsadata); return (err == 0); bool ReleaseSocketsLib() // releases socket library {

err = WSACleanup(); return (err == 0); Below see the test screenshots: Test 1 (chatting) 1. Launching server: 2. Now server is waiting for client. Launching client: Server output:

Client output: 3. Client writes Hello : Client output:

Server output: 4. Server writes Hi to client:

Server output: Client output: 5. Client sends QUIT command: Server output:

Client output: Now both programs terminate. Test 2 (server not found) 1. Launching client program before server: Client output:

Test 3 (terminating client program) 1. Launching server, client (see test 1 screenshots 1, 2). 2. Closing client program: Server output: Now server program terminates. Test 4 (server disconnected)

1. Launching server, client (see test 1 screenshots 1, 2). 2. Closing server program. Client output: Now client program terminates.