int main(int argc, char** argv) { g_signal.RegisterSignals(); int port = 7777; int g_servfd = listen_net(port); if (g_servfd < 0) { printf("listen_net failed %s\n", strerror(errno)); return -1; } printf("listen_net start\n"); int clifd1 = my_accept(g_servfd); if (clifd1 < 0) { printf("my_accept failed %s\n", strerror(errno)); return -1; } set_fd_nonblock(clifd1); printf("my_accept clifd1 %d\n", clifd1); int clifd2 = my_accept(g_servfd); if (clifd2 < 0) { printf("my_accept failed %s\n", strerror(errno)); return -1; } set_fd_nonblock(clifd2); printf("my_accept clifd2 %d\n", clifd2); int fds[2]; fds[0] = clifd1; fds[1] = clifd2; int pid1 = fork(); if (pid1 == 0) //子进程 { clildfolw(fds, 2); return 0; } int pid2 = fork(); if (pid2 == 0) //子进程 { clildfolw(fds, 2); return 0; } sleep(1000); return 0; }
int main(int argc, char **argv) { int listenfd = 0; int connfd = 0; pthread_t tid; socklen_t addrlen; socklen_t len ; struct sockaddr *cliaddr = NULL; int *iptr = NULL; if (argc == 2) { listenfd = tcp_listen(NULL, argv[1], &addrlen); } else if (argc == 3) { listenfd = tcp_listen(argv[1], argv[2], &addrlen); } else { err_quit("usage: ./a.out [ <host> ] <service or port>"); } cliaddr = my_malloc(addrlen); while (1) { len = addrlen; iptr = my_malloc(sizeof(int)); *iptr = my_accept(listenfd, cliaddr, &len); my_pthread_create(&tid, NULL, &doit, iptr); } }
bool add_client(int fd_socket, t_server *server) { int client_fd; if ((my_accept(&client_fd, fd_socket)) == false) return (false); if (server->client_log == server->client_max && (server->client_max += 1)) if ((server->fd_type = realloc(server->fd_type, sizeof(fd_type) * (server->client_max))) == NULL || (server->exec = realloc(server->exec, sizeof(fptr) * (server->client_max))) == NULL || (server->user = realloc(server->user, sizeof(t_client) * (server->client_max))) == NULL || (server->id_users = realloc(server->id_users, sizeof(int) * (server->client_max))) == NULL) return (false); if ((server->user[client_fd] = malloc(sizeof(t_client))) == NULL) return (false); set_client(server, client_fd); server->fd_type[client_fd] = FD_CLIENT; server->id_users[client_fd] = -1; server->exec[client_fd] = &read_on_client; server->user[client_fd]->id = find_player(server); printf("%s\nPlayer %d connected%s\n", text[GREEN], server->user[client_fd]->id, text[NOCOLOR]); server->client_log += 1; return (reponse_to_client(client_fd, "BIENVENUE\n", server, 10)); }
gboolean process_comming_request(GIOChannel *gio, GIOCondition condition, gpointer data) { int ret; if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) g_error("unknown condition"); if (condition & G_IO_IN) { int lsock; int csock; lsock = g_io_channel_unix_get_fd(gio); csock = my_accept(lsock); if (csock < 0) return TRUE; ret = recv_pdu(csock); if (ret < 0) err("process recieved pdu"); close(csock); } return TRUE; }
/* * call-seq: * * server = Kgio::UNIXServer.new("/path/to/unix/socket") * server.kgio_accept -> Kgio::Socket or nil * server.kgio_accept(klass = MySocket) -> MySocket or nil * server.kgio_accept(nil, flags) -> Kgio::Socket or nil * * Initiates a blocking accept and returns a generic Kgio::Socket * object with the kgio_addr attribute set (to the value of * Kgio::LOCALHOST) on success. * * On Ruby implementations using native threads, this can use a blocking * accept(2) (or accept4(2)) system call to avoid thundering herds. * * An optional +klass+ argument may be specified to override the * Kgio::Socket-class on a successful return value. * * An optional +flags+ argument may also be specifed to override the * value of +Kgio.accept_cloexec+ and +Kgio.accept_nonblock+. +flags+ * is a bitmask that may contain any combination of: * * - Fcntl::FD_CLOEXEC - close-on-exec flag * - IO::NONBLOCK - non-blocking flag */ static VALUE unix_accept(int argc, VALUE *argv, VALUE self) { struct accept_args a; a.addr = NULL; a.addrlen = NULL; prepare_accept(&a, self, argc, argv); return my_accept(&a, 0); }
/* * call-seq: * * server = Kgio::TCPServer.new('0.0.0.0', 80) * server.kgio_accept -> Kgio::Socket or nil * server.kgio_tryaccept -> Kgio::Socket or nil * server.kgio_tryaccept(klass = MySocket) -> MySocket or nil * * Initiates a blocking accept and returns a generic Kgio::Socket * object with the kgio_addr attribute set to the IP address of * the client on success. * * On Ruby implementations using native threads, this can use a blocking * accept(2) (or accept4(2)) system call to avoid thundering herds. * * An optional +klass+ argument may be specified to override the * Kgio::Socket-class on a successful return value. * * An optional +flags+ argument may also be specifed to override the * value of +Kgio.accept_cloexec+ and +Kgio.accept_nonblock+. +flags+ * is a bitmask that may contain any combination of: * * - Fcntl::FD_CLOEXEC - close-on-exec flag * - IO::NONBLOCK - non-blocking flag */ static VALUE tcp_accept(int argc, VALUE *argv, VALUE self) { struct sockaddr_storage addr; socklen_t addrlen = sizeof(struct sockaddr_storage); struct accept_args a; a.addr = (struct sockaddr *)&addr; a.addrlen = &addrlen; prepare_accept(&a, self, argc, argv); return my_accept(&a, 0); }
void nap_chat_start(int snum) { GetFile *gf; SocketList *s; if (!(s = get_socket(snum)) || !(gf = (GetFile *)s->info)) { put_it("error get_socket(%d)", snum); nap_finished_file(snum, PREMATURE_FINISH); return; } if (gf->deleted) { if (gf->write != -1) close(gf->write); gf->write = -1; if (gf->deleted++ > 5) close_socketread(snum); return; } if ((gf->flags & NAP_CHAT) == NAP_CHAT) { /* we need to accept the connection here. */ struct sockaddr_in remaddr; int sra = sizeof(struct sockaddr_in); int sock = -1; if ((sock = my_accept(snum, (struct sockaddr *) &remaddr, &sra)) > -1) { set_keepalive(sock); gf->port = ntohs(remaddr.sin_port); add_socketread(sock, gf->port, 0, inet_ntoa(remaddr.sin_addr), nap_chat, NULL); set_socketinfo(sock, gf); s->info = NULL; gf->flags = NAP_CHAT_CONNECTED; gf->socket = sock; malloc_strcpy(&gf->ip, inet_ntoa(remaddr.sin_addr)); say("DCC CHAT to %s [%s:%d] established", gf->nick, gf->ip, gf->port); gf->starttime = now; } close_socketread(snum); return; } }
int main(int argc, char **argv) { int listenfd = 0; int connfd = 0; pid_t childpid ; socklen_t clilen; struct sockaddr_in cliaddr; struct sockaddr_in servaddr; listenfd = my_socket(AF_INET, SOCK_STREAM, 0); signal(SIGCHLD, tu5_11sig_chld); /// bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); my_bind(listenfd, (SA *)&servaddr, sizeof(servaddr)); my_listen(listenfd, LISTENQ); while(1) { clilen = sizeof(cliaddr); if ( (connfd = my_accept(listenfd, (SA*)&cliaddr, &clilen)) < 0) { } if ((childpid = my_fork()) == 0) { my_close(listenfd); tu5_17str_echo(connfd); exit(0); } my_close(connfd); } return 0; }
int Rsock() { int sfd = listen_net(g_port); if (sfd < 0) { printf("listen_net failed:%s \n", strerror(errno)); return -1; } int fd = my_accept(sfd); if (fd < 0) { printf("my_accept failed:%s \n", strerror(errno)); return -1; } int buff = 1024*1024; socklen_t len = sizeof(int); if(setsockopt(fd , SOL_SOCKET,SO_RCVBUF , &buff , len) == -1) { printf("setsockopt SO_RCVBUF failed:%s\n", strerror(errno)); } if(setsockopt(fd , SOL_SOCKET,SO_SNDBUF , &buff , len) == -1) { printf("setsockopt SO_SNDBUF failed:%s\n", strerror(errno)); } gettimeofday(&time_c, NULL); while (true) { if (_HandleSignal() < 0) break; gettimeofday(&time_a, NULL); nRet = read(fd, rbuff, g_len); //nRet = readn(fd, rbuff, g_len, 0); gettimeofday(&time_b, NULL); struct timeval *time_w = (struct timeval *)(rbuff); if (nRet != g_len) { printf("read fialed nRet %d %s\n", nRet, strerror(errno)); break; } g_count0++; if(g_verify == 1) { struct data* d = (struct data* )(rbuff+sizeof(struct timeval)); if( d->compare() < 0) { printf("format error %s\n");fflush(NULL); } } unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time0 += use_time0; unsigned long long delay_time = (time_b.tv_sec - time_w->tv_sec) * 1000000 + (time_b.tv_usec - time_w->tv_usec); if(delay_time < 10000) { if(delay_time > g_delay_max) g_delay_max = delay_time; recv_delay_count0++; recv_delay0 += delay_time; } } gettimeofday(&time_d, NULL); unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec); total_time2 += use_time2; g_count2 = g_count0; return 0; }
int Rpollset() { #ifdef AIX int servfd = listen_net(g_port); if (servfd < 0) { printf("listen_net failed:%s \n", strerror(errno)); return -1; } struct poll_ctl pollset[CLIENT_NUM]; for(int i = 0; i< CLIENT_NUM; i++) { pollset[i].fd = my_accept(servfd); if (pollset[i].fd < 0) { printf("my_accept failed %s\n", strerror(errno)); return -1; } int buff = 1024*1024; socklen_t len = sizeof(int); if(setsockopt(pollset[i].fd , SOL_SOCKET,SO_RCVBUF , &buff , len) == -1) { printf("setsockopt SO_RCVBUF failed:%s\n", strerror(errno)); } if(setsockopt(pollset[i].fd , SOL_SOCKET,SO_SNDBUF , &buff , len) == -1) { printf("setsockopt SO_SNDBUF failed:%s\n", strerror(errno)); } } pollset_t ps = pollset_create(-1); if(ps < 0) { printf("pollset_create failed %s\n",strerror(errno)); return -1; } for(int i = 0; i< CLIENT_NUM; i++) { pollset[i].events = POLLIN; pollset[i].cmd = PS_ADD; printf("my_accept clifd[%d] %d\n", i , pollset[i].fd); } nRet = pollset_ctl(ps, pollset, CLIENT_NUM); if(nRet < 0) { printf("pollset_ctl failed %s\n",strerror(errno)); return -1; } char buff[MAX_FD][1024]; int readlen[MAX_FD]; memset(buff, 0, MAX_FD*1024); memset(readlen, 0, sizeof(readlen)); struct pollfd fds[CLIENT_NUM]; memset(fds, 0, sizeof(fds)); int conn = CLIENT_NUM; int nRet = 0; gettimeofday(&time_c, NULL); while (true) { if (_HandleSignal() < 0) break; if (conn == 0) { printf("no fd poll\n"); break; } if (g_count2 >= loop_times && loop_times != 0) { printf("finish\n"); break; } gettimeofday(&time_a, NULL); nRet = pollset_poll(ps, fds, CLIENT_NUM, -1); gettimeofday(&time_b, NULL); if (nRet < 0) { printf("poll failed %s\n", strerror(errno)); break; } unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time0 += use_time0; g_count0++; for(int i = 0; i< CLIENT_NUM; i++) { if(fds[i].fd > 0 && (fds[i].revents & (POLLIN | POLLERR))) { gettimeofday(&time_e, NULL); nRet = read(fds[i].fd, buff[fds[i].fd]+readlen[fds[i].fd], g_len-readlen[fds[i].fd]); gettimeofday(&time_f, NULL); if (nRet == 0) { printf("%d fd %d close\n", i, fds[i].fd); struct poll_ctl delset; delset.cmd = PS_DELETE; delset.fd = fds[i].fd; nRet = pollset_ctl(ps, &delset, 1); if(nRet < 0) { printf("pollset_ctl failed %s\n",strerror(errno)); return -1; } fds[i].fd = -1; conn -- ; break; } if (nRet < 0) { printf("%d fd %d err %s \n", i, fds[i].fd,strerror(errno)); fds[i].fd = -1; conn -- ; break; } unsigned long long use_time1 = (time_f.tv_sec - time_e.tv_sec) * 1000000 + (time_f.tv_usec - time_e.tv_usec); total_time1 += use_time1; g_count1++; if (nRet != g_len-readlen[fds[i].fd])//没读完 { readlen[fds[i].fd] += nRet; } else if (nRet == g_len-readlen[fds[i].fd])//读完 { if(g_verify == 1 && buff[fds[i].fd][0] != 'a') { printf("format error %s\n", buff[fds[i].fd]); return -1; } readlen[fds[i].fd] = 0; g_count2++; memcpy(rbuff, buff[fds[i].fd], sizeof(buff[fds[i].fd])); memset(buff[fds[i].fd], 0, sizeof(buff[fds[i].fd])); } } } } gettimeofday(&time_d, NULL); unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec); total_time2 += use_time2; nRet = pollset_destroy(ps); if(nRet < 0) { printf("pollset_destroy failed %s\n",strerror(errno)); return -1; } #endif return 0; }
int Rpoll2() { int servfd = listen_net(g_port); if (servfd < 0) { printf("listen_net failed:%s \n", strerror(errno)); return -1; } struct pollfd fds[CLIENT_NUM]; for(int i = 0; i< CLIENT_NUM; i++) { fds[i].fd = my_accept(servfd); if (fds[i].fd < 0) { printf("my_accept failed %s\n", strerror(errno)); return -1; } int buff = 1024*1024; socklen_t len = sizeof(int); if(setsockopt(fds[i].fd , SOL_SOCKET,SO_RCVBUF , &buff , len) == -1) { printf("setsockopt SO_RCVBUF failed:%s\n", strerror(errno)); } if(setsockopt(fds[i].fd , SOL_SOCKET,SO_SNDBUF , &buff , len) == -1) { printf("setsockopt SO_SNDBUF failed:%s\n", strerror(errno)); } } for(int i = 0; i< CLIENT_NUM; i++) { fds[i].events = POLLIN; printf("my_accept clifd[%d] %d\n", i , fds[i].fd); } char buff[CLIENT_NUM][1024]; int readlen[CLIENT_NUM]; memset(buff, 0, CLIENT_NUM*1024); memset(readlen, 0, sizeof(readlen)); int conn = CLIENT_NUM; int nRet = 0; gettimeofday(&time_c, NULL); while (true) { if (_HandleSignal() < 0) break; if (conn == 0) { printf("no fd poll\n"); break; } if (g_count2 >= loop_times && loop_times != 0) { printf("finish\n"); break; } gettimeofday(&time_a, NULL); nRet = poll(fds, CLIENT_NUM, -1); gettimeofday(&time_b, NULL); if (nRet < 0) { printf("poll failed %s\n", strerror(errno)); break; } unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time0 += use_time0; g_count0++; for(int i = 0; i< CLIENT_NUM; i++) { if(fds[i].revents & (POLLIN | POLLERR)) { gettimeofday(&time_e, NULL); nRet = read(fds[i].fd, buff[i]+readlen[i], g_len-readlen[i]); gettimeofday(&time_f, NULL); if (nRet == 0) { printf("%d fd %d close\n", i, fds[i].fd); fds[i].fd = -1; conn -- ; break; } if (nRet < 0) { printf("%d fd %d err %s \n", i, fds[i].fd,strerror(errno)); fds[i].fd = -1; conn -- ; break; } unsigned long long use_time1 = (time_f.tv_sec - time_e.tv_sec) * 1000000 + (time_f.tv_usec - time_e.tv_usec); total_time1 += use_time1; g_count1++; if (nRet != g_len-readlen[i])//没读完 { readlen[i] += nRet; } else if (nRet == g_len-readlen[i])//读完 { if(g_verify == 1 && buff[i][0] != 'a') { printf("format error %s\n", buff[i]); return -1; } readlen[i] = 0; g_count2++; memcpy(rbuff, buff[i], sizeof(buff[i])); memset(buff[i], 0, sizeof(buff[i])); } } } } gettimeofday(&time_d, NULL); unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec); total_time2 += use_time2; return 0; }
int Rpoll() { int servfd = listen_net(g_port); if (servfd < 0) { printf("listen_net failed:%s \n", strerror(errno)); return -1; } struct pollfd fds[CLIENT_NUM]; for(int i = 0; i< CLIENT_NUM; i++) { fds[i].fd = my_accept(servfd); if (fds[i].fd < 0) { printf("my_accept failed %s\n", strerror(errno)); return -1; } int buff = 1024*1024; socklen_t len = sizeof(int); if(setsockopt(fds[i].fd , SOL_SOCKET,SO_RCVBUF , &buff , len) == -1) { printf("setsockopt SO_RCVBUF failed:%s\n", strerror(errno)); } if(setsockopt(fds[i].fd , SOL_SOCKET,SO_SNDBUF , &buff , len) == -1) { printf("setsockopt SO_SNDBUF failed:%s\n", strerror(errno)); } } for(int i = 0; i< CLIENT_NUM; i++) { fds[i].events = POLLIN; printf("my_accept clifd[%d] %d\n", i , fds[i].fd); } int conn = CLIENT_NUM; int nRet = 0; gettimeofday(&time_c, NULL); while (true) { if (_HandleSignal() < 0) break; if (conn == 0) { printf("no fd poll\n"); break; } if (g_count2 >= loop_times && loop_times != 0) { printf("finish\n"); break; } gettimeofday(&time_a, NULL); nRet = poll(fds, CLIENT_NUM, -1); gettimeofday(&time_b, NULL); if (nRet < 0) { printf("poll failed %s\n", strerror(errno)); break; } unsigned long long use_time0 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time0 += use_time0; g_count0++; for(int i = 0; i< CLIENT_NUM; i++) { if(fds[i].revents & (POLLIN | POLLERR)) { gettimeofday(&time_e, NULL); nRet = readn(fds[i].fd, rbuff, g_len, 0); gettimeofday(&time_f, NULL); if (nRet == 0) { printf("%d fd %d close\n", i, fds[i].fd); fds[i].fd = -1; conn -- ; break; } if (nRet != g_len) { printf("readn fialed nRet %d %s\n", nRet, strerror(errno)); break; } if(g_verify == 1) { struct data* d = (struct data* )(rbuff+sizeof(struct timeval)); if( d->compare() < 0) { printf("format error %s\n");fflush(NULL); } } unsigned long long use_time1 = (time_f.tv_sec - time_e.tv_sec) * 1000000 + (time_f.tv_usec - time_e.tv_usec); total_time1 += use_time1; g_count1++; g_count2++; } } } gettimeofday(&time_d, NULL); unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec); total_time2 += use_time2; return 0; }
int Sock() { pthread_t mythread; nRet = pthread_create(&mythread, NULL, thread_function, NULL); if ( nRet < 0 ) { printf("error creating thread."); abort(); } g_sfd = listen_net(g_port); if (g_sfd < 0) { printf("listen_net failed:%s \n", strerror(errno)); return -1; } g_afd = my_accept(g_sfd); if (g_afd < 0) { printf("my_accept failed:%s \n", strerror(errno)); return -1; } close(g_sfd); g_sfd = 0; nRet = pthread_join(mythread, NULL); if ( nRet < 0) { printf("error joining thread."); abort(); } while (true) { if (_HandleSignal() < 0) break; gettimeofday(&time_a, NULL); nRet = write(g_afd, wbuff, g_len); gettimeofday(&time_b, NULL); if (nRet < 0) { printf("fialed %s\n", strerror(errno)); return -1; } if(g_usleep > 0) usleep(g_usleep); gettimeofday(&time_c, NULL); nRet = read(g_cfd, rbuff, g_len); gettimeofday(&time_d, NULL); if (nRet < 0) { printf("fialed %s\n", strerror(errno)); return -1; } g_count1++; g_count2++; unsigned long long use_time1 = (time_b.tv_sec - time_a.tv_sec) * 1000000 + (time_b.tv_usec - time_a.tv_usec); total_time1 += use_time1; unsigned long long use_time2 = (time_d.tv_sec - time_c.tv_sec) * 1000000 + (time_d.tv_usec - time_c.tv_usec); total_time2 += use_time2; } return 0; }
/* {{{ data_accept */ databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp) { php_sockaddr_storage addr; socklen_t size; #ifdef HAVE_FTP_SSL SSL_CTX *ctx; zend_long ssl_ctx_options = SSL_OP_ALL; int err, res; zend_bool retry; #endif if (data->fd != -1) { goto data_accepted; } size = sizeof(addr); data->fd = my_accept(ftp, data->listener, (struct sockaddr*) &addr, &size); closesocket(data->listener); data->listener = -1; if (data->fd == -1) { efree(data); return NULL; } data_accepted: #ifdef HAVE_FTP_SSL /* now enable ssl if we need to */ if (ftp->use_ssl && ftp->use_ssl_for_data) { ctx = SSL_CTX_new(SSLv23_client_method()); if (ctx == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to create the SSL context"); return 0; } #if OPENSSL_VERSION_NUMBER >= 0x0090605fL ssl_ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; #endif SSL_CTX_set_options(ctx, ssl_ctx_options); data->ssl_handle = SSL_new(ctx); if (data->ssl_handle == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to create the SSL handle"); SSL_CTX_free(ctx); return 0; } SSL_set_fd(data->ssl_handle, data->fd); if (ftp->old_ssl) { SSL_copy_session_id(data->ssl_handle, ftp->ssl_handle); } do { res = SSL_connect(data->ssl_handle); err = SSL_get_error(data->ssl_handle, res); switch (err) { case SSL_ERROR_NONE: retry = 0; break; case SSL_ERROR_ZERO_RETURN: retry = 0; SSL_shutdown(data->ssl_handle); break; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: { php_pollfd p; int i; p.fd = ftp->fd; p.events = (err == SSL_ERROR_WANT_READ) ? (POLLIN|POLLPRI) : POLLOUT; p.revents = 0; i = php_poll2(&p, 1, 300); retry = i > 0; } break; default: php_error_docref(NULL, E_WARNING, "data_accept: SSL/TLS handshake failed"); SSL_shutdown(data->ssl_handle); SSL_free(data->ssl_handle); return 0; } } while (retry); data->ssl_active = 1; } #endif return data; }
bool SocketServer::doAccept() { m_services.lock(); m_services_poller.clear(); for (unsigned i = 0; i < m_services.size(); i++) { m_services_poller.add(m_services[i].m_socket, true, false, true); } assert(m_services.size() == m_services_poller.count()); const int accept_timeout_ms = 1000; const int ret = m_services_poller.poll(accept_timeout_ms); if (ret < 0) { // Error occured, indicate error to caller by returning false m_services.unlock(); return false; } if (ret == 0) { // Timeout occured m_services.unlock(); return true; } bool result = true; for (unsigned i = 0; i < m_services_poller.count(); i++) { const bool has_read = m_services_poller.has_read(i); if (!has_read) continue; // Ignore events where read flag wasn't set ServiceInstance & si = m_services[i]; assert(m_services_poller.is_socket_equal(i, si.m_socket)); const NDB_SOCKET_TYPE childSock = my_accept(si.m_socket, 0, 0); if (!my_socket_valid(childSock)) { // Could not 'accept' socket(maybe at max fds), indicate error // to caller by returning false result = false; continue; } SessionInstance s; s.m_service = si.m_service; s.m_session = si.m_service->newSession(childSock); if (s.m_session != 0) { m_session_mutex.lock(); m_sessions.push_back(s); startSession(m_sessions.back()); m_session_mutex.unlock(); } } m_services.unlock(); return result; }
void echotester (int sock) { setcntl(sock, F_SETFL, O_NONBLOCK, "O_NONBLOCK"); long long sent = 0, recvd = 0, recvdnow = 0; int ptrsend = 0; int ptrrecv = 0; struct pollfd pollfd = { .fd = sock, .events = POLLIN | POLLOUT, }; struct timeval tb, ti, te; gettimeofday(&tb, NULL); ti = tb; while (1) { int ret = poll(&pollfd, 1, 1000 /*ms*/); if (ret == -1) { perror("poll"); exit(1); } if (pollfd.revents & POLLIN) { ssize_t ret = read(sock, bufin, BUFLEN); if (ret == -1) { perror("read"); exit(1); } size_t pr = 0; while (ret) { ssize_t size = ret; if (size > BUFLEN - ptrrecv) size = BUFLEN - ptrrecv; if (memcmp(bufin + pr, bufout + ptrrecv, size) != 0) { fprintf(stderr, "\ndata differ (sent=%Li revcd=%Li ptrsend=%i ptrrecv=%i ret=%i size=%i)\n", sent, recvd, ptrsend, ptrrecv, (int)ret, (int)size); int i = 0; for (i = 0; i < size; i++) if (bufin[i + pr] != bufout[i + ptrrecv]) { printf("offset-diff @%Li @0x%Lx\n", i + recvd, i + recvd); break; } int j = i > 16? i - 16: 0; int k = i + 16 < size? i + 16: size - 1; while (j++ < k) printf("@%Lx:R%02x/S%02x ", j + recvd, bufin[j + pr], bufout[j + ptrrecv]); printf("\n"); exit(1); } recvd += size; recvdnow += size; ptrrecv = (ptrrecv + size) & (BUFLEN - 1); ret -= size; pr += size; } } if (pollfd.revents & POLLOUT) { ssize_t size = BUFLEN - ptrsend; ssize_t ret = write(sock, bufout + ptrsend, size); if (ret == -1) { perror("write"); exit(1); } sent += ret; ptrsend = (ptrsend + ret) & (BUFLEN - 1); } gettimeofday(&te, NULL); if (te.tv_sec - ti.tv_sec > 1) { printbw(te.tv_sec - tb.tv_sec, te.tv_usec - tb.tv_usec, recvd, "avg:"); printbw(te.tv_sec - ti.tv_sec, te.tv_usec - ti.tv_usec, recvdnow, "now:"); printsz(recvd, "size:"); printf("-----\r"); fflush(stdout); ti = te; recvdnow = 0; } } my_close(sock); } void echoserver (int sock) { setcntl(sock, F_SETFL, O_NONBLOCK, "O_NONBLOCK"); int ptrsend = 0; int ptrrecv = 0; size_t inbuf = 0; struct pollfd pollfd = { .fd = sock, .events = POLLIN | POLLOUT, }; while (1) { pollfd.events = 0; if (inbuf < BUFLEN) pollfd.events |= POLLIN; if (inbuf) pollfd.events |= POLLOUT; int ret = poll(&pollfd, 1, 1000 /*ms*/); if (ret == -1) { perror("poll"); exit(1); } if (pollfd.revents & POLLIN) { ssize_t maxrecv = BUFLEN - inbuf; if (maxrecv > BUFLEN - ptrrecv) maxrecv = BUFLEN - ptrrecv; ssize_t ret = read(sock, bufin + ptrrecv, maxrecv); if (ret == -1) { perror("read"); exit(1); } inbuf += ret; ptrrecv = (ptrrecv + ret) & (BUFLEN - 1); } if (pollfd.revents & POLLOUT) { ssize_t maxsend = inbuf; if (maxsend > BUFLEN - ptrsend) maxsend = BUFLEN - ptrsend; ssize_t ret = write(sock, bufin + ptrsend, maxsend); if (ret == -1) { perror("write"); exit(1); } inbuf -= ret; ptrsend = (ptrsend + ret) & (BUFLEN - 1); } } my_close(sock); } int main (int argc, char* argv[]) { int op; const char* host = "localhost"; int port = 10102; int server = 0; int i; //int nodelay = 0; while ((op = getopt(argc, argv, "hp:d:fs")) != EOF) switch(op) { case 'h': help(); return 0; case 'p': port = atoi(optarg); break; case 'd': host = optarg; break; // case 'f': // nodelay = 1; // break; case 's': server = 1; break; default: printf("option '%c' not recognized\n", op); help(); return 1; } for (i = 0; i < BUFLEN; i++) { #if 1 bufout[i] = random() >> 23; #else char c = i & 0x0f; c += c > 9? 'a' - 10: '0'; bufout[i] = c; #endif } int sock = my_socket(); if (server) { printf("waiting on port %i\n", port); my_bind_listen(sock, port); int clisock = my_accept(sock); echoserver(clisock); close(sock); } else { printf("remote host: %s\n" "port: %i\n", host, port); my_connect(host, port, sock); echotester(sock); } return 0; }
int main(int argc, const char *argv[]) { int listenfd, connfd; struct sockaddr_in servaddr, cliaddr; socklen_t cliaddr_len; char buf[MAXLINE]; char str[INET_ADDRSTRLEN]; int n; pid_t pid; int filefd; listenfd = my_socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); my_bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); my_listen(listenfd, 20); printf("Accepting connections...\n"); while (1) { cliaddr_len = sizeof(cliaddr); connfd = my_accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); pid = fork(); if (pid == -1) { perror("call to fork"); exit(1); } else if (pid == 0) { my_close(listenfd); n = my_read(connfd, buf, MAXLINE); filefd = open(buf, O_RDWR | O_CREAT | O_TRUNC, 0666); my_write(filefd, &buf[strlen(buf)+1], strlen(&buf[strlen(buf)+1])); while ((n = my_read(connfd, buf, MAXLINE))) { printf("Received from %s at PORT %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port)); if (filefd < 0) { perror("create file error"); exit(1); } //for (i = 0; i < n; i++) //{ //buf[i] = toupper(buf[i]); //} my_write(filefd, buf, n); } my_close(filefd); exit(0); } else { my_close(connfd); } } return 0; }
int main(int argc, const char *argv[]) { int listenfd, connfd; struct sockaddr_in servaddr, cliaddr; socklen_t cliaddr_len; char buf[MAXLINE]; char str[INET_ADDRSTRLEN]; int n, i; pid_t pid; listenfd = my_socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); my_bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); my_listen(listenfd, 20); printf("Accepting connections...\n"); while (1) { cliaddr_len = sizeof(cliaddr); connfd = my_accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); pid = fork(); if (pid == -1) { perror("call to fork"); exit(1); } else if (pid == 0) { my_close(listenfd); while (1) { n = my_read(connfd, buf, MAXLINE); if (n == 0) { printf("The other side has been closed.\n"); break; } printf("Received from %s at PORT %d\n", inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port)); for (i = 0; i < n; i++) { buf[i] = toupper(buf[i]); } my_write(connfd, buf, n); } my_close(connfd); exit(0); } else { my_close(connfd); } } return 0; }
/* ** We have something to read from a file descriptor */ static void trace_ip_ready_input(ErlDrvData handle, ErlDrvEvent fd) { TraceIpData *data = (TraceIpData *) handle; int client; if (!(data->flags & FLAG_LISTEN_PORT) && event2sock(fd) == data->listenfd) { /* ** Someone tries to connect to already connected port, ** just accept and close. */ if (!IS_INVALID_SOCKET((client = my_accept(data->listenfd)))) { closesocket(client); } return; } if (event2sock(fd) == data->listenfd) { /* ** Maybe accept, we are a listen port... */ if (!IS_INVALID_SOCKET((client = my_accept(data->listenfd)))) { data->fd = client; set_nonblocking(client); if (data->que[data->questart] != NULL) { my_driver_select(data, data->fd, FLAG_WRITE | FLAG_READ, SELECT_ON); } else { my_driver_select(data, data->fd, FLAG_READ, SELECT_ON); } data->flags &= ~(FLAG_LISTEN_PORT); } return; } /* * It is a probably EOF because the other end closed the socket, * but better make sure. */ if ((SOCKET)(long)fd == data->fd) { #ifdef __WIN32__ close_client(data); #else int res; char sbuf[128]; if ((res = read(data->fd, sbuf, sizeof sbuf)) == 0) { close_client(data); } /* * Something else. Just ignore it. * * When /dev/poll is used on Solaris, this callback can * be called even if there is nothing to read. An attempt * to read will result in an EAGAIN error. */ #ifdef DEBUG if (res < 0) { fprintf(stderr, "Read on fd %d failed with errno=%d\r\n", data->fd, errno); } #endif #endif return; } ASSERT(0); }
/* {{{ data_accept */ databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp) { php_sockaddr_storage addr; socklen_t size; #if HAVE_OPENSSL_EXT SSL_CTX *ctx; zend_long ssl_ctx_options = SSL_OP_ALL; #endif if (data->fd != -1) { goto data_accepted; } size = sizeof(addr); data->fd = my_accept(ftp, data->listener, (struct sockaddr*) &addr, &size); closesocket(data->listener); data->listener = -1; if (data->fd == -1) { efree(data); return NULL; } data_accepted: #if HAVE_OPENSSL_EXT /* now enable ssl if we need to */ if (ftp->use_ssl && ftp->use_ssl_for_data) { ctx = SSL_CTX_new(SSLv23_client_method()); if (ctx == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to create the SSL context"); return 0; } #if OPENSSL_VERSION_NUMBER >= 0x0090605fL ssl_ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; #endif SSL_CTX_set_options(ctx, ssl_ctx_options); data->ssl_handle = SSL_new(ctx); if (data->ssl_handle == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to create the SSL handle"); SSL_CTX_free(ctx); return 0; } SSL_set_fd(data->ssl_handle, data->fd); if (ftp->old_ssl) { SSL_copy_session_id(data->ssl_handle, ftp->ssl_handle); } if (SSL_connect(data->ssl_handle) <= 0) { php_error_docref(NULL, E_WARNING, "data_accept: SSL/TLS handshake failed"); SSL_shutdown(data->ssl_handle); SSL_free(data->ssl_handle); return 0; } data->ssl_active = 1; } #endif return data; }
/* {{{ data_accept */ databuf_t* data_accept(databuf_t *data, ftpbuf_t *ftp) { php_sockaddr_storage addr; socklen_t size; #ifdef HAVE_FTP_SSL SSL_CTX *ctx; SSL_SESSION *session; int err, res; zend_bool retry; #endif if (data->fd != -1) { goto data_accepted; } size = sizeof(addr); data->fd = my_accept(ftp, data->listener, (struct sockaddr*) &addr, &size); closesocket(data->listener); data->listener = -1; if (data->fd == -1) { efree(data); return NULL; } data_accepted: #ifdef HAVE_FTP_SSL /* now enable ssl if we need to */ if (ftp->use_ssl && ftp->use_ssl_for_data) { ctx = SSL_get_SSL_CTX(ftp->ssl_handle); if (ctx == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to retreive the existing SSL context"); return 0; } data->ssl_handle = SSL_new(ctx); if (data->ssl_handle == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to create the SSL handle"); return 0; } SSL_set_fd(data->ssl_handle, data->fd); if (ftp->old_ssl) { SSL_copy_session_id(data->ssl_handle, ftp->ssl_handle); } /* get the session from the control connection so we can re-use it */ session = SSL_get_session(ftp->ssl_handle); if (session == NULL) { php_error_docref(NULL, E_WARNING, "data_accept: failed to retreive the existing SSL session"); SSL_free(data->ssl_handle); return 0; } /* and set it on the data connection */ res = SSL_set_session(data->ssl_handle, session); if (res == 0) { php_error_docref(NULL, E_WARNING, "data_accept: failed to set the existing SSL session"); SSL_free(data->ssl_handle); return 0; } do { res = SSL_connect(data->ssl_handle); err = SSL_get_error(data->ssl_handle, res); switch (err) { case SSL_ERROR_NONE: retry = 0; break; case SSL_ERROR_ZERO_RETURN: retry = 0; SSL_shutdown(data->ssl_handle); break; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: { php_pollfd p; int i; p.fd = ftp->fd; p.events = (err == SSL_ERROR_WANT_READ) ? (POLLIN|POLLPRI) : POLLOUT; p.revents = 0; i = php_poll2(&p, 1, 300); retry = i > 0; } break; default: php_error_docref(NULL, E_WARNING, "data_accept: SSL/TLS handshake failed"); SSL_shutdown(data->ssl_handle); SSL_free(data->ssl_handle); return 0; } } while (retry); data->ssl_active = 1; } #endif return data; }