All pastes #361414 Raw Edit

Anonymous

public cpp v1 · immutable
·published 2007-02-18 03:28 UTC
rendered paste body
//Client to Aaron's fileserver//Author  $ Dylan Johnston//Connect $ bitforge.redirectme.net:404//Notes   $ Win-sock-two#include <cstdlib>#include <iostream>#include <string>#include <windows.h>#include <winsock2.h>#include <ws2tcpip.h>#define WIN32_LEAN_AND_MEANusing namespace std;const string DEBUG = "[debug]: ";const string INPUT = "[input]: ";const string BYTES = "[bytes]: ";char serverAddress[4096];//typedef unsigned char byte;//byte myBuffer[4097]; //<3 Aaron[]char sendbuf[4096];char recvbuf[4096];char backup[4096];char *result;SOCKET bytesSent;SOCKET bytesRecv;SOCKET ConnectSocket;int KeepLooping = 1;int UseTheNetwork = 0;int Commands(char sCom[]){    strcpy (backup,sCom);    //LIST CONTENTS IN CURRENY DIRECTORY (kinda useless?)    if (!strcmp(sCom, "list")){        UseTheNetwork = 1;        sprintf(sendbuf, "002\r\n");        bytesSent = send( ConnectSocket, sendbuf, strlen(sendbuf), 0 );        cout << DEBUG << "command \"list\" sent\n";    //QUIT CLIENT PROPERLY    } else if (!strcmp(sCom, "quit")){        KeepLooping = 0;            //MAKE DIRECTORY CODE #004        } else if ( !strcmp( "mkdir", strtok( sCom, " " ) ) ) {        UseTheNetwork = 1;        strtok( backup, " " );        sprintf( sendbuf, "004 \"%s\"\r\n", strtok( NULL, " " ) );        bytesSent = send( ConnectSocket, sendbuf, strlen(sendbuf), 0 );        cout << DEBUG << "command \"mkdir\" sent\n";    //CHANGE DIRECTORY CODE #005    } else if ( !strcmp( "cd", strtok( sCom, " " ) ) ) {        UseTheNetwork = 1;        strtok( backup, " " );        sprintf( sendbuf, "005 \"%s\"\r\n", strtok( NULL, " " ) );        bytesSent = send( ConnectSocket, sendbuf, strlen(sendbuf), 0 );        cout << DEBUG << "command \"cd\" sent\n";    //IF COMMAND IS UNKNOWN [we won't let it even be able to be sent]    } else {        UseTheNetwork = 0;        cout << "unknown command\n";    }}int main(){    //ONLY REAL CONSOLE GUI FUNTION :D    SetConsoleTitle("FILESERVER CLIENT");    int iResult;        WSADATA wsaData; //start winsock    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);        if (iResult != 0)     {        cout << DEBUG << "failed" << iResult << "\n";        return 0;        closesocket( ConnectSocket );     }         cout << DEBUG << "successful startup of winsock\n";    //SOCKET ConnectSocket;    ConnectSocket = socket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP);        if (ConnectSocket == INVALID_SOCKET)     {        cout << DEBUG << "error " << WSAGetLastError();        closesocket( ConnectSocket );         closesocket( ConnectSocket );     }        addrinfo *aiList = 0;    addrinfo aiHints = {0};    aiHints.ai_family = AF_UNSPEC;    aiHints.ai_socktype = SOCK_STREAM;    aiHints.ai_protocol = IPPROTO_TCP;    cout << INPUT << "address: ";    gets(serverAddress);        //GET IP FROM DNS    if (getaddrinfo( serverAddress, "404", &aiHints, &aiList ) != 0 )     {         cout << DEBUG << "the address did not resolve\n";        closesocket( ConnectSocket );         WSACleanup();         system("Pause");        return 0;     }                int result;    cout << DEBUG << "connecting..." << "\n";    addrinfo * current = aiList;        //CONNECT TO SERVER    while ( current != 0 )    {        result = connect( ConnectSocket, current->ai_addr, (int)current->ai_addrlen );        if ( result == SOCKET_ERROR )        {            closesocket( ConnectSocket );            current = current->ai_next;        } else {            cout << DEBUG << "successfully connected to " << serverAddress << "\n";            break;        }    }    //gets the GAYEST WELCOME MESSAGE EVER    while( bytesRecv != SOCKET_ERROR ) {        bytesRecv = recv( ConnectSocket, recvbuf, 4096, 0 );        if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {            cout << DEBUG << "connection closed\n";            break;        } else {            cout << BYTES;            printf( "Bytes Recv: %ld\n", bytesRecv );            cout << DEBUG << recvbuf << "\n";            break;        }    }        while (KeepLooping = 1){    cout << INPUT << "message: ";    gets(sendbuf);    Commands(sendbuf);    if (KeepLooping == 0){        cout << DEBUG << "breaking main loop\n";        break;    } else if (UseTheNetwork == 1){        while( bytesRecv != SOCKET_ERROR ) {            bytesRecv = recv( ConnectSocket, recvbuf, 4096, 0 );            if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {                cout << DEBUG << "connection closed\n";                break;            } else {                int recvint;                sscanf(recvbuf, "%03d", &recvint);                cout << DEBUG << "recvbuf = " << recvint << "\n";                if (bytesRecv == 3){                cout << BYTES;                printf( "Bytes Recv: %ld\n", bytesRecv );                cout << DEBUG << recvbuf << "\n";                } else {                break;                }            }        }    } else {        cout << DEBUG << "not sending network\n";    }    }    cout << DEBUG << "program termination\n";closesocket( ConnectSocket );system("Pause");WSACleanup();return 0;}