int main( int argc , char **argv ) { unsigned long cliaddr; unsigned short cliport; int ufd , nfd; fprintf( stderr, "start\n"); uinit(INADDR_ANY); fprintf( stderr, "inited\n"); ufd = usocket_serv( 8 ); fprintf( stderr, "socketd %d\n", ufd ); ulisten( ufd ); fprintf( stderr, "listened\n"); while(1){ int r =uproc(); /* fprintf( stderr, "uproced %d\n" , r);*/ nfd = uaccept( ufd , &cliaddr ,&cliport); if( nfd >= 0 ){ fprintf( stderr, "accepted! nfd:%d %x %d\n" , nfd ,cliaddr , ntohs(cliport) ); break; } } while(1){ int r = uproc(); fprintf( stderr,"." ); if( ureadable( nfd ) ){ char buffer[800]; int r; fprintf( stderr, "REadable!!\n"); bzero( buffer ,sizeof(buffer)); if( (r = uread( nfd, buffer , sizeof( buffer ) )) >= 0 ){ int wr; fprintf( stderr , "ReadData %d: [%s]\n" ,r, buffer ); wr = uwrite( nfd , buffer , r ); fprintf( stderr ,"Wrote %dbytes\n",wr ); } } usleep(100*1000); } }
void pping_server::startup() { sock = usocket(AF_INET, SOCK_STREAM, 0, __sys_call_error); if (sock == U_INVALID_SOCKET) throw USER_ENV_EXCEPTION("Failed to create socket", false); if (uNotInheritDescriptor(UHANDLE(sock), __sys_call_error) != 0) throw USER_EXCEPTION(SE4080); if (ubind_tcp(sock, port, "localhost", __sys_call_error) == U_SOCKET_ERROR) throw USER_ENV_EXCEPTION2("Failed to bind socket", usocket_error_translator(), false); if (ulisten(sock, PPING_LSTNR_QUEUE_LEN, __sys_call_error) == U_SOCKET_ERROR) throw USER_ENV_EXCEPTION("Failed to listen socket", false); uResVal res = uCreateThread(pping_server_lstn_thread_proc_st, this, &server_lstn_thread_handle, PPING_STACK_SIZE, NULL, __sys_call_error); if (res != 0) throw USER_ENV_EXCEPTION("Failed to create pping server thread", false); initialized = true; }
int main(int argc, char** argv) { #if (defined WIN32 || defined _WIN32) WSADATA wsadata; int rc = WSAStartup(MAKEWORD(2,0), &wsadata); if (rc) return 1; #endif upoll_t* upq = upoll_create(32); intptr_t sd1 = usocket(PF_INET, SOCK_STREAM, IPPROTO_TCP); printf("server: %li\n", sd1); printf("bind: %d\n", ubind(sd1, "127.0.0.1", "1976")); printf("listen: %d\n", ulisten(sd1, 128)); upoll_event_t ev1, ev2; upoll_event_t events[8]; ev1.events = UPOLLIN; ev1.data.fd = sd1; upoll_ctl(upq, UPOLL_CTL_ADD, sd1, &ev1); int r = 0, x = 0; while (1) { int i, e; e = upoll_wait(upq, events, 8, -1); printf("events: %i\n", e); if (x++ == 50) return 0; for (i = 0; i < e; i++) { char buf[4096]; if (events[i].events & UPOLLERR) { printf("ERROR on %li\n", events[i].data.fd); upoll_ctl(upq, UPOLL_CTL_DEL, events[i].data.fd, NULL); uclose(events[i].data.fd); } else if (events[i].events & UPOLLIN) { printf("have POLLIN on %li\n", events[i].data.fd); if (events[i].data.fd == sd1) { int sd2 = uaccept(sd1); printf("uaccept: %i\n", sd2); ev2.events = UPOLLIN; ev2.data.fd = sd2; upoll_ctl(upq, UPOLL_CTL_ADD, sd2, &ev2); } else { int cfd = events[i].data.fd; memset(buf, 0, 4096); printf("client input...\n"); r = uread(cfd, buf, 4096); printf("read %i bytes\n", r); if (r == 0) { printf("EOF DETECTED\n"); upoll_ctl(upq, UPOLL_CTL_DEL, events[i].data.fd, NULL); uclose(events[i].data.fd); } else { ev2.events = UPOLLOUT; upoll_ctl(upq, UPOLL_CTL_MOD, cfd, &ev2); } } } else if (events[i].events & UPOLLOUT) { printf("client writable...\n"); int cfd = events[i].data.fd; int w = uwrite(cfd, buf, r); ev2.events = UPOLLIN; printf("wrote %i bytes, mod for UPOLLIN again\n", w); upoll_ctl(upq, UPOLL_CTL_MOD, cfd, &ev2); } } } #if (defined WIN32 || defined _WIN32) WSACleanup(); #endif return 0; }