Exemple #1
0
int main(int argc, char **argv){
	int port;
	string ipAddress;
	bool done = FALSE;
	char recMessage[STRLEN];
    char sendMessage[STRLEN];
	string input;
    
    port = atoi(argv[1]);
	//Client
	cout<<"Enter an IP address, 127.0.0.1 is the loopback address"<<endl;
	cin>>ipAddress;
	ClientSocket sockClient;
	cout<<"ATTEMPTING TO CONNECT..."<<endl;
	sockClient.ConnectToServer( ipAddress.c_str(), port );
	//Connected
	//sockClient.RecvData(recMessage, STRLEN);//Get ye here yon connection message
	//cout << recMessage<<endl;
	while ( !done ){     
		//sockClient.GetAndSendMessage(); //Sends input message
		input = sockClient.UserInput("test.txt");
		cout<<"\t--WAIT--"<<endl;
		sockClient.RecvData( recMessage, STRLEN );
		cout<<"Recv > "<<recMessage<<endl;
		/*
		if ( strcmp( recMessage, "end" ) == 0 || strcmp( sendMessage, "end" ) == 0 ){
			done = true;
		}
		*/
		
	}
	sockClient.CloseConnection();
}
Exemple #2
0
int main(){
	int port = 8888;
	string ipAddress;
	bool done;
	char recMessage[STRLEN];
    char sendMessage[STRLEN];

	//Client
	cout<<"Enter an IP address, 127.0.0.1 is the loopback address"<<endl;
	cin>>ipAddress;
	ClientSocket sockClient;
	cout<<"ATTEMPTING TO CONNECT..."<<endl;
	sockClient.ConnectToServer( ipAddress.c_str(), port );
	//Connected
	while ( !done ){     
		sockClient.GetAndSendMessage();
		cout<<"\t--WAIT--"<<endl;
		sockClient.RecvData( recMessage, STRLEN );
		cout<<"Recv > "<<recMessage<<endl;
		if ( strcmp( recMessage, "end" ) == 0 || strcmp( sendMessage, "end" ) == 0 ){
			done = true;
		}
	}
	sockClient.CloseConnection();
}
int main()
{
        //Variables definitions:
        string ipAddress;
        int port = 54321;
        char recMessage[STRLEN];
        string userId;
        string command;

        cout << "Enter the Server's ip address: ";
        cin  >> ipAddress;

        ClientSocket sockClient;
        sockClient.ConnectToServer(ipAddress.c_str(), port);
        sockClient.RecvData( recMessage, STRLEN );

        if (checkName("LOGIN", recMessage)== false) {
                sockClient.CloseConnection();
        }else{
                cout << "Please enter the user-identifier: ";
                cin  >> userId;
                sockClient.SendData(stringToCharArray(userId));
                cin.clear();
                cin.ignore(1000, '\n'); //Removing the /n character for the next getline.
        }

        sockClient.RecvData( recMessage, STRLEN );

        if (checkName("WELCOME", recMessage)== false) {
                sockClient.CloseConnection();
                return 0;
        }else{

                while(true) {
                        cout << "Please enter the command: ";
                        getline(cin, command);
                        sockClient.SendData(stringToCharArray(command));
                        if (checkCommand(command, sockClient) == false) break;
                }

        }


        return 0;
}
void serverLoop() {
    int port = 666;
    //char *ipAddress = "127.0.0.1";
    string ipAddress;
    cout<<"1) Host server"<<endl;
    cout<<"2) Join server"<<endl;
    cout<<"3) Quit"<<endl;
    cin>>choice;
    if ( choice == 3 )
        exit(0);
    else if ( choice == 2 )
    {
        //Client
        cout<<"Enter an IP address, 127.0.0.1 is the loopback address"<<endl;
        cin>>ipAddress;
        cout<<"ATTEMPTING TO CONNECT..."<<endl;
        sockClient.ConnectToServer( ipAddress.c_str(), port );
        //Connected
		glutMainLoop();			// Enter GLUT main loop
        sockClient.CloseConnection();
    }
int main()
{
    int port = 8888;
    string ipAddress;
    char recMessage[STRLEN];

    //Client
    cout<<"Enter an IP address of server: ";
    cin>>ipAddress;

    ClientSocket sockClient;

    //connect to server
    cout<<"ATTEMPTING TO CONNECT..."<<endl << endl;
    sockClient.ConnectToServer( ipAddress.c_str(), port );

    //Recieve message from server (authentication request)
    sockClient.RecvData( recMessage, STRLEN );
    cout << recMessage;

    //send request to server (password)
    sockClient.GetAndSendMessage();

    //server response (valid or invalid password)
    sockClient.RecvData( recMessage, STRLEN );
    cout << recMessage << endl;

    string temp = recMessage;

    if (temp == "\t***INVALID CREDENTIALS****\nDISCONNECTING")
    {
        sockClient.CloseConnection();
        system("pause");
        return 0;
    }

    while (true)
    {
        //server list of actions to take
        sockClient.RecvData( recMessage, STRLEN );
        cout << recMessage;

        //client response of action
        sockClient.GetAndSendMessage();

        //server responds to selected action
        sockClient.RecvData( recMessage, STRLEN );

        //if quit is entered, server sends "Goodbye"
        string temp = recMessage;
        if (temp == "INVALID ENTRY")
        {
            cout << recMessage << endl;
        }
        else if (temp == "Goodbye")
        {
            sockClient.CloseConnection();
            cout << endl << "\t***DISCONNECTED" << endl;
            system("pause");
            return 0;
        }
        else if (temp == "List")
        {
            sockClient.RecvData( recMessage, STRLEN );
            temp = recMessage;
            if (temp != "***No files in directory"){
                int num = atoi(recMessage);
                for (int i = 0; i < num; i++)
                {
                    sockClient.RecvData( recMessage, STRLEN );
                    cout << endl <<"\t" << recMessage;
                }
                cout << endl << endl;
            }
        }

        else if(temp == "send")
        {
            cout << "Enter file name: ";
            string fileName;
            cin >> fileName;

            char *cfilename = new char[fileName.length()+1];
            strcpy(cfilename,fileName.c_str());
            sockClient.SendData(cfilename);

            sockClient.RecvData(recMessage, STRLEN);
            temp = recMessage;
            if(temp == "fail"){
                cerr << "\tFile does not exist" << endl;
            }
            else if(temp == "okay"){
                ofstream fout;
                fout.open(fileName.c_str());
                sockClient.RecvData(recMessage, STRLEN);
                //cout << "..." << recMessage << endl;
                string txt(recMessage);
                while(txt != "EOFEOFEOFEOFEOFEOF"){
                    fout << txt << endl;
                    sockClient.RecvData(recMessage, STRLEN);
                    txt = recMessage;
                }
                fout.close();
            }
        }
    }
Exemple #6
0
int main(int argc, char* argv[])
{
    int port = atoi(argv[1]);
    //char *ipAddress = "127.0.0.1";
    string ipAddress;
    bool done = false;
    char recMessage[STRLEN];
    char sendMessage[STRLEN];
    string input;
    string line;
    string fileToCopy;

    cout<<"Enter an IP address, 127.0.0.1 is the loopback address"<<endl;
    cin>>ipAddress;
    ClientSocket sockClient;
    cout<<"ATTEMPTING TO CONNECT..."<<endl;
    sockClient.ConnectToServer( ipAddress.c_str(), port );
    //Connected

    //Recieving login
    sockClient.RecvData( recMessage, STRLEN ); //rec

    cout<< "Recv > " << recMessage <<endl;

    //sockClient.GetAndSendMessage();
    char message[STRLEN];
    cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
    cout<<"Send > ";
    cin.get( message, STRLEN );
    sockClient.SendData( message );

    sockClient.RecvData( recMessage, STRLEN ); //rec
    cout<< "Recv > " << recMessage <<endl;

    while ( true )
    {
        //sockClient.GetAndSendMessage();
        char message[STRLEN];
        cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
        cout<<"Send > ";
        cin.get( message, STRLEN );
        sockClient.SendData( message );
        cout << "input is " << message << endl;

        input = string(message).substr(0,4);
        if(string(message).size() > 4)
            fileToCopy = string(message).substr(5);
        cout << "input is " << input << endl;
        cout << "File to copy is " << fileToCopy << endl;
        cout<<"\t--WAIT--"<<endl;


       if(input == "SEND")
        {

            //recMessage[STRLEN] = ' ';
            ofstream fout;
            fout.open(fileToCopy.c_str(), fstream::app);

            do
            {
                cout << "FIRST" << recMessage << endl;
                memset(&recMessage, '\0', sizeof(recMessage));
                cout << "SECOND" << recMessage << endl;
                sockClient.RecvData( recMessage, STRLEN );
                cout << "THIRD" << recMessage << endl;
                if(recMessage != "EOF")
                {
                    fout << recMessage << endl;
                    //memset(recMessage, 0, sizeof(char)*STRLEN);
                }
            }while(strcmp(recMessage, "EOF") == 0);//end eof

        }

        else if(input == "LIST")
        {
            sockClient.RecvData(recMessage, STRLEN);
            cout << recMessage;
            cout << endl;
        }
        else
        {
            cout << "Error" << endl;
        }
    }
}