Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <winsock.h> //note that this requires a linked .lib
- #include <fstream>
- using namespace std;
- WSADATA wsadata;
- bool init(){
- int error = WSAStartup(0x0202, &wsadata);
- if(error){
- return false;
- }
- if(wsadata.wVersion != 0x0202)
- {
- WSACleanup(); //Clean up Winsock
- return false;
- }
- return true;
- }
- string MESSAGE;
- char file_piece[1024];
- string file_full;
- int TCP_PORT = 5721;
- string TCP_IP;
- bool CloseConnection (SOCKET s)
- {
- //Close the socket if it exists
- if(s)
- closesocket(s);
- return true;
- }
- char* get_local_address(){
- char ac[80];
- gethostname(ac, sizeof(ac));
- struct hostent *phe = gethostbyname(ac);
- if (phe == 0) {
- cerr << "Yow! Bad host lookup." << endl;
- return NULL;
- }
- struct in_addr addr;
- memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr));
- return inet_ntoa(addr);
- }
- //CONNECTTOHOST – Connects to a remote host
- bool ConnectToHost(int PortNo, const char* IPAddress)
- {
- //Fill out the information needed to initialize a socket…
- SOCKADDR_IN target; //Socket address information
- target.sin_family = AF_INET; // address family Internet
- target.sin_port = htons (PortNo); //Port to connect on
- target.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
- SOCKET s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
- if (s == INVALID_SOCKET)
- {
- return false; //Couldn't create the socket
- }
- //Try connecting...
- if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
- {
- return false; //Couldn't connect
- }
- else
- send(s, MESSAGE.c_str(), MESSAGE.length(), 0);
- return true; //Success
- CloseConnection(s);
- }
- void handshake_send(){
- ConnectToHost(TCP_PORT, TCP_IP.c_str());
- }
- SOCKET SetUpListener(const char* pcAddress, int nPort)
- {
- u_long nInterfaceAddr = inet_addr(pcAddress);
- if (nInterfaceAddr != INADDR_NONE) {
- SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
- if (sd != INVALID_SOCKET) {
- sockaddr_in sinInterface;
- sinInterface.sin_family = AF_INET;
- sinInterface.sin_addr.s_addr = nInterfaceAddr;
- sinInterface.sin_port = nPort;
- if (bind(sd, (sockaddr*)&sinInterface,
- sizeof(sockaddr_in)) != SOCKET_ERROR) {
- listen(sd, 1);
- return sd;
- }
- }
- }
- return INVALID_SOCKET;
- }
- SOCKET AcceptConnection(SOCKET ListeningSocket, sockaddr_in& sinRemote)
- {
- int nAddrSize = sizeof(sinRemote);
- return accept(ListeningSocket, (sockaddr*)&sinRemote, &nAddrSize);
- }
- SOCKET SetUpListener(const char* pcAddress, int nPort);
- SOCKET AcceptConnection(SOCKET ListeningSocket, sockaddr_in& sinRemote);
- char acReadBuffer[1024];
- bool exit_server_1;
- bool exit_server_2;
- bool password_entered = false;
- int password_try_count = 3;
- bool request_done = false;
- bool read_sock(SOCKET sd)
- {
- // Read data from client
- int nReadBytes = 1;
- while(nReadBytes != 0){
- nReadBytes = recv(sd, acReadBuffer, 5000, 0);
- if((string)acReadBuffer == "got"){
- password_entered = true;
- return true;
- }
- else if((string)acReadBuffer == "password"){
- password_entered = false;
- cout << "You have " << password_try_count << " attempts left." << endl;
- password_try_count--;
- if(password_try_count <= 0){
- MESSAGE = "kill";
- handshake_send();
- exit(-1);
- }
- return true;
- }
- if(strlen(acReadBuffer) > 0){
- memcpy(acReadBuffer, file_piece, sizeof(acReadBuffer));
- return true;
- }
- if (nReadBytes == SOCKET_ERROR) {
- return false;
- }
- }
- if(exit_server_1){
- exit_server_2 = true;
- }
- return false;
- }
- WSADATA w;
- int handshake_receive()
- {
- string IP_BIND = get_local_address();
- cout << IP_BIND << endl;
- //SOCKET ListeningSocket = SetUpListener(IP_BIND.c_str(), htons(TCP_PORT));
- //if (ListeningSocket == INVALID_SOCKET) {
- // cout << "Error with listener: " << WSAGetLastError() << endl;
- // return -1;
- //}
- // Spin forever handling clients
- while (1) {
- SOCKET ListeningSocket = SetUpListener(IP_BIND.c_str(), htons(TCP_PORT));
- if (ListeningSocket == INVALID_SOCKET) {
- cout << "Error with listener: " << WSAGetLastError() << endl;
- return -1;
- }
- // Wait for a connection, and accepting it when one arrives.
- cout << "Waiting for a connection..." << flush;
- sockaddr_in sinRemote;
- SOCKET sd = AcceptConnection(ListeningSocket, sinRemote);
- if (sd != INVALID_SOCKET) {
- cout << "Accepted connection from " <<
- inet_ntoa(sinRemote.sin_addr) << ":" <<
- ntohs(sinRemote.sin_port) << "." << endl;
- }
- else {
- cout << "Error with accepter: " << WSAGetLastError() << endl;
- return -1;
- }
- if(read_sock(sd)){
- if(password_entered){
- exit_server_2 = true;
- }
- exit_server_1 = true;
- }
- if(exit_server_1 && exit_server_2){
- exit_server_1 = false;
- exit_server_2 = false;
- break;
- }
- if(CloseConnection(sd) && CloseConnection(ListeningSocket)){}
- else{
- cout << "Error disconnecting: " << WSAGetLastError() << endl;
- return -1;
- }
- }
- return 0;
- }
- string url_request;
- string password;
- void client(){
- cout << "Setting up client." << endl;
- cout << "IP of server? ";
- cin >> TCP_IP;
- while(true){
- if(!password_entered){
- cout << "Enter password: ";
- cin >> password;
- MESSAGE = password;
- handshake_send();
- if(handshake_receive() == -1){
- cout << "Error occurred, restarting server." << endl;
- return;
- }
- cout << "here" << endl;
- }
- else{
- cout << "URL: " << endl;
- cin >> url_request;
- MESSAGE = url_request;
- handshake_send();
- if(handshake_receive() == -1){
- cout << "Error occurred, restarting server." << endl;
- return;
- }
- }
- if(request_done){
- cout << "Requested URL " << url_request << " fetched." << endl;
- cout << "You will find the HTML file to your URL in your current directory." << endl;
- }
- }
- }
- int main()
- {
- if(!init()){
- cout << "Something went wrong. Please restart the program." << endl;
- return 0;
- }
- while(true){
- client();
- }
- WSACleanup(); //Clean up Winsock
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement