Exemplo n.º 1
0
void CNetRender::SetServer(qint32 portNo)
{
	DeleteClient();
	DeleteServer();
	this->portNo = portNo;
	server = new QTcpServer(this);
	WriteLog("NetRender - starting server.");
	if(!server->listen(QHostAddress::Any, portNo))
	{
		if(server->serverError() == QAbstractSocket::AddressInUseError)
		{
			cErrorMessage::showMessage(
				QObject::tr("NetRender - address already in use.\n\nIs there already a mandelbulber server instance running on this port?"),
				cErrorMessage::errorMessage, gMainInterface->mainWindow);
		}
		else
		{
			cErrorMessage::showMessage(
				QObject::tr("NetRender - SetServer Error:\n\n") + server->errorString(),
				cErrorMessage::errorMessage, gMainInterface->mainWindow);
		}
		deviceType = netRender_UNKNOWN;
	}
	else
	{
		connect(server, SIGNAL(newConnection()), this, SLOT(HandleNewConnection()));
		deviceType = netRender_SERVER;
		WriteLog("NetRender - Server Setup on localhost, port: " + QString::number(portNo));

		if(systemData.noGui)
		{
			QTextStream out(stdout);
			out << "NetRender - Server Setup on localhost, port: " + QString::number(portNo) + "\n";
		}

		status = netRender_NEW;
		emit NewStatusServer();
	}
}
int main( int argc, char *argv[] )
{
	int				iPort;
	int				iSocket;
	
	if( argc != 2 ) {
		printf( "Usage:  demoserver <port #>\n" );
		return 1;
	}
	
	if( geteuid() != 0 ) {
		printf( "Warning:  Tool must be run as EUID = 0 to use Kerberos/GSSAPI\n" );
	}
	
	// if we were relying on Kerberos for this service, we could pre-flight our 
	// service principal before we start the service, or just let GSSAPI determine 
	// at the time of connection
	//
	// we have opted for at time of connection, because we don't know what we
	// are using uses for service principal

	// let's convert the string provided for the port into a number
	iPort = strtol( argv[1], NULL, 10 );
	
	// don't allow privileged ports for this test
	if( iPort < 1024 ) {
		printf( "Error:  Port value must be greater than 1024\n" );
	}
	
	// this demonstration code will wait for an authentication identification code
	//    GSSAPI
	//    CRAM-MD5
	//    cleartext
	// Once method is determined, it will attempt to authenticate the user
	
	iSocket = BindSocket( iPort );
	if( iSocket > 0 ) {
		
		// just informative
		printf( "Listening on port %d\n", iPort );

		// go into a loop until we are killed
		while( 1 ) {

			int	iAccept;
			
			// Accept a TCP connection
			if( (iAccept = accept(iSocket, NULL, 0)) < 0) {
				printf("Error:  Couldn't accept connection");
				continue;
			}
			
			// once we have a connection, go to our request-response loop
			if( HandleNewConnection( iAccept ) == 0 ) {
				
				// if we had no error we could go do something, but in this case we will
				// just close the connection

				close( iAccept );
				iAccept = 0;
				
			} else {

				// close our socket
				close( iAccept );
				iAccept = 0;
			}
		}
		
		// close our socket
		close( iSocket );
		iSocket = 0;

	} else {
		printf( "Error:  Unable to listen on port %d\n", iPort );
		return 1;
	}

	return 0;
}