ftnword f77name(rpn_comm_softbarrier_init)(ftnword *comm) /* bind to port, return pointer to structure */ { struct set_of_ports *p=(struct set_of_ports *)malloc(sizeof(struct set_of_ports)); if(p == NULL) return(-1); p->my_server=bind_to_port(&(p->my_port),&(p->my_ip)); MPI_Comm_rank(*comm,&(p->pe_me)); MPI_Comm_size(*comm,&(p->nprocs)); p->list_server= malloc(p->nprocs*sizeof(int)); p->list_port = malloc(p->nprocs*sizeof(int)); p->comm = *comm; p->next = chain; chain = p; listen(p->my_server,2); MPI_Allgather(&(p->my_ip) ,1,MPI_INTEGER,p->list_server,1,MPI_INTEGER,*comm); MPI_Allgather(&(p->my_port),1,MPI_INTEGER,p->list_port ,1,MPI_INTEGER,*comm); #ifdef DEBUG if(p->pe_me==0){ int pe; for(pe=0;pe<p->nprocs;pe++) printf("PE=%d, IP=%x, port=%d\n",pe,p->list_server[pe],p->list_port[pe]); } #endif return(*comm); }
int main(int argc, char *argv[]){ char pwd[256]; printf("Please input: "); fgets( pwd, 255, stdin); int listener_d = socket(PF_INET, SOCK_STREAM, 0); if(listener_d == -1) error("Cannot open socket"); bind_to_port(listener_d, 30000); if(listen(listener_d,10) == -1) error("Cannot listen"); puts("Waiting for connection"); while(1){ struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if(connect_d == -1) error("Cannot open secondary socket"); send(connect_d, pwd, strlen(pwd), 0); close(connect_d); } return 0; }
static sock_t new_listening_TCP_socket(int family, uint16_t port) { sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); if (!sock_valid(sock)) { return ~0; } int ok = set_socket_nonblock(sock); if (ok && family == AF_INET6) { ok = set_socket_dualstack(sock); } if (ok) { ok = set_socket_reuseaddr(sock); } ok = ok && bind_to_port(sock, family, port) && (listen(sock, TCP_MAX_BACKLOG) == 0); if (!ok) { kill_sock(sock); return ~0; } return sock; }
int main() { int listener_d = open_listener_socket(); bind_to_port(listener_d, 30000); if(listen(listener_d, 10) == -1) error("Can't listen"); puts("Waiting for connection"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); while(1){ int connect_d = accept(listener_d, (struct sockaddr *) &client_addr, &address_size); if(connect_d == -1) error("Can't open secondary socket"); char *msg = advice[rand() % 5]; say(connect_d, msg); close(connect_d); } return 0; }
static sock_t new_listening_TCP_socket(int family, uint16_t port) { sock_t sock = socket(family, SOCK_STREAM, IPPROTO_TCP); if (!sock_valid(sock)) { return ~0; } #ifndef TCP_SERVER_USE_EPOLL int ok = set_socket_nonblock(sock); #else int ok = 1; #endif if (ok && family == AF_INET6) { ok = set_socket_dualstack(sock); } ok = ok && bind_to_port(sock, family, port) && (listen(sock, TCP_MAX_BACKLOG) == 0); if (!ok) { kill_sock(sock); return ~0; } return sock; }
int main(int argc, char *argv[]) { if(catch_signal(SIGINT, handle_shutdown) == -1) error("Can't set the interrupt handler."); listener_d = open_listener_socket(); bind_to_port(listener_d, 30000); if(listen(listener_d, 10) == -1) error("Can't listen."); struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); puts("Waiting for connection."); char buf[255]; // This version of the server forks a new seperate child process to each client. while(1) { int connect_d = accept(listener_d, (struct sockaddr*) &client_addr, &address_size); if(connect_d == -1) error("Can't open secondary socket."); if(!fork()) { close(listener_d); if(say(connect_d, "Temperature sensor ready\r\n") != -1) { read_in(connect_d, buf, sizeof(buf)); logger(buf); if(strncasecmp("READDATA", buf, 8)) { } else { say(connect_d, "1: temp data in JSON format\r\n"); say(connect_d, "2: temp data in JSON format\r\n"); say(connect_d, "3: temp data in JSON format\r\n"); } /* else { if(say(connect_d, "Oscar\r\n> ") != -1) { read_in(connect_d, buf, sizeof(buf)); if(strncasecmp("Oscar who?", buf, 10)) say(connect_d, "You should say 'Oscar who?'!\r\n"); else say(connect_d, "Oscar silly question, you get a silly answer.\r\n"); } } */ } close(connect_d); exit(0); } close(connect_d); } return 0; }
int main(int argc, char **argv) { main_b(); int listen_socket, connection_socket = 0, address_lenght, optval = 1; struct sockaddr_in server_address, client_address; listen_socket = create_socket(); setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); server_address = set_address(server_address); bind_to_port(listen_socket, server_address); address_lenght = sizeof(client_address); while (1) perform_connection(connection_socket, listen_socket, client_address, address_lenght); }
void *runserver(void *arg) { int listener; fd_set readset; listener = open_listener_socket(); //make_nonblocking(listener); bind_to_port(listener, server_port); FD_ZERO(&readset); //Listen if (listen(listener, QUEUE_SIZE) == -1) perror("Can't listen"); //Accept a connection struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); int idx = 0; all_sockets[idx] = listener; pthread_t referee; pthread_create(&referee, NULL, run_referee, NULL); while (!shutdown_server) { server_is_running = 1; int connect_d = accept(listener, (struct sockaddr *) &client_addr, &address_size); if (connect_d == -1){ if (shutdown_server == 1) { break; } perror("Can't open secondary socket"); break; } pthread_t thread; printf("SERVER: accepting new client on socket %i\r\n", connect_d); struct thread_arg *thread_data = (struct thread_arg *) malloc(sizeof(struct thread_arg)); thread_data->client_socket = connect_d; idx++; thread_data->thread_idx = idx; pthread_create(&thread, NULL, handle_tcp_client, thread_data); pthread_detach(thread); } if (all_sockets[0] > 0) { close(all_sockets[0]); } pthread_join (referee, NULL); server_is_running = 0; return (void *) NULL; }
int main(int argc, char *argv[]){ // Call handle shutdown if "Ctrl+C" is hit if (catch_signal(SIGINT, handle_shutdown) == -1) error("Can’t set the interrupt handler"); listener_d = open_listener_socket(); // Create a socket bind_to_port(listener_d, 30000); // Set the listen queue lenght to 10 if (listen(listener_d, 10) == -1) error("Can’t listen"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); puts("Waiting for connection"); char buf[255]; while (1) { // Listen for a connection int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if (connect_d == -1) error("Can’t open secondary socket"); if(!fork()){ close(listen_d); // Send data to the client if (say(connect_d, "Internet Knock-Knock Protocol Server\r\nVersion 1.0\r\nKnock! Knock!\r\n>") != -1) { read_in(connect_d, buf, sizeof(buf)); // Check the clients answers if (strncasecmp("Who's there?", buf, 12)) say(connect_d, "You should say 'Who's there?'!"); else { if (say(connect_d, "Oscar\r\n> ") != -1) { read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Oscar who?", buf, 10)) say(connect_d, "You should say 'Oscar who?'!\r\n"); else say(connect_d, "Oscar silly question, you get a silly answer\r\n"); } } } close(connect_d); exit(0); } // close secondary socket. close(connect_d); } return 0; }
int main(int argc, char **argv) { #ifdef _WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2,0), &wsaData); #endif main_b(); int listen_socket, connection_socket = 0, address_lenght, optval = 1; struct sockaddr_in server_address, client_address; listen_socket = create_socket(); setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); server_address = set_address(server_address); bind_to_port(listen_socket, server_address); address_lenght = sizeof(client_address); while (1) perform_connection(connection_socket, listen_socket, client_address, address_lenght); }
int main(int argc, char *argv[]) { int connect_d = 0, rc = 0; char intro_msg[] = "Internet Knock-Knock Protocol Server\nKnock, knock.\n"; if (catch_signal(SIGINT, handle_shutdown) == -1) error("Setting interrupt handler"); int port = 30000; listener_d = open_listener_socket(); bind_to_port(listener_d, port); if (listen(listener_d, 10) == -1) error("Can't listen"); printf("Waiting for connection on port %d\n", port); char buf[255]; while (1) { connect_d = open_client_socket(); if (say(connect_d, intro_msg) == -1) { close(connect_d); continue; } read_in(connect_d, buf, sizeof(buf)); // check to make sure they said "Who's there?" if (say(connect_d, "Surrealist giraffe.\n") == -1) { close(connect_d); continue; } read_in(connect_d, buf, sizeof(buf)); // check to make sure they said "Surrealist giraffe who?" if (say(connect_d, "Bathtub full of brightly-colored machine tools.\n") == -1) { close(connect_d); continue; } close(connect_d); } return 0; }
int main(int argc, char **argv) { time_seed = time(0); parse_parameters(argc, argv); if (getuid() == 0) { if (UID == -1) error("user not set"); if (setgid(GID) != 0) error("Unable to drop group privileges"); if (setuid(UID) != 0) error("Unable to drop user privileges"); } if (BASEDIR == NULL) set_basedir(); startup_message(); int listen_socket, optval = 1; struct sockaddr_in server_address; listen_socket = create_socket(); setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); server_address = set_address(server_address); bind_to_port(listen_socket, server_address); if (DAEMON) { pid_t pid; pid = fork(); if (pid == -1) error("Failed to fork"); if (pid == 0) while (1) perform_connection(listen_socket); } else while (1) perform_connection(listen_socket); return 0; }
/* 主函数 */ int main(int argc, char *argv[]) { if (catch_signal(SIGINT, handle_shutdown) == -1) error("Can't set the interrupt handler");// 如果有人按了Ctrl-C就调用handle_shutdown()。 listener_d = open_listener_socket();// 创建套接字。 bind_to_port(listener_d, 30000); if (listen(listener_d, 10) == -1) {// 把监听长度设为10。 error("listen error: "); } struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); puts("Waiting for connection"); char buf[255]; while (1) {// 需要循环“接受连接,然后开始对话”。 int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size);// 接受来自客户端的连接。 if (connect_d == -1) { error("connect error:"); } if (!fork()) {// 创建子进程,如果fork()调用返回0,就说明你在子进程中。 close(listener_d);// 在子进程中,需要关闭主监听套接字。子进程只用connect_d套接字和客户端通信。 if (say(connect_d, "Internet Knock-Knock Server \r\nVersion 1.0\r\nKnock! Knock!\r\n>") != -1){ read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Who's there?", buf, 12)) say(connect_d, "You should say 'Who's there?' !"); else { if (say(connect_d, "Oscar\r\n>") != -1) { read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Oscar who?", buf, 10)) say(connect_d, "You should say 'Oscar who?' !\r\n"); else say(connect_d, "Oscar silly question, you set a silly answer\r\n"); } } } close(connect_d);// 一旦通信结束,子进程就可以关闭通信套接字了。 exit(0);// 通信结束以后,子进程应该退出程序。这样就能防止子进程进入服务器的主循环。 } close(connect_d); } return 0; }
int main(int argc, char* argv[]) { if (signal(SIGINT, handle_shutdown) == SIG_ERR) /* SIGINT = ctrl-c = intr */ error("Can't set the interrupt handler"); listener_d = open_listener_socket(); bind_to_port(listener_d, 30000); if (listen(listener_d, 10) == -1) /* 监听连接 */ error("Can't listen"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof client_addr; puts("Waiting for connection"); char buf[255]; for(;;) { int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if (connect_d == -1) error("Can't open secondary socket"); if ( !fork()) { /* 这里会检查连接时的进程号,如果返回值为0则表示该进程是子进程需要在操作,否者跳过 */ if (say(connect_d, "Internet Knock-Knock Protocol Server\r\nVersion 1.0\r\nKnock! Knock!\r\n> ") != -1) { /* 向客户端发送数据 */ /* 上面这行就决定你一开是会看到什么 */ read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Who's there?", buf, 12)) /* 比较 buf 变量的值是否为 who's there? 这里就决定你输入什么才是正确的 */ say(connect_d, "You should say 'Who's there?'!"); else { if (say(connect_d, "Oscar\r\n> ") != -1) { read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Oscar who?", buf, 10)) say(connect_d, "You should say 'Oscar who?'!\r\n"); else say(connect_d, "Oscar silly question, you get a silly answer\r\n"); } } } close(connect_d); exit(0); } close(connect_d); } return 0; }
int main(int argc, char *argv[]) { if (argc != 2) { puts("Please specify port."); exit(1); } int port = atoi(argv[1]); //al teclear CTRL+C se termina el servidor if (catch_signal(SIGINT, handle_shutdown) == -1) error("Can't set the interrupt handler"); listener_d = open_listener_socket(); bind_to_port(listener_d, port); //maximo de clientes 10 if (listen(listener_d, MAX_CLIENTS) == -1) error("Can't listen"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); printf("Listening in port %d\n", port); char buf[255]; while (1) { printf("Esperando la petición\n"); int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if (connect_d == -1){ error("Can't open secondary socket"); } //ejecución normal threads pthread_create(&threads[client_counter], NULL, new_client, (void*)connect_d); } return 0; }
int main (int argc, char *argv[]) { if (catch_signal (SIGINT, handle_shutdown) == -1) error ("割り込みハンドラを設定できません。"); listener_d = open_listener_socket (); bind_to_port (listener_d, 30000); if (listen (listener_d, 10) == -1) error ("接続待できません。"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof (client_addr); puts ("接続を待っています。"); char buf[256]; while (1) { int connect_d = accept (listener_d, (struct sockaddr*)&client_addr, &address_size); if (connect_d == -1) error ("第2のソケットを開けません。"); if (!fork()) { close (!fork()); if (say (connect_d, "インターネット ノックノックプロトコル\r\nバージョン1.0\r\n") != -1) { read_in (connect_d, buf, sizeof (buf)); if (strncasecmp ("Who's there?", buf, 12)) say (connect_d, "「Who's there?」と入力しなければいけません。"); else { if (say (connect_d, "Oscar\r\n") != -1) { read_in (connect_d, buf, sizeof (buf)); if (strncasecmp ("Oscar who?", buf, 10)) say (connect_d, "「Who's there?」と入力しなければいけません。"); else say (connect_d, "Oscar silly question, you'll get silly answer."); } } } close (connect_d); exit (0); } close (connect_d); } return 0; }
OscFloatReceiver::OscFloatReceiver(int port) { // Create socket and bind socket to port listener_d = open_listener(); bind_to_port(listener_d, port); }
int bind_to_tcp6_port(int port, int backlog) { return bind_to_port(port, AF_INET6, SOCK_STREAM, backlog); }
int bind_to_udp6_port(int port) { return bind_to_port(port, AF_INET6, SOCK_DGRAM, 0); }
int main(int argc, char *argv[]) { struct server_settings settings = {30000, 0, 1}; handle_arguments(argc, argv, &settings); struct player_character hero; init_player_character(&hero); char stat_buffer[256]; randomize_player_character_stats(&hero); snprintf(stat_buffer, 255, "STR - %i\r\nDEX - %i\r\nCON - %i\r\n", hero.strength, hero.dexterity, hero.constitution); if (catch_signal(SIGINT, handle_shutdown) == -1) { error("Can't set the interrupt handler"); } else if (settings.verbose) { puts("Interrupt handler set"); } listener_d = open_listener_socket(); bind_to_port(listener_d, settings.port_number); if (listen(listener_d, 10) == -1) { error("Can't listen"); } else if (settings.verbose) { printf("Listening on port %i", settings.port_number); } struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); if (settings.verbose) { puts("Waiting for connection"); } while(1) { int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if (connect_d == -1) { error("Can't open secondary socket"); } else if (settings.verbose) { puts("New connection started"); } if (!fork()) { close(listener_d); char input_buffer[512]; char output_buffer[512]; char send_buffer[512]; char action_buffer[3]; send_version(connect_d); while (1) { get_command(connect_d, action_buffer); puts(action_buffer); if (action_buffer[0] == 's') { puts("opening chat buffer"); listen_to_client(connect_d, input_buffer); parse_command(input_buffer, output_buffer); sprintf(send_buffer, "You said: \"%s\"\r\n", output_buffer); puts(send_buffer); send_to_client(connect_d, send_buffer); } else { handle_user_command(connect_d, action_buffer); } } close(connect_d); exit(0); } close(connect_d); } return 0; }