Ejemplo n.º 1
0
NetController::NetController(QObject *parent) :
    QObject(parent)
{
    mainserver=new QTcpServer(this);
    //tsock = new QTcpSocket(this);
    connect(mainserver,&QTcpServer::newConnection,this,&NetController::rr);
    //qDebug() << "bind \n";
    startserver();
}
Ejemplo n.º 2
0
int main(int argc, char * argv[])
{

	int sockfd;
    int newsockfd;
	char readbuf[SIZE];
	char *read_request=malloc(MEMORYSIZE);
    long int port_num=0,default_port=9000;

    
    struct sigaction sa,sa_usr1;
    sa.sa_flags=0;
    sa.sa_handler=handle_signal;

    if(sigaction(SIGINT,&sa,NULL)==-1)
        exit(EXIT_FAILURE);
    if(sigaction(SIGTERM,&sa,NULL)==-1)
        exit(EXIT_FAILURE);

    sa_usr1.sa_flags=0;
    sa_usr1.sa_handler=handle_sigusr1;
    if(sigaction(SIGUSR1,&sa_usr1,NULL)==-1)
        exit(EXIT_FAILURE);
    
    printf("%d\n",getpid());
    strategy=getinput(argc,argv,directory_name); 
    printf("%d\n",portno);
    sockfd=startserver(portno);
    time(&server_start_time);
    //printf(ctime(&server_start_time));
    if(strategy=='w')
    {
        //printf("Calling thread pool with %d %d\n",*thread_num,*buf_size);
//        active=0;
        dispatch_to_thread_pool(sockfd,directory_name);
    }
    else
    {
        while(active)
        {
//        check=1;
            newsockfd=accept(sockfd,(struct sockaddr*)NULL,NULL);
            if(newsockfd<0)
            {
                continue;
            }
            request_count++;
            if(newsockfd>0)
                dispatch_connection(read_request,strategy,newsockfd,directory_name);
        }
     }
	
	free(read_request);
    close(sockfd);
	printf("graceful exit\n");
    return(0);
}
Ejemplo n.º 3
0
int main( int argc , char *argv[] )
{
	int ls , cs ;

	ls = startserver( argc , argv ) ;
	while( (cs = accept( ls , NULL , NULL )) > 0 )
        handlerequest( cs ) ; 
	perror( "Accept" ) ;
}
 virtual void setup(int& roudnds_,int fourtytwo_,int random_) {
   startserver();
   clientsock = connectclientsocket();
   configureclientsocket(clientsock);
   
   std::string m = mode();
   int rc = ::send(clientsock,m.c_str(),m.size(),0);
   SOCK_ERRORCHECK(rc,"send");
 }
