Example #1
0
PlacesEngine::PlacesEngine(QObject *parent, const QVariantList &args)
    : Plasma::DataEngine(parent, args)
{
    connect(&m_placesModel, SIGNAL(modelReset()),
            this, SLOT(modelReset()));
    connect(&m_placesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this, SLOT(dataChanged(QModelIndex,QModelIndex)));
    connect(&m_placesModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
            this, SLOT(placesAdded(QModelIndex,int,int)));
    connect(&m_placesModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
            this, SLOT(placesRemoved(QModelIndex,int,int)));

    sendAllData();
}
Example #2
0
MyServer::MyServer(int port, QWidget *parent) : QWidget(parent), shipCounter(0), shipIndexCounter(0)
{
    server = new QTcpServer(this);
    if(!server->listen(QHostAddress::Any , port)){
        messageLabel -> setText("Port error");
        server->close();
        return;
    }

    isClientConnected = false;

    qsrand(QTime::currentTime().msec());
    screen = QApplication::desktop()->screenGeometry();

    connect(server, SIGNAL(newConnection()), this, SLOT(slotNewConnection()));

    createGui();

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(sendAllData()));

}
Example #3
0
int main(int argc, char *argv[])
{
	char hostname[100];
	char buf[BUFFERSIZE] = {};
	char name[20]={}, message[MESSGSIZE]={};
	int option=1;
	char optionStr[5];
	int sd;
	int port;
	int count;
	struct sockaddr_in pin;
	struct hostent *hp;

	/* check for command line arguments */
	if (argc != 3)
	{
		printf("Usage: client host port\n");
		exit(1);
	}

	/* get hostname and port from argv */
	strcpy(hostname, argv[1]);
	port = atoi(argv[2]);

	/* create an Internet domain stream socket */
	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
		perror("Error creating socket");
		exit(1);
	}

	/* lookup host machine information */
	if ((hp = gethostbyname(hostname)) == 0) {
		perror("Error on gethostbyname call");
		exit(1);
	}

	/* fill in the socket address structure with host information */
	memset(&pin, 0, sizeof(pin));
	pin.sin_family = AF_INET;
	pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
	pin.sin_port = htons(port); /* convert to network byte order */


	printf("Connecting to %s:%d\n\n", hostname, port); 

   /* connect to port on host */
   if (connect(sd,(struct sockaddr *)  &pin, sizeof(pin)) == -1) {
		perror("Error on connect call");
		exit(1);
	}

	printf("\nEnter your name: "); 
	fgets(name, NAMELENGTH, stdin);
	name[strlen(name)-1] = '\0';

	/* send the user's name to the server */ 
	if ( (count = sendAllData(sd, name, strlen(name))) == -1) 
	{
		perror("Error on write call");
		exit(1);
	}

	/* read login status from the server */
	if((count = receiveAllData(sd, buf, BUFFERSIZE)) == -1)
	{
		perror("Error on read call");
		exit(1);
	}
	/*Check the login status and exit if login failed.*/
	if (strcmp(buf,"LOGIN_FAIL") == 0)
	{
		printf("\nYou are already logged from another terminal. Please logout from that terminal and try again.\n");
		close(sd);
		exit(1);
	}
	
	while(option !=7)
	{
		printf("\n\n1. Display the names of all known users.");
		printf("\n2. Display the names of all currently connected users.");
		printf("\n3. Send a text message to a particular user.");
		printf("\n4. Send a text message to all currently connected users.");
		printf("\n5. Send a text message to all known users.");
		printf("\n6. Get my messages.");
		printf("\n7. Exit.\n");
		printf("Enter your choice: ");
		fgets(optionStr, 5, stdin);
		optionStr[strlen(optionStr)-1] = '\0';
		option = atoi(optionStr);
		
		
		if(option>7 || option <1)
		{
			printf("Invalid option selected.\n");
			continue;
		}
		
		/* Send the option selected to the server */
		sprintf(buf,"%d",option);
		if((count = sendAllData(sd, buf, strlen(buf))) == -1)
		{
			perror("Error on write call");
			exit(1);
		}
		
		/* Perform the operation requested*/
		switch(option)
		{
			case 1:
				/* Receive the response by server for that option */		
				if((count = receiveAllData(sd, buf, BUFFERSIZE)) == -1)
				{
					perror("Error on read call");
					exit(1);
				}
				printf("\nKnown users:\n");
				printData(buf);
				break;
			case 2:
				/* Receive the response by server for that option */		
				if((count = receiveAllData(sd, buf, BUFFERSIZE)) == -1)
				{
					perror("Error on read call");
					exit(1);
				}
				printf("\nCurrently connected users:\n");
				printData(buf);
				break;
			case 3:
				printf("\nEnter the user name: ");
				fgets(name, NAMELENGTH, stdin);
				name[strlen(name)-1] = '\0';
				printf("\nEnter your message: ");
				fgets(message, MESSGSIZE, stdin);
				message[strlen(message)-1] = '\0';
				sprintf(buf,"%s|%s", name, message);
				if((count = sendAllData(sd, buf, strlen(buf))) == -1)
				{
					perror("Error on write call");
					exit(1);
				}
				break;
			case 4:
				printf("\nEnter your message: ");
				fgets(message, MESSGSIZE, stdin);
				message[strlen(message)-1] = '\0';
				if((count = sendAllData(sd, message, strlen(message))) == -1)
				{
					perror("Error on write call");
					exit(1);
				}
				break;
			case 5:
				printf("\nEnter your message: ");
				fgets(message, MESSGSIZE, stdin);
				message[strlen(message)-1] = '\0';		
				if((count = sendAllData(sd, message, strlen(message))) == -1)
				{
					perror("Error on write call");
					exit(1);
				}
				break;
			case 6:
				/* Receive the response by server for that option */		
				if((count = receiveAllData(sd, buf, BUFFERSIZE)) == -1)
				{
					perror("Error on read call");
					exit(1);
				}
				if(strcmp(buf,"")==0)
				{
					printf("\nYou do not have any unread messages.\n");
				}
				else
				{
					printf("\nYour messages:\n");
					printData(buf);
				}
				break;
			case 7:
			default:
				if((count = receiveAllData(sd, buf, BUFFERSIZE)) == -1)
				{
					perror("Error on read call");
					exit(1);
				}
				if(strcmp(buf, "LOGGED_OUT")==0)
					printf("\nYou have successfully logged out\n");
				option = 7;
				break;
		}
	}


	/* close the socket */
	close(sd);
}
__interrupt void Timer_A (void)
{
	CCR0 += 10000; //Decrease this to increase rate that values are checked
    sendAllData();
}