int main(int argc, char* argv[]) { if (argc < 4) { std::cout << "Usage: " << argv[0] << " <host> <port> <local/remote>" << std::endl; exit(1); } NetworkOptions options; options.set_host(argv[1]); options.set_port(atoi(argv[2])); options.set_max_packet_size(1024); if (strcmp(argv[3], "local") == 0) { options.set_socket_family(PF_UNIX); options.set_sin_path("/tmp/__echoserver__"); } else { options.set_socket_family(PF_INET); } EventLoopImpl ss; EchoServer echo_server(&ss, options); if (echo_server.Start() != 0) { // What the hell happened std::cout << "Server failed to start\n"; return 1; } ss.loop(); return 0; }
int main(int argc, char* argv[]) { if (argc != 2) { std::cerr << "Usage: echo_server <port>" << std::endl; return -1; } return echo_server(atoi(argv[1])).run(); }
main( int argc, char *argv[] ) { int portno ; if( argc != 2 ) { fprintf( stdout,"Usage: %s portno\n",argv[0] ); exit( -1 ); } portno = strtol( argv[1],0,10 ); echo_server( portno ); }
int main() { //udp socket int sockfd = socket(PF_INET,SOCK_DGRAM,0); struct sockaddr_in bindaddr; bindaddr.sin_family = AF_INET; bindaddr.sin_port = htons(9999); int ret = inet_pton(AF_INET,"127.0.0.1",(void*)&bindaddr.sin_addr); if (ret != 1) perror("trandfomr ip addr\n"); bind(sockfd,(struct sockaddr*)&bindaddr,sizeof(bindaddr)); echo_server(sockfd); close(sockfd); return 0; }
int main() { int listen_fd; // create a socket for listening if ((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { ERR_EXIT("socket function return negative"); } unlink(SOCKET_FILE); struct sockaddr_un server_addr; memset(&server_addr, 0, sizeof(server_addr)); server_addr.sun_family = AF_UNIX; strcpy(server_addr.sun_path, SOCKET_FILE); // bind a address to the listening socket for clients to connect if (bind(listen_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) { ERR_EXIT("bind error"); } // start to listen for connecting request if (listen(listen_fd, SOMAXCONN) < 0) { ERR_EXIT("listen error"); } while (1) { // create a new socket, which implies a new connection between server and client int conn_fd = accept(listen_fd, NULL, NULL); if (conn_fd == -1) { if (conn_fd == EINTR) continue; ERR_EXIT("acccept error"); } pid_t pid = fork(); if (pid == -1) { ERR_EXIT("fork error"); } if (pid == 0) { close(listen_fd); echo_server(conn_fd); exit(EXIT_SUCCESS); } close(conn_fd); } return 0; }
int main(int argc, char **argv) { int client, ret; char listen_path[PATH_MAX]; if (argc < 2) { printf("Usage: %s LISTEN_PORT\n", argv[0]); exit(EXIT_FAILURE); } signal(SIGCHLD, SIG_IGN); establish_server(argv[1], listen_path, sizeof listen_path); for (; ; close(client)) { client = accept_client(listen_path); ret = fork(); if (ret == 0) echo_server(client); else if (ret < 0) perror("fork"); } return EXIT_SUCCESS; }
int main(void) { signal(SIGCHLD,sig_hander); //signal(SIGCHLD,SIG_IGN); //创建一个 int listensocket; if((listensocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0) { ERR_EXIT("socket"); } struct sockaddr_in seraddr; //memset()这个函数是初始化这个内存 memset(&seraddr,0,sizeof(seraddr)); seraddr.sin_family=AF_INET; seraddr.sin_port=htons(3000); //ip地址的初始化可以有以下三种方式,但是推荐第一种 seraddr.sin_addr.s_addr=htonl(INADDR_ANY); /*seraddr.sin_addr.s_addr=inet_addr("127.0.0.1"); inet_aton("127.0.0.1",&seraddr.sin_addr);*/ //我们最好在绑定之前设置一下套接字选项SO_REUSEADDR int on=1; if(setsockopt(listensocket,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))<0) { ERR_EXIT("setsockopt"); } //这个socket的地址结构已经初始化好了之后那么接下来就是绑定了,就是将套接字绑定的本地的套接字的地址上; if(bind(listensocket,(struct sockaddr*)&seraddr,sizeof(seraddr))<0) { ERR_EXIT("bind"); } //bind 完了之后就是要监听这个套接字,监听之前的套接字是主动的套接字,监听后的套接字就是被动的套接字了; if(listen(listensocket,SOMAXCONN)<0) { ERR_EXIT("listen"); } //listen之后就是将主动套接字转换为被动套接字,接下来就是接受客户端的连接请求,accept函数的调用; struct sockaddr_in cliaddr; socklen_t cliaddrlen=sizeof(cliaddr); int connsocket; pid_t pid; while(1) { if((connsocket=accept(listensocket,(struct sockaddr*)&cliaddr,&cliaddrlen))<0) { ERR_EXIT("accept"); } printf("接收到一个连接:客户端的ip=%s,port=%d\n",inet_ntoa(cliaddr.sin_addr),ntohs(cliaddr.sin_port)); pid=fork(); if(pid==-1) { ERR_EXIT("fork"); } if(pid==0)//也就是如果是子进程,那么子进程就不需要监听套接字,首先要关闭监听套接字,然后就是处理通信的过程 { close(listensocket); echo_server(connsocket,cliaddr); exit(EXIT_SUCCESS); } else { close(connsocket); } } //close(connsocket); //close(listensocket); return 0; }
/*-------------------------------------------------------------------- main() ------------------------------------------------------------------*/ int main( int argc, char *argv[] ) { echo_server( 1 ); return( 0 ); } /* end of main() */