Пример #1
0
void TcpServer::run() {

  m_listening = true;

  while (m_listening && m_open) {

    //printf("TcpServer: spinning\n");
    check_for_clients(); // new connections
    check_for_messages(); // messages from clients
  }

  serverStopped();
  m_listening = false;
}
Пример #2
0
int main()
{
	IRCD *ircd = malloc(sizeof(IRCD));
	ircd->numusers = 0;
	ircd->numchans = 0;
	ircd->exiting = 0;

	ircd->socks.ircd = get_socket(CLIENT_CONNECT_PORT, 0);
	net_bind(ircd->socks.ircd);
	net_listen(ircd->socks.ircd);
/*
	strcpy(ircd->users[0].nick, "test");
	
	ircd->channels[0].users[0] = ircd->users[0];

	printf("Nick of first user in channel: %s; user's nick: %s\n", ircd->channels[0].users[0].nick, ircd->users[0].nick);
*/

	
	/* START: Reuse port
	 * This bit of code lets you reuse the port (http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#bind)
	 */
	int yes=1;
	//char yes='1'; // Solaris people use this

	// lose the pesky "Address already in use" error message
	if (setsockopt(ircd->socks.ircd.fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
		perror("setsockopt");
		exit(1);
	}
	/* END: Reuse port */

	while(1){
		check_for_clients(ircd);
		recv_from_clients(ircd);
	}

	pthread_exit(NULL);
}