Пример #1
0
QTcpSocket *Server::getRemoteSocket()
{
    if (remote_server_socket && remote_server_socket->state()==QAbstractSocket::ConnectedState && remote_server_socket->isWritable()) {
        return remote_server_socket;
    }
    if (remote_server_socket) delete remote_server_socket;
    is_init_connection = true;
    remote_server_socket = new QTcpSocket();
    remote_server_socket->connectToHost(QHostAddress(remoteIPAddress), remote_port, QIODevice::ReadWrite);
    if (remote_server_socket->waitForConnected(5000)) {
        connect(remote_server_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
        connect(remote_server_socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
        connect(remote_server_socket, SIGNAL(readyRead()), this, SLOT(recieveData()));
        connect(remote_server_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    } else {
        emit error(tr("Connect error: %1").arg(remote_server_socket->errorString()));
        delete remote_server_socket;
        remote_server_socket = NULL;
    }
    is_init_connection = false;
    if (!remote_server_socket) {
        connectionTimer->start();
    } else {
        connectionTimer->stop();
    }
    return remote_server_socket;
}
Пример #2
0
//--------------------------------------------------------
// Call once per frame (60 times/sec)
void liveConnection::update() {
	
	// Increment step
	if (playing == true) {
		step ++;
		if (step == NUM_STEPS) {
			step = 0;
		}
	}
	
	// send first message in que
	if (message_que.size() > 0) {
		for (int i=0; i<message_que.size(); i++) {
			ofxOscMessage m = message_que[i];
			cout << "PNEXT: " << m.getAddress() << " , " << m.getArgAsString( 0 ) << endl;
		}
		cout << "SENDING: " << message_que[0].getArgAsString( 0 ) << endl;
		sender.sendMessage( message_que[ 0 ] );
		message_que.erase( message_que.begin() );
		cout << "message_que.size(): " << message_que.size() << endl;
//		cout << "NEXT: " << message_que[0].getArgAsString( 0 ) << endl;
		for (int i=0; i<message_que.size(); i++) {
			ofxOscMessage m = message_que[i];
			cout << "NEXT: " << m.getAddress() << m.getArgAsString( 0 ) << endl;
		}
	}
	
	// get messages
	recieveData();
}
Пример #3
0
char* Server::recieveData(){
	//recieves data into a buffer
	char* buffer = (char*) malloc(1000);
	int status = recv(socketFileDescriptor, buffer, 1000, 0);
	//makes damn sure there is a null character in there somewhere
	*(buffer + 999) = '\0';
	*(buffer + status) = '\0';

	//NOTE: RAND IS HERE FOR TESTING PURPOSES. REMOVE IT BEFORE USE 
	if((status < 0)/*||(rand() %10 < 2)*/){//if there was an error
		printf("Recieve has failed. Error: %s\n", strerror(errno));
		free(buffer);
		timeval fakeTime;
		fakeTime.tv_sec = -2;
		sendData(fakeTime);//send a retry request
		return recieveData();
	}		
	if(status == 0){//if the connection has closed from the other end
		printf("Client has closed the connection\n");
		free(buffer);
		char* fakeString = new char();//fakes a return value
		return fakeString;
	}
	if(strchr(buffer, '\n') != NULL){//otherwise, if the string is properly formatted
		return buffer;
	}
}
Пример #4
0
void Server::recieveConnection()
{
    if (is_init_connection) return;

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    QString client_ip = clientConnection->peerAddress().toString();
    quint32 client_ip_int = clientConnection->peerAddress().toIPv4Address();

    emit write_message(tr("New connection from IP: %1").arg(client_ip));

    connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));

    if (client_ip_int == QHostAddress(remoteIPAddress).toIPv4Address() || is_config_mode) {
        if (remote_server_socket && clientConnection != remote_server_socket) {
            delete remote_server_socket;
        }
        remote_server_socket = clientConnection;
        connect(clientConnection, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
        connect(clientConnection, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
        connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recieveData()));
        connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    } else {
        clientConnection->abort();
    }
}
Пример #5
0
gboolean viewRequest_delete (GtkWidget *entry1, gpointer a1)
{
	client_state_t *client = (client_state_t *)a1;
	//request newreq = *(request *)client->info1;

	netMessage_t message;
	bzero(&message, sizeof(netMessage_t));
	message.opcode = REQUEST_REMOVE;
	//message.details = newreq.reqno;
	if(sendData(client->thisClient, &message, sizeof(netMessage_t)) == 0)
	{
		show_Failure(entry1, client->window);
	}
	else
	{
		recieveData(client->thisClient, &message, sizeof(netMessage_t));
		if(message.opcode == SUCCESS_PROCESS)
		{
			show_Success(entry1, client->window);
		}
		if(message.opcode == FAILURE_PROCESS)
		{
			show_Failure(entry1, client->window);
		}
	
	}
	return TRUE;
}
Пример #6
0
void Server::updateLag(){
	timeval fakeTime;
	fakeTime.tv_sec = -3;
	fakeTime.tv_usec = 0;
	char* returnedData;
	timeval sysTime1;
	timeval sysTime2;
	lag = 0;
	for(int i = 0; i< 10; i++){
		
		gettimeofday(&sysTime1, NULL);
		sendData(fakeTime);
		returnedData = recieveData();
		if(strcmp(returnedData, "ping\n")){
			printf("Ping not returned, instead got: %s\n", returnedData);
		}

		gettimeofday(&sysTime2, NULL);
		lag += ((sysTime2.tv_sec - sysTime1.tv_sec)*1000000 + (sysTime2.tv_usec - sysTime1.tv_usec));
	}

	lag /= 10;
	printf("Lag found to be %d us\n", lag);
	lagTime = sysTime2;

}
Пример #7
0
interrupt [TWI] void TWI_interface(void) {
	recieveData();
	switch(index_in) {
	case 'K' :
		i2ckilled = 1;
		index_in = 0;
		break;
	case 'S' :
	        if(Data_in > 10)
	        Data_in = 10;
	        else if(Data_in < 0)
	        Data_in = 0;  
	        maxspeed = Data_in;   
	        index_in = 0;
		break;
	case 'V' :
		getSpeedIndex();	
		index_in = 0;
		break;   
	case 'P' : 
	
		//sendData('P', keypad, TWI_SENSOR_ADDR); 
		 
		//keypad = keypad_oneKey();    //polling
		//sendData('P', keypad, TWI_SENSOR_ADDR);
		index_in = 0;
		break;
	default:
		break;
	}
}
Пример #8
0
void Server::recieveConnection()
{
    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));

    QString client_ip = clientConnection->peerAddress().toString();
    quint32 client_ip_int = clientConnection->peerAddress().toIPv4Address();

    emit write_message(tr("New connection from IP: %1").arg(client_ip));

    if (sockets->contains(client_ip_int)) {
        QTcpSocket *oldClientConnection = (QTcpSocket*) sockets->value(client_ip_int);
        if (oldClientConnection && oldClientConnection->state() != QAbstractSocket::UnconnectedState) {
            oldClientConnection->disconnectFromHost();
        }
        sockets->remove(client_ip_int);
    }

    sockets->insert(client_ip_int, clientConnection);

    connect(clientConnection, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
    connect(clientConnection, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recieveData()));
    connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
}
Пример #9
0
// func working fine
void *enterNetData(void *in)
{
	event e;
	char buffer[256];
	int n;
	char c;
	while(1)
	{
		bzero(buffer,255);
		n = recieveData(buffer,256);
		bcopy(buffer, &e,sizeof(event));
		if(n>0)
		{
			bcopy(&e,buffer , sizeof(event));
			if(e.time == playerno)
			{
				continue;
			}
			if(e.label == 4)
			{
				bcopy(e.buffer1,&coins[e.ball1],sizeof(BALL));
				if(e.ball2 == 1)
				{
					runphytrd = 1;
					chance = 0;
					gamepause = 0;	
				}
				if(playerno == 0)
				{
					sendData(&e,sizeof(event),-1);
				}
			}
			if(e.label == 5)
			{
				gamepause =1 - gamepause;
				if(playerno == 0)
				{
					sendData(&e,sizeof(event),-1);
				}
			}
			if(e.label == 7)
			{
				chance = 1;
				coins[0].pos.x = 0.0;
				coins[0].pos.y = -BOARD_SIDE + LINE_HEIGHT + LINE_WIDTH/2;
			}
		}	
	}
}
Пример #10
0
//--------------------------------------------------------
int liveConnection::live_path(string arg){
	
	ofxOscMessage m;
	m.setAddress( "/live_path" );
	m.addStringArg(arg);
	sender.sendMessage( m );
	
	while (int_buffer == 0) {
		recieveData();
		sleep(0.1);
		//cout << "sleep..." << endl;
	}
	int return_int = int_buffer;
	int_buffer = 0;
	return return_int;
}
bool RoboCupSSLClient::open(bool blocking) {
    qDebug() << "opening";
 if(!udpSocket->bind(QHostAddress(_net_address),_port))
 {
     qDebug() << "coudnt bind";
     return false;
 }

 if(! udpSocket->joinMulticastGroup(QHostAddress(_net_address)))
 {
     qDebug() << "coudnt join";
     return false;
 }


 connect(udpSocket,SIGNAL(readyRead()),this,SLOT(recieveData()));
}
Пример #12
0
void Server::runLoop(){

timeval timeValue;
char* data;
bool resend = false;
int x;
int y;
timeval returnedTime;


	while(true){

		//sleep(1);//for testng only. Otherwise it is way too fast
		if(!resend){
			//NOTE: TO BE REPLACED WITH SYSTEM CALL TO BLAST BUS SOMEHOW
			gettimeofday(&timeValue, NULL);//gets a time value
			
			if(abs(lagTime.tv_sec - timeValue.tv_sec) > 300){
				updateLag();
				gettimeofday(&timeValue, NULL);
			}
		}
		printf("sending data: %ld seconds, %ld microseconds\n", timeValue.tv_sec, timeValue.tv_usec);
		sendData(timeValue);//sends the query time to the laptop
		resend = false;		
		
		data = recieveData();//recieves the response

		if(!strcmp(data, "resend\n")){//if there was a resend request
			printf("Resending old data\n");
			resend = true;
			free(data);
			continue;
		}else{
			if(!strcmp(data, "")){//if there was an error
				printf("Exiting\n");
				free(data);
				return;
			}
			//extracts data from the string
			sscanf(data, "X=%d;Y=%d;s=%ld;us=%ld\n", &x, &y, &returnedTime.tv_sec, &returnedTime.tv_usec);
			passData(x, y, returnedTime);//passes data back to the blast bus
			free(data);
		}
	}
}
Пример #13
0
int main()
{
	int i;
	scanf("%d",&i);
	int portno;
	scanf("%d", &portno);
	char c;
	char s[20];
	
	if( i==0)
	{
		createServer(portno);
	}
	else
	{
		createClient(portno , "ubuntu");
	}
	while(1)
	{
	c = getchar();
	
	if(c == '1')
	{
		printf("\ntransmiiting data\n");
		scanf("%s",s);
		sendData(s,sizeof(s),-1);
	}
	if(c=='2')
	{
		printf("\nrecieving data\n");
		recieveData(s,20);
		printf("this is the string %s",s);
		printf("hi");
	}
	}	
	
}
Пример #14
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("mydb.sqlite");
    db.setPassword("qwerty");
    if(!db.open()){
        QMessageBox::information(this,"Message","Not connected");
    }
    //QString str="create table 'student'('number' integer PRIMARY KEY NOT NULL, 'fio' VARCHAR(36), 'group' integer);";
    /*model = new QSqlTableModel(0, db);
    model->setTable("student2");          // Имя таблицы базы данных.
    model->select();
    ui->tableView->setModel(model);*/
    ui->pushButton->setEnabled(false);
    ui->pushButton_4->setEnabled(false);
    ui->pushButton_5->setEnabled(false);
    ui->pushButton_3->setEnabled(false);
    formd=new DialogUpdate(this);
    form=new Dialog(this);
    forms=new Dialogsearch(this);
    forml=new Dialoglogin(this);
    //connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(onButtonSend()));
    connect(this,SIGNAL(sendData(QString,QString,QString)),formd,SLOT(recieveData(QString,QString,QString)));
    connect(form,SIGNAL(refresh()),this,SLOT(onRefresh()));
    connect(formd,SIGNAL(refresh()),this,SLOT(onRefresh()));
    connect(forml,SIGNAL(sendLogin(QString, QString,QString)),this,SLOT(onLogin(QString,QString,QString)));
    connect(this,SIGNAL(sendTable(QString)),form,SLOT(onTable(QString)));
    connect(this,SIGNAL(sendTable(QString)),formd,SLOT(onTable(QString)));
    connect(this,SIGNAL(sendTable(QString)),forms,SLOT(onTable(QString)));
    QMessageBox::information(this,"Warnign!","You are working in costumer mod");
    ui->listWidget->addItems(db.tables());
    ui->lineEdit->setText("create table 'student'('number' integer PRIMARY KEY NOT NULL, 'fio' VARCHAR(36), 'group' integer)");
}
Пример #15
0
int main( int argc, char *argc[])
{
	int param	  = 0;
	int show_help = 0;
	int mode	  = 0;
	int server_port = RFC1350_PORT;

	/*------- Sockets ----------*/
	struct sockaddr_in server;
	struct sockaddr_in client;
	struct hostent *server_host;
	int	client_socket;

	/*-------- TFTP ------------*/
	int final = 0;
	tftp_rwq_hdr query;
	FILE *file;

	retransmission_time = 5; /* Default seconds value for retransmission*/ 

	while( (param = getopt( argc, argv, "hvrwt:f:H:p:")) != -1)
	{
		switch( (char)param)
		{
			case 'f':
				target_file = optarg;
				break;
			case 'H':
				server_host_name = optarg;
				break;
			case 'p':
				server_port = atoi(optarg);
				break;
			case 'r':
				/* User wants to read data from the server.Flags -r and -w cannot be set
				 * at the same time.*/
				if( mode != 0)
				{
					mode = -1;
				}
				else
				{
					mode = RFC1350_OP_RRQ;
				}
				break;
			case 'w':
				/* User wants to write data to the server. Flags -r and -w cannot be set
				 * at the same time.*/
				if( mode != 0)
				{
					mode = -1;
				}
				else
				{
					mode = RFC1350_OP_WRQ;
				}
				break;
			case 'v':
				/* User wants verbose mode, so the program
				 * must give feedback to the user*/
				verbose = 1;
			case 't':
				retransmission_time = atoi( optarg);
				break;
			case 'h':
				show_help = 1;
				break;
		}
	}

	/*--------------------- param control ------------------*/
	if( target_file == NULL)
	{
		printf( NO_FILE_SET_ERR);
		fflush( stdout);
		show_help = 1;
	}
	if( server_host_name == NULL)
	{
		printf( NO_HOST_SET_ERR);
		fflush( stdout);
		show_help = 1;
	}
	if( mode == 0)
	{
		printf( NO_MODE_SET_ERR);
		fflush( stdout);
		show_help = 1;
	}
	if( mode < 0)
	{
		printf( TWO_MODES_SET_ERR);
		fflush( stdout);
		show_help = 1;
	}
	if( show_help == 1)
	{
		showHelp();
		return -1;
	}

	/*------------------- Sockets initialization ----------------*/
	int		retval = 0;
	client.sin_family	   = AF_INET;
	client.sin_port		   = htons(0);
	client.sin_addr.s_addr = INADDR_ANY;

	client_socket = socket( AF_INET, SOCK_DGRAM, 0);
	if( client_socket == -1)
	{
		printf("%s:%d socket error, %s\n",
				__FILE__, __LINE__, strerror(errno));
		return -1;
	}

	retval = bind( client_socket, (struct sockaddr*)&client, sizeof(client));
	if( retval == -1)
	{
		printf("%s:%d bind error, %s\n",
				__FILE__, __LINE__, strerror(errno));
		return -1;
	}

	server.sin_family	   = AF_INET;
	server.sin_port		   = htons( server_port);
	server_host	= gethostbyname( server_host_name);
	server.sin_addr.s_addr = inet_addr( inet_ntoa(*((struct in_addr*)server_host->h_addr)));
	/*memcpy( (char *)&server.sin_addr, (char *)server_host->h_addr, server_host->h_length);*/

	/*------------------------- Set the packages and send them ----------------------*/
	strcpy( query.filename, target_file);
	strcpy( query.mode, "octet");

	/* Switch actions depending on the mode given by the user*/
	switch( mode)
	{
		case RFC1350_OP_RRQ:
			/* The user wants to read from the server */
			/* Client sends and ACK, server sends data*/
			file = fopen( target_file, "wb"); /* File could not be created, so exit the program*/ 
			if( file == NULL)
			{
				printf(COULD_NOT_CREATE_FILE_ERR);
				fflush( stdout);
				return -1;
			}

			query.opcode = RFC1350_OP_RRQ;
			sendQuery( query, &client_socket, server);
			final = recieveData( file, &server, &client_socket, VERBOSE_SUBJECT_CLIENT);
			if( final == 0)
			{
				printf( "File transfer was successful!\n");
				fflush( stdout);
			}
			break;
		case RFC1350_OP_WRQ:
			/* The user wants to write to the server */
			/* Client sends data, server sends and ACK */
			file = fopen( target_file, "rb");
			if( file == NULL)
			{
				printf( FILE_DOES_NOT_EXITST_ERR);
				fflush( stdout);
				return -1;
			}

			query.opcode = RFC1350_OP_WRQ;
			sendQuery( query, &client_socket, server);
			final = recieveACK( 0, &server, &client_socket, VERBOSE_SUBJECT_CLIENT);
Пример #16
0
Test::Test(QObject *parent) : QObject(parent)
{
    serial = new QSerialPort(this);
    connect(serial, SIGNAL(readyRead()), this, SLOT(recieveData()) );
}