Ejemplo n.º 5
0
static int startserver_orbust(int *pipefd)
{
	int rc;

	while ((rc=startserver(pipefd)) < 0)
	{
		sleep(5);
	}
	return rc;
}
Ejemplo n.º 6
0
int
main() {
    startserver();

    // run client code
    clientthread();

    // stop server
    stopserver();

    return 0;
}
Ejemplo n.º 7
0
bool Cserver::init()
{
	/* init winsock */
	WSADATA data;
	int res=WSAStartup(0x0002,&data);
	if(res!=0) 
	{ 
		DEBUG("WSAStartup failed\n");
		MessageBox(NULL,"Could not initialize Windows Sockets.\n"
			"Note that this program requires WinSock 2.0 or higher.","WinLIRC",MB_OK);
		return false;
	}

	/* begin password stuff */
	CRegKey key;
	bool haveKey=true;
	if(key.Open(HKEY_CURRENT_USER,"Software\\LIRC")!=ERROR_SUCCESS) //First try HKCU, then HKLM
		if(key.Open(HKEY_LOCAL_MACHINE,"Software\\LIRC")
		   !=ERROR_SUCCESS)
		{
			haveKey=false;
			DEBUG("warning: can't open Software\\LIRC key");
		}

	char s[512];
	DWORD len=512;
	if(!haveKey || key.QueryValue(s,"password",&len)!=ERROR_SUCCESS)
		password.Empty();
	else
		password=s;
	/* end password stuff */
	DWORD x;
	if(!haveKey || key.QueryValue(x,"tcp_port")!=ERROR_SUCCESS)
		tcp_port=IR_PORT;
	else
		tcp_port=x;
	if(startserver()==false)
		return false;

	/* start thread */
	/* THREAD_PRIORITY_IDLE combined with the REALTIME_PRIORITY_CLASS */
	/* of this program still results in a really high priority. (16 out of 31) */
	if((ServerThreadHandle=
		AfxBeginThread(ServerThread,(void *)this,THREAD_PRIORITY_IDLE))==NULL)
	{
		DEBUG("AfxBeginThread failed\n");
		return false;
	}	
    
	return true;
}
Ejemplo n.º 8
0
int main(int argc,char **argv)
{
  int n;

  for (n=0;n<HARD_USERLIMIT;n++)
    pchild[n]=(child_t *)NULL;
  for (n=0;n<LOGININFO_COUNT;n++)
    logininfo[n]=(logininfo_t *)NULL;

  parsecmdline(argc,argv);

  while (reinit) {
    reinit=0;
    preinit();
    
    if (readini()<0) exit(1);
    if (!checkconf()) exit(1);
    
    do_writepidfile();
    do_changeroot();

    if (daemonmode) {
      if (fork()==0) startserver();
      exit(0); /* better be sure */
    } else startserver();
    for (n=0;n<maxusers;n++)
      if (pchild[n]) {
	free(pchild[n]);
	pchild[n]=(child_t *)NULL;
      }
    for (n=0;n<logininfocount;n++)
      if (logininfo[n]) {
	free(logininfo[n]);
	logininfo[n]=(logininfo_t *)NULL;
      }
  }
  exit(0);
}
Ejemplo n.º 9
0
main(){
struct sockaddr_in *clientaddr;
char *port="10000";
int c;

c=sizeof(struct sockaddr_in);
startserver(port);

int client;
while(client){

client= accept(listenfd,(struct sockaddr *)&clientaddr, &c);
respond(client);

}

}
void ConsolidatedTests::CreateAndReleaseTests()
{
    beginTest("Create and Release Tests");
    initialize_field_manager(4);
    startserver(65002);
    
    register_callbacks(join_game,
                       leave_game,
                       take_cell,
                       get_size,
                       get_cell_player,
                       is_there_a_winner);
    
    Thread::sleep(5);
    stopserver();
    release_field_manager();
    Thread::sleep(50);
}
Ejemplo n.º 11
0
int main(int argc, char *argv[])
{
	pid_t pid;

	int listenfd;
	int connfd;
	
	socklen_t len;
	struct sockaddr_in cliaddr;

	loadconf("miniftp.conf");
	printf("server_addr: %s\n", server_addr);
	printf("server_port: %d\n", server_port);
	printf("server_max_connfd: %d\n", server_max_connfd);


	if (geteuid() != 0)//need root privilege.
	{
		printf("please use root privilege.\n");
		exit(0);
	}

	listenfd = startserver(NULL, 7777);

	len = sizeof(cliaddr);
	if ((connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &len)) == -1)
		perror("accept");
	pid = fork();
	if (pid == -1)
		perror("fork");
	else if (pid == 0)//child
	{
		startsession(connfd);
	}

	wait(NULL); //wait for child to terminate.

	return 0;
}
Ejemplo n.º 12
0
void *mang_all(void *portno)
{
int port=atoi((char *)portno);
int syscall,noworr,valid,side;int netinfo[2];
char *ipaddress=(char *)malloc(20*(sizeof(char)));;
pthread_t man_left,man_top,man_right,man_bottom;
char buffer[256];
while(1)		//better condition??
	{
	bzero(buffer,256);
	printf("WAITING!\n");
	startserver(port,netinfo);
	noworr=recv(netinfo[0],buffer,256,0);//client wrote its position relative to this comp and whether it has game info and machine name
	valid=checkpos(buffer,&side,ipaddress);
	//extract side from buffer
	printf("neti==%d\n",netinfo[0]);
	if(valid==-1)
		{
		sprintf(buffer,"0");
		noworr=send(netinfo[0],buffer,strlen(buffer),0);
		}
	else if(valid==1)
		{
		//for new client connecting to this as server
		sendgameinfo(buffer);
		noworr=send(netinfo[0],buffer,strlen(buffer),0);		//check for buffer overflow
		//updategameinfo(side,syscall);			//playercount
		gameinfo.side[side].sock=netinfo[0];
		gameinfo.side[side].there=occupied;
		strcpy(gameinfo.side[side].ip,ipaddress);
		gameinfo.playercount++;

		printf("side===%d\n",side);
		switch(side)
			{
			case left: {pthread_create(&man_left,NULL,manl,"1");break;}
			case right: {pthread_create(&man_right,NULL,manr,"3");break;}
			case top: {pthread_create(&man_top,NULL,mant,"2");break;}
			case bottom: {pthread_create(&man_bottom,NULL,manb,"0");break;}

			}
		}
	else if(valid==2)
		{
		// for client is already connected to some comp. thus having game info
		gameinfo.side[side].sock=netinfo[0];
		gameinfo.side[side].there=occupied;
		strcpy(gameinfo.side[side].ip,ipaddress);

		gameinfo.playercount++;

		switch(side)
			{
			case left: {pthread_create(&man_left,NULL,manl,"1");break;}
			case right: {pthread_create(&man_right,NULL,manr,"3");break;}
			case top: {pthread_create(&man_top,NULL,mant,"2");break;}
			case bottom: {pthread_create(&man_bottom,NULL,manb,"0");break;}
			}
		}
	}
}
Ejemplo n.º 13
0
int main(int argc, char *argv[])
{
	const char *action = NULL;
	struct sigaction sa;

	ARGBEGIN {
	case 'l':
	case 'r':
	case 's':
		if (writefifo(fifofile, ARGC()) == -1)
			die("cannot write to fifo\n");
		exit(EXIT_SUCCESS);
	default:
		usage();
	} ARGEND;
	
	if (argc > 1)
		usage();

	if (getuid() != 0)
		die("only root can run this program\n");
	if (getlock(pidfile) == -1)
		die("cannot get exclusive lock\n");
	if (createfifo(fifofile, 0666) == -1)
		die("cannot create fifo\n");

	sa.sa_flags = SA_NOCLDSTOP;
	sa.sa_handler = catchsignal;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGCHLD, &sa, NULL);

	signal(SIGINT, catchsignal);
	signal(SIGHUP, catchsignal);
	signal(SIGQUIT, catchsignal);
	signal(SIGTERM, catchsignal);
	signal(SIGKILL, catchsignal);
	signal(SIGPIPE, catchsignal);

	startserver(server, display);
	do {
		const char *user, *pass;
		char c;

		if (autologin && authenticate(autologin, NULL)) {
			autologin = NULL;
			goto login;
		}

		creategui();
		do {
			rungui(&user, &pass);
			if (!strcmp(user, "reboot"))
				action = rebootcmd;
			else if (!strcmp(user, "shutdown"))
				action = shutdowncmd;
		} while (!action && !authenticate(user, pass));
		destroygui();

		if (action)
			break;

login:		childpid = startsession(child, env);

		c = readfifo(fifofile);
		if (c == 'r')
			action = rebootcmd;
		else if (c == 's')
			action = shutdowncmd;
		else if (!c)
			while (childpid)
				sleep(10);

		stopsession();
	} while (!action);
	signal(SIGINT, SIG_IGN);
	signal(SIGHUP, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
	signal(SIGTERM, SIG_DFL);
	signal(SIGKILL, SIG_DFL);
	signal(SIGCHLD, SIG_DFL);
	signal(SIGPIPE, SIG_IGN);
	stopserver();

	removefifo(fifofile);
	removelock(pidfile);

	return system(action);
}
Ejemplo n.º 14
0
void Cserver::ThreadProc(void)
{
	if(server==INVALID_SOCKET) return;
	int i;
	
	CEvent ServerEvent;
	CEvent ClientEvent[MAX_CLIENTS];
#define MAX_DATA 1024	// longest message a client can send
	char ClientData[MAX_CLIENTS][MAX_DATA];
	char toparse[MAX_DATA];
	HANDLE events[MAX_CLIENTS+2];
	bool server_running=true;
	
	WSAEventSelect(server,ServerEvent,FD_ACCEPT);

	for(i=0;i<MAX_CLIENTS;i++) ClientData[i][0]=0;
	
	for(;;)
	{		
		int count=0;
		events[count++]=ServerThreadEvent;
		if(server_running) events[count++]=ServerEvent;
		for(i=0;i<MAX_CLIENTS;i++)
			if(clients[i]!=INVALID_SOCKET)
				events[count++]=ClientEvent[i];
		
		unsigned int res=WaitForMultipleObjects(count,events,FALSE,INFINITE);
		if(res==WAIT_OBJECT_0)
		{
			DEBUG("ServerThread terminating\n");
			AfxEndThread(0);
			return;
		}
		else if(server_running && res==(WAIT_OBJECT_0+1))
		{
			for(i=0;i<MAX_CLIENTS;i++)
				if(clients[i]==INVALID_SOCKET) break;
			if(i==MAX_CLIENTS)
			{
				DEBUG("Should have at least 1 empty client, but none found\n");
				continue;
			}
			
			clients[i]=accept(server,NULL,NULL);
			if(clients[i]==INVALID_SOCKET)
			{
				DEBUG("accept() failed\n");
				continue;
			}

			WSAEventSelect(clients[i],ClientEvent[i],FD_CLOSE|FD_READ);
			ClientEvent[i].ResetEvent();
			ClientData[i][0]='\0';
			DEBUG("Client connection %d accepted\n",i);

			for(i=0;i<MAX_CLIENTS;i++)
				if(clients[i]==INVALID_SOCKET) break;
			if(i==MAX_CLIENTS)
			{
				DEBUG("Server full.  Closing server socket.\n");
				stopserver();
				server_running=false;
			}
		}
		else /* client closed or data received */
		{
			count=server_running?2:1;
			for(i=0;i<MAX_CLIENTS;i++)
			{
				if(clients[i]!=INVALID_SOCKET)
				{
					if(res==(WAIT_OBJECT_0+(count++)))
					{
						/* either we got data or the connection closed */
						int curlen=strlen(ClientData[i]);
						int maxlen=MAX_DATA-curlen-1;
						int bytes=recv(	clients[i], ClientData[i]+curlen, maxlen, 0);
						if(bytes==0 || bytes==SOCKET_ERROR)
						{
							/* Connection was closed or something's screwy */
							closesocket(clients[i]);
							clients[i]=INVALID_SOCKET;
							DEBUG("Client connection %d closed\n",i);

							if(server_running==false)
							{
								DEBUG("Slot open.  Restarting server.\n");
								if(startserver()==true)
								{
									WSAEventSelect(server,ServerEvent,FD_ACCEPT);
									server_running=true;
								}
								else
								{
									DEBUG("Server failed to restart.\n");
									stopserver();
								}
							}
						}
						else /* bytes > 0, we read data */
						{
							ClientData[i][curlen+bytes]='\0';
							char *cur=ClientData[i];
							for(;;) {
								char *nl=strchr(cur,'\n');
								if(nl==NULL) {
									if(cur!=ClientData[i]) 
										memmove(ClientData[i],cur,strlen(cur)+1);
									break;
								} else {
									*nl='\0';
									// ----------------------------
									// Do something with the received line (cur)
									DEBUG("Got string: %s\n",cur);
									LRESULT copyDataResult=false;
									char *response=NULL;
									strcpy(toparse,cur);
									char *command=NULL;
									char *remotename=NULL;
									char *buttonname=NULL;
									char *repeats=NULL;
									if (toparse) command=strtok(toparse," \t\r");
									if (!command) //ignore lines with only whitespace
									{
										cur=nl+1;
										break;
									}
									else if (stricmp(command,"VERSION")==0)
									{

										if (strtok(NULL," \t\r")==NULL)
										{
											copyDataResult = true;
											response = new char[strlen(id)+9];
											if (response) sprintf(response,"DATA\n1\n%s\n",id);
										}
										else copyDataResult=-100;
									}
									else if (stricmp(command,"LIST")==0)
									{
										remotename=strtok(NULL," \t\r");
										struct ir_remote *all=global_remotes;
										int n=0;
										if (!remotename)
										{
											copyDataResult = true;
											while(all)
											{
												n++;
												all=all->next;
											}
											if (n!=0)
											{
												response = new char[n*(LINE_LEN+2)+7];
												sprintf(response,"DATA\n%d\n",n);
												all=global_remotes;
												while(all)
												{
													strcat(response,all->name);
													strcat(response,"\n");
													all=all->next;
												}
											}
										}
										else
										{
											while (all!=NULL && stricmp(remotename,all->name)) all=all->next;
											if (all)
											{
												copyDataResult = true;
												struct ir_ncode *allcodes=all->codes;
												while (allcodes->name)
												{
													n++;
													allcodes++;
												}
												if (n!=0)
												{
													response = new char[n*(LINE_LEN+2)+7];
													sprintf(response,"DATA\n%d\n",n);
													allcodes=all->codes;
													while(allcodes->name)
													{
														strcat(response,allcodes->name);
														strcat(response,"\n");
														allcodes++;
													}
												}
											}
											else copyDataResult=-1; //unknown remote
										}
									}

									else if (!password.IsEmpty() && !password.CompareNoCase(command)) //only accept commands if a password is set and matches
									{
										CString incoming = (LPCSTR) (cur);
										int j=incoming.FindOneOf(" \t")+1;
										remotename=strtok(NULL," \t\r");
										if (remotename==NULL)
										{
											response = new char[14];
											if (response) sprintf(response,"DATA\n1\nremote missing\n");
										}
										else 
										{
											buttonname=strtok(NULL," \t\r");
											if (buttonname==NULL)
											{
												response = new char[12];
												if (response) sprintf(response,"DATA\n1\ncode missing\n");
											}
											else
											{

												HWND pOtherWnd = FindWindow(NULL, "WinLirc");
												if (pOtherWnd)
												{
													COPYDATASTRUCT cpd;
													cpd.dwData = 0;
													cpd.cbData = strlen(&cur[j]);
													cpd.lpData = (void*)&cur[j];
													copyDataResult = SendMessage(pOtherWnd,WM_COPYDATA,NULL,(LPARAM)&cpd);
												}
											}
										}
									}
									else
									{
										response = new char[strlen(command)+26];
										if (response) sprintf(response,"DATA\n1\n%s%s\n","unknown command: ",command);
									}
									if (copyDataResult==-1)
									{
										response = new char[strlen(remotename)+25];
										if (response) sprintf(response,"DATA\n1\n%s%s\n","unknown remote: ",remotename);
									}
									else if (copyDataResult==-2)
									{
										response = new char[strlen(buttonname)+23];
										if (response) sprintf(response,"DATA\n1\n%s%s\n","unknown code: ",buttonname);
									}
									else if (copyDataResult==-100)
									{
											response = new char[15];
											if (response) sprintf(response,"DATA\n1\nbad send packet\n");
									}
									reply(cur,i,copyDataResult==1,response);
									if (response) delete(response);
									cur=nl+1;
								}
							}
						
						}

						break;
					}
				}
			}
		}
	}
}
void ConsolidatedTests::TwoPlayersSimple()
{
    beginTest("Two Players Simple");
    initialize_field_manager(4);
    set_delay(0);
    startserver(65002);
    
    register_callbacks(join_game,
                       leave_game,
                       take_cell,
                       get_size,
                       get_cell_player,
                       is_there_a_winner);
    
    
    ScopedPointer<Client> frank = new Client();
    frank->setName("Frank");
    add_instruction(frank, "sleep", 10);
    add_instruction(frank, "join", "");
    
    NamedValueSet* instruction = add_instruction(frank, "take", "");
    instruction->set("x", 0);
    instruction->set("y", 1);
    instruction->set("name", "Frank");
    
    instruction = add_instruction(frank, "take", "");
    instruction->set("x", 0);
    instruction->set("y", 0);
    instruction->set("name", "Frank");
    
    instruction = add_instruction(frank, "take", "");
    instruction->set("x", 1);
    instruction->set("y", 0);
    instruction->set("name", "Frank");
    
    instruction = add_instruction(frank, "take", "");
    instruction->set("x", 1);
    instruction->set("y", 1);
    instruction->set("name", "Frank");
    
    instruction = add_instruction(frank, "status", "");
    instruction->set("x", 0);
    instruction->set("y", 1);
    
    add_instruction(frank, "sleep", 2);
    
    ScopedPointer<Client> susy = new Client();
    susy->setName("Susy");
    add_instruction(susy, "sleep", 5);
    add_instruction(susy, "join", "");
    
    instruction = add_instruction(susy, "take", "");
    instruction->set("x", 0);
    instruction->set("y", 1);
    instruction->set("name", "Susy");
    
    instruction = add_instruction(susy, "take", "");
    instruction->set("x", 0);
    instruction->set("y", 0);
    instruction->set("name", "Susy");
    
    instruction = add_instruction(susy, "take", "");
    instruction->set("x", 1);
    instruction->set("y", 0);
    instruction->set("name", "Susy");
    
    instruction = add_instruction(susy, "take", "");
    instruction->set("x", 1);
    instruction->set("y", 1);
    instruction->set("name", "Susy");
    
    add_instruction(susy, "sleep", 3);

    
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            instruction = add_instruction(susy, "take", "");
            instruction->set("x", i);
            instruction->set("y", j);
            instruction->set("name", "Susy");
        }
    }
    
    pool.addJob(frank, false);
    pool.addJob(susy, false);
    
    while(pool.getNumJobs() > 0 )
    {
        Thread::sleep(100);
    }
    
    stopserver();
    while( server_running() > 0)
    {
        Thread::sleep(100);
    }
    release_field_manager();
    Thread::sleep(1000);
    
}