Skip to content

Instantly share code, notes, and snippets.

@nealian
Created October 25, 2016 00:15
Show Gist options
  • Save nealian/abd2301ebcd3deb3382f4c2cf41685fd to your computer and use it in GitHub Desktop.
Save nealian/abd2301ebcd3deb3382f4c2cf41685fd to your computer and use it in GitHub Desktop.
Networking Project 1
// based on work from http://cs.baylor.edu/~donahoo/practical/CSockets/practical/
/*
* C++ sockets on Unix and Windows
* Copyright (C) 2002
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "PracticalSocket.h" // For Socket and SocketException
#include <iostream> // For cerr and cout
#include <fstream> // For file streams
#include <sstream> // For atoi() replacement
#include <string> // String processing
#include <unistd.h> // For fork()
using namespace std;
const unsigned int RCVBUFSIZE = 1024; // Size of receive buffer
void HandleTCPClient(TCPSocket *sock, string nodeName);
unsigned short string_to_us(string instring);
void send_until_eof(TCPSocket *sock, ifstream *infile);
int main(int argc, char *argv[]) {
string servAddress = "localhost";
if (fork() == 0) {
// Node A
try {
string current_line;
ifstream confAFile ("confA.txt");
getline(confAFile, current_line); // First line contains Node B port
unsigned short nodeBPort = string_to_us(current_line);
// Establish connection with Node B
TCPSocket clientNodeA(servAddress, nodeBPort);
send_until_eof(&clientNodeA, &confAFile);
} catch(SocketException &e) {
cerr << e.what() << endl;
exit(1);
}
} else if(fork() == 0) {
// Node B
try {
string current_line;
ifstream confBFile ("confB.txt");
getline(confBFile, current_line); // First line; contains node B port
unsigned short nodeBPort = string_to_us(current_line);
getline(confBFile, current_line); // Second line; contains node C port
unsigned short nodeCPort = string_to_us(current_line);
TCPServerSocket servNodeB(nodeBPort); // Server Socket object
TCPSocket clientNodeB(servAddress, nodeCPort);
send_until_eof(&clientNodeB, &confBFile);
// Get connection from Node A
HandleTCPClient(servNodeB.accept(), "B"); // Wait for a client to connect
} catch(SocketException &e) {
cerr << e.what() << endl;
exit(1);
}
} else {
// Node C
try {
string current_line;
ifstream confCFile ("confC.txt");
// Apparently this is the more C++ way to do atoi(current_line)?
getline(confCFile, current_line); // First line; contains node B port
unsigned short nodeCPort = string_to_us(current_line);
TCPServerSocket servNodeC(nodeCPort); // Server Socket object
// Get connection from Node B
HandleTCPClient(servNodeC.accept(), "C"); // Wait for a client to connect
} catch(SocketException &e) {
cerr << e.what() << endl;
exit(1);
}
}
return 0;
}
// for nodes B and C
void HandleTCPClient(TCPSocket *sock, string nodeName) {
char echoBuffer[RCVBUFSIZE] = {'\0'};
int recvMsgSize;
cout << "Node " << nodeName << " received: ";
while ((recvMsgSize = sock->recv(echoBuffer, RCVBUFSIZE)) > 0) { // Zero means EOT
echoBuffer[recvMsgSize] = '\0'; // Gotta clean up the trash from last iteration.
cout << echoBuffer;
}
delete sock;
}
unsigned short string_to_us(string instring) {
// Apparently this is a more C++ way to do atoi(current_line)?
stringstream convert(instring);
unsigned short outnum;
convert >> outnum;
return outnum;
}
void send_until_eof(TCPSocket *sock, ifstream *infile) {
string current_line;
if (infile->is_open()) {
while (getline(*infile, current_line)) {
// Send the string to the server
stringstream line_with_end;
line_with_end << current_line << endl;
sock->send(line_with_end.str().c_str(), line_with_end.str().size());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment