Beispiel #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();
}
Beispiel #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;
}
Beispiel #4
0
void menuDisplay() {
	
	if (!done) {
		if ( choice == 2 )
		{
			sockClient.SendData(player1->getName());
				cout<<"\t--WAIT--"<<endl;
				sockClient.RecvData( recMessage, STRLEN );
				cout<<"Recv > "<<recMessage<<endl;
				player1->setName(name);
				player2->setName(recMessage);
				done = true;
				recvd = true;
		}
		else if ( choice == 1 )
		{
				cout<<"\t--WAIT--"<<endl;
				sockServer.RecvData( recMessage, STRLEN );
				player1->setName(recMessage);
				player2->setName(name);
				cout<<"Recv > "<<recMessage<<endl;
				sockServer.SendData(player2->getName());
				done = true;
				recvd = false;
		}
	}

	  glMatrixMode( GL_MODELVIEW );		// Setup model transformations
	  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  
	  glPushMatrix();
	  drawGLString(-0.5 , 0.5, 1, "Welcome to MiniGolf!");
  	  drawGLString(-0.5 , 0.0, 1, "Press 'b' to Begin");
	  drawGLString(-0.5,  -0.5, 1, "Press 'h' to display High Scores");
	  glPopMatrix();
}
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();
            }
        }
    }
Beispiel #6
0
// display func
void display_obj()
{
  updateCamera();
  if(showScores){
	  showScores = false;
  }
  if (showMenu) {
	  menuDisplay();
  }
  else {
	  glMatrixMode( GL_MODELVIEW );		// Setup model transformations
	  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  
	  glPushMatrix();

	  //draw tiles
	  drawTiles();
  
	  //draw tee

	  glColor3f(0,0,1);
	  glBegin(GL_POLYGON);
	  vector<double> tpos = tee->getPos();
	  glVertex3d(tpos[0]-0.1,tpos[1]+0.01,tpos[2]+0.1);
	  glVertex3d(tpos[0]-0.1,tpos[1]+0.01,tpos[2]-0.1);
	  glVertex3d(tpos[0]+0.1,tpos[1]+0.01,tpos[2]-0.1);
	  glVertex3d(tpos[0]+0.1,tpos[1]+0.01,tpos[2]+0.1);
	  glEnd();
   
	  //draw cup
	  glColor3f(0,0,0);
	  glBegin(GL_TRIANGLE_STRIP);
		for (double i =0; i<180; i++) {
			double x = 0.1 * cos(i);
			double y = 0.1 * sin(i);
			vector<double> hpos = hole->getPos();
			glVertex3d(hpos[0],hpos[1],hpos[2]);
			glVertex3d(hpos[0] + x,hpos[1]+.01,hpos[2]+y);
		}
	  glEnd();

	  char * str;
	  char * str2;
	  char buffer[10];
	  str = itoa(player1->getCurrentScore(), buffer, 10);
	  str2 = itoa(player2->getCurrentScore(), buffer, 10);
	  drawGLString(ball->getPos()[0] , ball->getPos()[1]+0.4, ball->getPos()[2], player1->getName());
	  drawGLString(ball->getPos()[0] , ball->getPos()[1]+0.1, ball->getPos()[2], str);
      drawGLString(ball2->getPos()[0] , ball2->getPos()[1]+0.4, ball2->getPos()[2], player2->getName());
	  drawGLString(ball2->getPos()[0] , ball2->getPos()[1]+0.1, ball2->getPos()[2], str2);

	  glPopMatrix();

	  glPushMatrix();
	  glTranslatef(ball->getPos()[0],ball->getPos()[1],ball->getPos()[2]);
	  glColor3f(1,1,1);
	  glutSolidSphere(BALL_RADIUS,20,16);
	  glDisable(GL_LIGHTING);
	  glDisable(GL_LIGHT0);
	  glColor3f(1,1,0);
	  glBegin(GL_LINES);
	  glVertex3d(0,0,0);
	  glVertex3d(ball_speed*10*ball->getDirection()[0],0,ball_speed*10*ball->getDirection()[2]);
	  glEnd();
	  glEnable(GL_LIGHTING);
	  glEnable(GL_LIGHT0);
	  glPopMatrix();

	  glPushMatrix();
	  glTranslatef(ball2->getPos()[0],ball2->getPos()[1],ball2->getPos()[2]);
	  glColor3f(1,0,0);
	  glutSolidSphere(BALL_RADIUS,20,16);
	  glDisable(GL_LIGHTING);
	  glDisable(GL_LIGHT0);
	  glColor3f(1,1,0);
	  glBegin(GL_LINES);
	  glVertex3d(0,0,0);
	  glVertex3d(ball_speed*10*ball2->getDirection()[0],0,ball_speed*10*ball2->getDirection()[2]);
	  glEnd();
	  glEnable(GL_LIGHTING);
	  glEnable(GL_LIGHT0);
	  glPopMatrix();

	  ball->Update();
	  ball2->Update();
	  if (!posgive) {
		  if ( choice == 2 )
			{			
				//sockClient.SendData(itoa(ball_speed*1000, buffer,10));
				sockClient.SendData(itoa(direction_theta*1000, buffer,10));
			}
			else if ( choice == 1 )
			{
				//sockServer.SendData(itoa(ball_speed*1000, buffer, 10));
				sockServer.SendData(itoa(direction_theta*1000, buffer,10));
			}
			posgive = true;
	  }
	  if (!recvd) {
		  if ( choice == 2 && player2->getTurn())
			{			
				sockClient.RecvData(recPos, STRLEN);
				float dir = atoi(recPos)/1000.;
				ball2->setDirection(cos(dir),0,sin(dir));
				ball2->setSpeed(0.05);
				ball2->setFriction(-0.0005);
				player2->incrementCurrentScore();
				recvd = true;
				shot = true;
			}
		  else if ( choice == 1 && player1->getTurn())
			{
				sockServer.RecvData(recPos, STRLEN);
				float dir = atoi(recPos)/1000.;
				ball->setDirection(cos(dir),0,sin(dir));
				ball->setSpeed(0.05);
				ball->setFriction(-0.0005);
				player1->incrementCurrentScore();
				recvd = true;
				shot = true;
			}
	  } 

	  if (shot) {
		  if ((ball->getSpeed() == 0 && player1->getTurn()) || (ball2->getSpeed() == 0 && player2->getTurn())) {
			  player1->switchTurn();
  			  player2->switchTurn();
	  		  shot = false;		
			  recvd = false;
		  }
	  }

	  win = Physics::checkCollisions(ball, tiles, hole);
	  win = Physics::checkCollisions(ball2,tiles,hole);
	  if (win) {
        //Add score to high score if it beats current score
		L->setScore(L->currLevel , player1->getCurrentScore(), player1->getName());
		//L->displayScores();
		L->writeScores();
        tiles.clear();
		L->currLevel++;
		//level_counter++;
		if (L->currLevel==NUM_LEVELS)
			L->currLevel = 0;
		init();
		tiles = FileReader::parseLevelFile(levels[L->currLevel], tiles, ball, ball2, tee, hole);
	  }
  }
  glFlush();				// Flush OpenGL queue
  glutSwapBuffers();			// Display back buffer
}
Beispiel #7
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;
        }
    }
}