Skip to content

Instantly share code, notes, and snippets.

@agauniyal
Created March 14, 2016 03:35
Show Gist options
  • Save agauniyal/8c4fa7550f2581bf5dcc to your computer and use it in GitHub Desktop.
Save agauniyal/8c4fa7550f2581bf5dcc to your computer and use it in GitHub Desktop.
CS NETWORKING PRACTICALS
#include <iostream>
#include <string>
#include <cstring>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 3) {
std::cerr << "Run program as 'program <ipaddress> <port>'\n";
return -1;
}
auto &ipAddress = argv[1];
auto &portNum = argv[2];
addrinfo hints, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(ipAddress, portNum, &hints, &p);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
if(p == NULL) {
std::cerr << "No addresses found\n";
return -3;
}
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
return -4;
}
int connectR = connect(sockFD, p->ai_addr, p->ai_addrlen);
if(connectR == -1) {
close(sockFD);
std::cerr << "Error while connecting socket\n";
return -5;
}
std::string buffer;
const size_t bufferSize = 50;
std::string input;
ssize_t bytes_recv = 0;
while(1) {
input.clear();
buffer.clear();
buffer.resize(100);
bytes_recv = recv(sockFD, &buffer.front(), bufferSize, 0);
if(bytes_recv == -1) {
std::cerr << "Error while receiving bytes\n";
return -6;
}
buffer.resize(bytes_recv);
std::cout << "\nClient recieved: " << buffer << std::endl;
if(buffer == "Bye" || buffer == "bye") {
std::cout << "\nBye recieved - Client closing";
close(sockFD);
freeaddrinfo(p);
return -7;
}
std::cout << "Enter message to send: ";
std::getline(std::cin, input);
auto bytes_sent = send(sockFD, input.data(), input.length(), 0);
if(input == "Bye" || input == "bye") {
return 0;
}
}
close(sockFD);
freeaddrinfo(p);
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 2) {
std::cerr << "Run program as 'program <port>'\n";
return -1;
}
auto &portNum = argv[1];
const unsigned int backLog = 8;
addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(NULL, portNum, &hints, &res);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
std::cout << "Detecting addresses" << std::endl;
unsigned int numOfAddr = 0;
char ipStr[INET6_ADDRSTRLEN];
for(p = res; p != NULL; p = p->ai_next) {
void *addr;
std::string ipVer = "IPv0";
if(p->ai_family == AF_INET) {
ipVer = "IPv4";
sockaddr_in *ipv4 = reinterpret_cast<sockaddr_in *>(p->ai_addr);
addr = &(ipv4->sin_addr);
++numOfAddr;
}
else {
ipVer = "IPv6";
sockaddr_in6 *ipv6 = reinterpret_cast<sockaddr_in6 *>(p->ai_addr);
addr = &(ipv6->sin6_addr);
++numOfAddr;
}
inet_ntop(p->ai_family, addr, ipStr, sizeof(ipStr));
std::cout << "(" << numOfAddr << ") " << ipVer << " : " << ipStr
<< std::endl;
}
if(!numOfAddr) {
std::cerr << "Found no host address to use\n";
return -3;
}
std::cout << "Enter the number of host address to bind with: ";
unsigned int choice = 0;
bool madeChoice = false;
do {
std::cin >> choice;
if(choice > (numOfAddr + 1) || choice < 1) {
madeChoice = false;
std::cout << "Wrong choice, try again!" << std::endl;
}
else
madeChoice = true;
} while(!madeChoice);
p = res;
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
freeaddrinfo(res);
return -4;
}
int bindR = bind(sockFD, p->ai_addr, p->ai_addrlen);
if(bindR == -1) {
std::cerr << "Error while binding socket\n";
close(sockFD);
freeaddrinfo(res);
return -5;
}
int listenR = listen(sockFD, backLog);
if(listenR == -1) {
std::cerr << "Error while Listening on socket\n";
close(sockFD);
freeaddrinfo(res);
return -6;
}
sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof(client_addr);
std::cin.clear();
std::cin.ignore();
std::string input;
std::string buffer;
while(1) {
int newFD =
accept(sockFD, (sockaddr *) &client_addr, &client_addr_size);
if(newFD == -1) {
std::cerr << "Error while Accepting on socket\n";
continue;
}
if(!fork()) {
close(sockFD);
bool byeRecvd = false;
do {
input.clear();
buffer.clear();
buffer.resize(100);
std::cout << "Enter message to send: ";
std::getline(std::cin, input);
auto bytes_sent = send(newFD, input.data(), input.length(), 0);
if(input == "bye" || input == "Bye") {
return 0;
}
auto bytes_recv = recv(newFD, &buffer.front(), 100, 0);
if(bytes_recv == -1) {
std::cerr << "Error while receiving bytes\n";
return -7;
}
buffer.resize(bytes_recv);
std::cout << "\nServer recieved: " << buffer << std::endl;
if(buffer == "Bye" || buffer == "bye") {
std::cout << "\nBye received - closing server!!\n";
byeRecvd = true;
}
} while(!byeRecvd);
close(newFD);
return 0;
}
close(newFD);
}
close(sockFD);
freeaddrinfo(res);
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 3) {
std::cerr << "Run program as 'program <ipaddress> <port>'\n";
return -1;
}
auto &ipAddress = argv[1];
auto &portNum = argv[2];
addrinfo hints, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(ipAddress, portNum, &hints, &p);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
if(p == NULL) {
std::cerr << "No addresses found\n";
return -3;
}
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
return -4;
}
int connectR = connect(sockFD, p->ai_addr, p->ai_addrlen);
if(connectR == -1) {
close(sockFD);
std::cerr << "Error while connecting socket\n";
return -5;
}
std::string buffer;
const size_t bufferSize = 50;
ssize_t bytes_recv = 0;
do {
if(bytes_recv == -1) {
std::cerr << "Error while receiving bytes\n";
return -6;
}
if(buffer.empty()) {
buffer.resize(bufferSize);
bytes_recv = recv(sockFD, &buffer.front(), bufferSize, 0);
}
else {
auto currentLength = buffer.length();
buffer.resize(currentLength + bufferSize);
bytes_recv = recv(sockFD, &buffer[currentLength], bufferSize, 0);
}
} while(bytes_recv != 0);
std::cout << buffer << std::endl;
close(sockFD);
freeaddrinfo(p);
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 2) {
std::cerr << "Run program as 'program <port>'\n";
return -1;
}
auto &portNum = argv[1];
const unsigned int backLog = 8;
addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(NULL, portNum, &hints, &res);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
std::cout << "Detecting addresses\n";
unsigned int numOfAddr = 0;
char ipStr[INET6_ADDRSTRLEN];
for(p = res; p != NULL; p = p->ai_next) {
void *addr;
std::string ipVer = "IPv0";
if(p->ai_family == AF_INET) {
ipVer = "IPv4";
auto *ipv4 = reinterpret_cast<sockaddr_in *>(p->ai_addr);
addr = &(ipv4->sin_addr);
++numOfAddr;
}
else {
ipVer = "IPv6";
auto *ipv6 = reinterpret_cast<sockaddr_in6 *>(p->ai_addr);
addr = &(ipv6->sin6_addr);
++numOfAddr;
}
inet_ntop(p->ai_family, addr, ipStr, sizeof(ipStr));
std::cout << "(" << numOfAddr << ") " << ipVer << " : " << ipStr
<< std::endl;
}
if(!numOfAddr) {
std::cerr << "Found no host address to use\n";
return -3;
}
std::cout << "Enter the number of host address to bind with: ";
unsigned int choice = 0;
bool madeChoice = false;
do {
std::cin >> choice;
if(choice > (numOfAddr + 1) || choice < 1) {
madeChoice = false;
std::cout << "Wrong choice, try again!\n";
}
else
madeChoice = true;
} while(!madeChoice);
p = res;
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
freeaddrinfo(res);
return -4;
}
int bindR = bind(sockFD, p->ai_addr, p->ai_addrlen);
if(bindR == -1) {
std::cerr << "Error while binding socket\n";
close(sockFD);
freeaddrinfo(res);
return -5;
}
int listenR = listen(sockFD, backLog);
if(listenR == -1) {
std::cerr << "Error while Listening on socket\n";
close(sockFD);
freeaddrinfo(res);
return -6;
}
sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof(client_addr);
std::string input(25, ' ');
input.shrink_to_fit();
while(1) {
int newFD = accept(sockFD, (sockaddr *)&client_addr, &client_addr_size);
if(newFD == -1) {
std::cerr << "Error while Accepting on socket\n";
continue;
}
if(!fork()) {
close(sockFD);
std::cout << "Sending DateTime to client! " << std::endl;
std::time_t t = std::time(0);
input = std::ctime(&t);
auto bytes_sent = send(newFD, input.data(), input.length(), 0);
close(newFD);
freeaddrinfo(res);
return 0;
}
close(newFD);
}
close(sockFD);
freeaddrinfo(res);
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <ctime>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 2) {
std::cerr << "Run program as 'program <port>'\n";
return -1;
}
auto &portNum = argv[1];
const unsigned int backLog = 8;
addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(NULL, portNum, &hints, &res);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
std::cout << "Detecting addresses" << std::endl;
unsigned int numOfAddr = 0;
char ipStr[INET6_ADDRSTRLEN];
for(p = res; p != NULL; p = p->ai_next) {
void *addr;
std::string ipVer = "IPv0";
if(p->ai_family == AF_INET) {
ipVer = "IPv4";
sockaddr_in *ipv4 = reinterpret_cast<sockaddr_in *>(p->ai_addr);
addr = &(ipv4->sin_addr);
++numOfAddr;
}
else {
ipVer = "IPv6";
sockaddr_in6 *ipv6 = reinterpret_cast<sockaddr_in6 *>(p->ai_addr);
addr = &(ipv6->sin6_addr);
++numOfAddr;
}
inet_ntop(p->ai_family, addr, ipStr, sizeof(ipStr));
std::cout << "(" << numOfAddr << ") " << ipVer << " : " << ipStr
<< std::endl;
}
if(!numOfAddr) {
std::cerr << "Found no host address to use\n";
return -3;
}
std::cout << "Enter the number of host address to bind with: ";
unsigned int choice = 0;
bool madeChoice = false;
do {
std::cin >> choice;
if(choice > (numOfAddr + 1) || choice < 1) {
madeChoice = false;
std::cout << "Wrong choice, try again!" << std::endl;
}
else
madeChoice = true;
} while(!madeChoice);
p = res;
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
freeaddrinfo(res);
return -4;
}
int bindR = bind(sockFD, p->ai_addr, p->ai_addrlen);
if(bindR == -1) {
std::cerr << "Error while binding socket\n";
close(sockFD);
freeaddrinfo(res);
return -5;
}
int listenR = listen(sockFD, backLog);
if(listenR == -1) {
std::cerr << "Error while Listening on socket\n";
close(sockFD);
freeaddrinfo(res);
return -6;
}
sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof(client_addr);
std::string input(25, ' ');
input.shrink_to_fit();
while(1) {
int newFD = accept(sockFD, (sockaddr *)&client_addr, &client_addr_size);
if(newFD == -1) {
std::cerr << "Error while Accepting on socket\n";
continue;
}
std::cout << "Sending DateTime to client! " << std::endl;
std::time_t t = std::time(0);
input = std::ctime(&t);
auto bytes_sent = send(newFD, input.data(), input.length(), 0);
close(newFD);
}
close(sockFD);
freeaddrinfo(res);
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 2) {
std::cerr << "Run program as 'program <port>'\n";
return -1;
}
auto &portNum = argv[1];
const unsigned int backLog = 8;
addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(NULL, portNum, &hints, &res);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
std::cout << "Detecting addresses" << std::endl;
unsigned int numOfAddr = 0;
char ipStr[INET6_ADDRSTRLEN];
for(p = res; p != NULL; p = p->ai_next) {
void *addr;
std::string ipVer = "IPv0";
if(p->ai_family == AF_INET) {
ipVer = "IPv4";
sockaddr_in *ipv4 = reinterpret_cast<sockaddr_in *>(p->ai_addr);
addr = &(ipv4->sin_addr);
++numOfAddr;
}
else {
ipVer = "IPv6";
sockaddr_in6 *ipv6 = reinterpret_cast<sockaddr_in6 *>(p->ai_addr);
addr = &(ipv6->sin6_addr);
++numOfAddr;
}
inet_ntop(p->ai_family, addr, ipStr, sizeof(ipStr));
std::cout << "(" << numOfAddr << ") " << ipVer << " : " << ipStr
<< std::endl;
}
if(!numOfAddr) {
std::cerr << "Found no host address to use\n";
return -3;
}
std::cout << "Enter the number of host address to bind with: ";
unsigned int choice = 0;
bool madeChoice = false;
do {
std::cin >> choice;
if(choice > (numOfAddr + 1) || choice < 1) {
madeChoice = false;
std::cout << "Wrong choice, try again!" << std::endl;
}
else
madeChoice = true;
} while(!madeChoice);
p = res;
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
freeaddrinfo(res);
return -4;
}
int bindR = bind(sockFD, p->ai_addr, p->ai_addrlen);
if(bindR == -1) {
std::cerr << "Error while binding socket\n";
close(sockFD);
freeaddrinfo(res);
return -5;
}
int listenR = listen(sockFD, backLog);
if(listenR == -1) {
std::cerr << "Error while Listening on socket\n";
close(sockFD);
freeaddrinfo(res);
return -6;
}
sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof(client_addr);
std::string input;
std::cin.clear();
std::cin.ignore();
while(1) {
int newFD = accept(sockFD, (sockaddr *)&client_addr, &client_addr_size);
if(newFD == -1) {
std::cerr << "Error while Accepting on socket\n";
continue;
}
std::cout << "Enter message to send: ";
std::getline(std::cin, input);
auto bytes_sent = send(newFD, input.data(), input.length(), 0);
close(newFD);
}
close(sockFD);
freeaddrinfo(res);
return 0;
}
#include <iostream>
#include <string>
#include <cstring>
extern "C" {
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
}
int main(int argc, char *argv[])
{
if(argc != 2) {
std::cerr << "Run program as 'program <port>'\n";
return -1;
}
auto &portNum = argv[1];
const unsigned int backLog = 8;
addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gAddRes = getaddrinfo(NULL, portNum, &hints, &res);
if(gAddRes != 0) {
std::cerr << gai_strerror(gAddRes) << "\n";
return -2;
}
std::cout << "Detecting addresses" << std::endl;
unsigned int numOfAddr = 0;
char ipStr[INET6_ADDRSTRLEN];
for(p = res; p != NULL; p = p->ai_next) {
void *addr;
std::string ipVer = "IPv0";
if(p->ai_family == AF_INET) {
ipVer = "IPv4";
sockaddr_in *ipv4 = reinterpret_cast<sockaddr_in *>(p->ai_addr);
addr = &(ipv4->sin_addr);
++numOfAddr;
}
else {
ipVer = "IPv6";
sockaddr_in6 *ipv6 = reinterpret_cast<sockaddr_in6 *>(p->ai_addr);
addr = &(ipv6->sin6_addr);
++numOfAddr;
}
inet_ntop(p->ai_family, addr, ipStr, sizeof(ipStr));
std::cout << "(" << numOfAddr << ") " << ipVer << " : " << ipStr
<< std::endl;
}
if(!numOfAddr) {
std::cerr << "Found no host address to use\n";
return -3;
}
std::cout << "Enter the number of host address to bind with: ";
unsigned int choice = 0;
bool madeChoice = false;
do {
std::cin >> choice;
if(choice > (numOfAddr + 1) || choice < 1) {
madeChoice = false;
std::cout << "Wrong choice, try again!" << std::endl;
}
else
madeChoice = true;
} while(!madeChoice);
p = res;
int sockFD = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if(sockFD == -1) {
std::cerr << "Error while creating socket\n";
freeaddrinfo(res);
return -4;
}
int bindR = bind(sockFD, p->ai_addr, p->ai_addrlen);
if(bindR == -1) {
std::cerr << "Error while binding socket\n";
close(sockFD);
freeaddrinfo(res);
return -5;
}
int listenR = listen(sockFD, backLog);
if(listenR == -1) {
std::cerr << "Error while Listening on socket\n";
close(sockFD);
freeaddrinfo(res);
return -6;
}
sockaddr_storage client_addr;
socklen_t client_addr_size = sizeof(client_addr);
std::string input;
std::cin.clear();
std::cin.ignore();
while(1) {
int newFD = accept(sockFD, (sockaddr *)&client_addr, &client_addr_size);
if(newFD == -1) {
std::cerr << "Error while Accepting on socket\n";
continue;
}
if(!fork()) {
close(sockFD);
std::cout << "\nEnter message to send: ";
std::getline(std::cin, input);
auto bytes_sent = send(newFD, input.data(), input.length(), 0);
close(newFD);
freeaddrinfo(res);
return 0;
}
close(newFD);
}
close(sockFD);
freeaddrinfo(res);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment