本文整理汇总了C++中MsgDistributor::accept方法的典型用法代码示例。如果您正苦于以下问题:C++ MsgDistributor::accept方法的具体用法?C++ MsgDistributor::accept怎么用?C++ MsgDistributor::accept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MsgDistributor
的用法示例。
在下文中一共展示了MsgDistributor::accept方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: server_main
/******************************************************************************
Description.: calling this function creates and starts the server threads
Input Value.: -
Return Value: -
******************************************************************************/
void server_main()
{
printf("\n[server] start supporting service\n");
if (!orbit)
{
// init part
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
clilen = sizeof(cli_addr);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
error("ERROR opening socket");
}
else
if (debug) printf ("[server] obtain socket descriptor successfully.\n");
bzero((char *) &serv_addr, sizeof(serv_addr));
// set up the port number
portno = PORT_NO;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
}
else
if (debug) printf("[server] bind tcp port %d sucessfully.\n",portno);
if(listen(sockfd,5))
{
error("ERROR listening");
}
else
if (debug) printf ("[server] listening the port %d sucessfully.\n\n", portno);
// init finished, now wait for a client
while (!global_stop) {
pthread_t thread_id;
//Block here. Until server accpets a new connection.
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
// error("ERROR on accept");
fprintf(stderr,"Accept error!\n");
continue; //ignore current socket ,continue while loop.
}
else
printf ("[server] server has got connect from %s, socket id: %d.\n", (char *)inet_ntoa(cli_addr.sin_addr), newsockfd);
/* create thread and pass context to thread function */
if (pthread_create(&thread_id, 0, serverThread, (void *)&(newsockfd)) == -1)
{
fprintf(stderr,"pthread_create error!\n");
break; //break while loop
}
pthread_detach(thread_id);
usleep(1000 * 5); // sleep 5ms to avoid clients gain same sock
} /* end of while */
close(sockfd);
}
// below is orbit mode
else
{
int newsockfd;
// init finished, now wait for a client
while (!global_stop) {
pthread_t thread_id;
//Block here. Until server accpets a new connection.
newsockfd = MsgD.accept();
if (newsockfd < 0)
{
// error("ERROR on accept");
fprintf(stderr,"Accept error!\n");
continue; //ignore current socket ,continue while loop.
}
else
printf ("[server] server has got new connection, socket id: %d.\n", newsockfd);
/* create thread and pass context to thread function */
if (pthread_create(&thread_id, 0, serverThread, (void *)&(newsockfd)) == -1)
{
fprintf(stderr,"pthread_create error!\n");
break; //break while loop
}
pthread_detach(thread_id);
usleep(1000 * 5); // sleep 5ms to avoid clients gain same sock
} /* end of while */
if (debug) printf("main end\n");
//.........这里部分代码省略.........