/* load up two integers and a string using xdr, then ship them */ int main(int argc, char* argv[]) { int sock; int test_number_a; int test_number_b; float test_number_c; char *test_string = NULL; char buffer[80]; char *service = get_port(); XDR xdrobject; XDR *xdrstream = &xdrobject; /* Get a socket (UDP) */ sock = passiveUDP(service); read(sock, buffer, 80); close(sock); /* XDR a message */ xdrmem_create(xdrstream, buffer, 80, XDR_DECODE); xdr_int(xdrstream, &test_number_a); xdr_int(xdrstream, &test_number_b); xdr_float(xdrstream, &test_number_c); xdr_wrapstring(xdrstream, &test_string); printf("%d, %d, %f %s\n", test_number_a, test_number_b, test_number_c, test_string); /* send the message */ xdr_destroy(xdrstream); return 0; }
/*------------------------------------------------------------------------ * main - Iterative server for DAYTIME service *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { char *service = "daytime"; /* service name or port number */ char buf[LINELEN+1]; /* buffer for one line of text */ struct sockaddr_in fsin; /* the request from address */ int alen; /* from-address length */ int tsock; /* TCP master socket */ int usock; /* UDP socket */ int nfds; fd_set rfds; /* readable file descriptors */ switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: daytimed [port]\n"); } tsock = passiveTCP(service, QLEN); usock = passiveUDP(service); nfds = MAX(tsock, usock) + 1; /* bit number of max fd */ FD_ZERO(&rfds); while (1) { FD_SET(tsock, &rfds); FD_SET(usock, &rfds); if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0) errexit("select error: %s\n", strerror(errno)); if (FD_ISSET(tsock, &rfds)) { int ssock; /* TCP slave socket */ alen = sizeof(fsin); ssock = accept(tsock, (struct sockaddr *)&fsin, &alen); if (ssock < 0) errexit("accept failed: %s\n", strerror(errno)); daytime(buf); (void) write(ssock, buf, strlen(buf)); (void) close(ssock); } if (FD_ISSET(usock, &rfds)) { alen = sizeof(fsin); if (recvfrom(usock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); daytime(buf); (void) sendto(usock, buf, strlen(buf), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } } }
/*------------------------------------------------------------------------ * main - Iterative UDP server for TIME service *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { struct sockaddr_in fsin; /* the from address of a client */ char *service = "time"; /* service name or port number */ char buf[1]; /* "input" buffer; any size > 0 */ int sock; /* server socket */ time_t now; /* current time */ unsigned int alen; /* from-address length */ switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: UDPtimed [port]\n"); } sock = passiveUDP(service); while (1) { alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); (void) time(&now); now = htonl((unsigned long)(now + UNIXEPOCH)); (void) sendto(sock, (char *)&now, sizeof(now), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } }
/* load up two integers and a string using xdr, then ship them */ int main(int argc, char* argv[]) { int sock; int test_number_a; int test_number_b; float test_number_c; char test_string[512]; char buffer[1024]; XDR xdrobject; XDR *xdrstream = &xdrobject; /* Get a socket (UDP) */ sock = passiveUDP(get_port()); read(sock, buffer, sizeof(buffer)); close(sock); /* XDR a message */ xdrmem_create(xdrstream, buffer, sizeof(buffer), XDR_DECODE); xdr_int(xdrstream, &test_number_a); xdr_int(xdrstream, &test_number_b); xdr_float(xdrstream, &test_number_c); xdr_wrapstring(xdrstream, (char **)&test_string); // printf("(recv) string success? %i\n",xdr_wrapstring(xdrstream, &test_string)); printf("%d, %d, %f, %s\n", test_number_a, test_number_b, test_number_c, test_string); /* send the message */ xdr_destroy(xdrstream); return 0; }
/*------------------------------------------------------------------------ * main - Super-server main program *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { struct service *psv, /* service table pointer */ *fd2sv[NOFILE]; /* map fd to service pointer */ int fd, nfds; fd_set afds, rfds; /* readable file descriptors */ switch (argc) { case 1: break; case 2: portbase = (u_short) atoi(argv[1]); break; default: errexit("usage: superd [portbase]\n"); } nfds = 0; FD_ZERO(&afds); for (psv = &svent[0]; psv->sv_name; ++psv) { if (psv->sv_useTCP) psv->sv_sock = passiveTCP(psv->sv_name, QLEN); else psv->sv_sock = passiveUDP(psv->sv_name); fd2sv[psv->sv_sock] = psv; nfds = MAX(psv->sv_sock+1, nfds); FD_SET(psv->sv_sock, &afds); } (void) signal(SIGCHLD, reaper); while (1) { memcpy(&rfds, &afds, sizeof(rfds)); if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0) { if (errno == EINTR) continue; errexit("select error: %s\n", strerror(errno)); } for (fd=0; fd<nfds; ++fd) if (FD_ISSET(fd, &rfds)) { psv = fd2sv[fd]; if (psv->sv_useTCP) doTCP(psv); else psv->sv_func(psv->sv_sock); } } }
int main(int argc, char **argv) { int nbytes, err; SOCKET sock; struct sockaddr_in sin; char *buf_ptr; int sin_len; if (argc != 2) { printf("Usage: %s <Port>\n", argv[0]); exit (1); } // Create a socket to read from sock = passiveUDP(atoi(argv[1])); buf_ptr = buf; // Set the options such that the recieve buffer size is set at the // application layer if (err = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, buf_ptr, sizeof(buf)) !=0) { printf ("Error in setsockopt!\n"); exit (1); } printf("setsockopt returns: %d\n", err) ; printf("Socket: %d\n", sock); printf ("Waiting for data ......\n"); sin_len = sizeof(sin); // Receive data from an unconneted (UDP) socket // Note that this function will block until data arrives at the socket! if ((nbytes = recvfrom (sock, buf, BUFSIZE, 0, (struct sockaddr *)&sin, &sin_len)) < 0) { perror ("recvfrom error"); exit(1); } printf ("Received %d bytes\t", nbytes); printf ("From host: %s\n", inet_ntoa (sin.sin_addr)); // Cleanup after yourself WSACleanup(); exit(0); }
/*------------------------------------------------------------------------ * main - Iterative UDP server for TIME service *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { struct sockaddr_in fsin; /* the from address of a client */ char *service = get_port(); /* service name or port number */ char buf[8]; /* "input" buffer; any size > 0 */ int sock; /* server socket */ // time_t now; /* current time */ int alen; /* from-address length */ struct timeval tv; switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: UDPtimed [port]\n"); } sock = passiveUDP(service); while (1) { alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); (void) gettimeofday(&tv, NULL ); printf("tv_sec: %x\n", tv.tv_sec); printf("tv_usec: %x\n", tv.tv_usec); tv.tv_sec = htonl(tv.tv_sec); tv.tv_usec = htonl(tv.tv_usec); memcpy(&buf[0], &tv.tv_sec, 4); memcpy(&buf[4], &tv.tv_usec, 4); // now = htonl((u_long)(now + UNIXEPOCH));u} (void) sendto(sock, (char *)&buf, sizeof(buf), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } }
int main(int agrc, char *argv[]) { struct sockaddr_in fsin; char *service="TEST"; char buf[1]; int sock; time_t now; unsigned int alen; //printf("agrc<%d>\n",agrc); switch(agrc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: UDPtimeed[port]\n"); } sock = passiveUDP(service); printf("socket<%d>\n",sock); while(1) { alen = sizeof(fsin); if(recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&fsin, &alen)<0) errexit("recvfrom: %s\n",strerror(errno)); else printf("Read into buf<%s>\n",buf); (void)time(&now); now = htonl((unsigned long)(now+UNIXEPOCH)); (void) sendto(sock,(char *)&now,sizeof(now),0,(struct sockaddr *)&fsin, sizeof(fsin)); } }
int main(int argc, char*argv[]) { char *service = "daytime"; char buf[LINELEN + 1]; struct sockaddr_in fsin; unsigned int alen; int tsock; int usock; int nfds; fd_set rfds; tsock = passiveTCP("tcp", QLEN); usock = passiveUDP("udp"); nfds = MAX(tsock, usock) + 1; FD_ZERO(&rfds); while (1) { FD_SET(tsock, &rfds); (usock, &rfds); if (select(nfds, &rfds, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0) < 0) errexit("select error : %s\n", strerror(errno)); if (FD_ISSET(tsock, &rfds)) { int ssock; pthread_t thread_id; alen = sizeof(fsin); ssock = accept(tsock, (struct sockaddr *) &fsin, &alen); if (ssock < 0) errexit("accept failed: %s\n", strerror(errno)); if (pthread_create(&thread_id, NULL, connection_handler, (void*) &ssock) < 0) { perror("could not create thread"); return 1; } daytime(buf); (void) write(ssock, buf, strlen(buf)); (void) close(ssock); } if (FD_ISSET(usock, &rfds)) { alen = sizeof(fsin); pthread_t thread_id; if (recvfrom(usock, buf, sizeof(buf), 0, (struct sockaddr *) &fsin, &alen) < 0) errexit("recvfrom: %s\n", strerror(errno)); if (pthread_create(&thread_id, NULL, connection_handlerudp, (void*) &usock) < 0) { perror("could not create thread"); return 1; } daytime(buf); (void) sendto(usock, buf, strlen(buf), 0, (struct sockaddr*) &fsin, sizeof(fsin)); } } void *connection_handlerudp(void *socket_desc) { char buf[LINELEN + 1]; unsigned int alen; struct sockaddr_in fsin; int readsize; int sock = *(int*) socket_desc; alen = sizeof(fsin); while ((readsize = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &fsin, &alen)) > 0) { sendto(sock, buf, sizeof(buf), 0, (struct sockaddr *) &fsin, sizeof(fsin)); } if (readsize == 0) { puts("Client disconnected"); fflush(stdout); } else if (readsize == -1) { perror("recv failed"); } errexit("recvfrom: %s\n", strerror(errno)); }
// multiple-protocol super server int main(int argc, char **argv) { Service *psv, *fd2sv[NOFILE]; int fd, nfds; fd_set afds, rfds; // set port base portbase = 0; // check the arguments switch (argc) { case 1: break; case 2: portbase = atoi(argv[1]); break; default: ERROR("usage: superd [portbase]", EINVAL); return(1); } // signal handlers for exiting setCleanUp(); // check of any port base offset if (portbase == 0) checkPortBase(); // initialize array of services nfds = 0; FD_ZERO(&afds); for (psv = services; psv->service != NULL; psv++) { // allocate a socket switch (psv->protocol) { case UseUDP: psv->socket = passiveUDP(psv->service); if (psv->socket < 0) { ERRORD("passiveUDP failed", psv->socket, errno); return(2); } break; case UseTCP: psv->socket = passiveTCP(psv->service, QueueLength); if (psv->socket < 0) { ERRORD("passiveTCP failed", psv->socket, errno); return(2); } break; default: ERROR("invalid protocol in service table", EINVAL); return(2); } // map descriptor to service fd2sv[psv->socket] = psv; nfds = mymax(psv->socket+1, nfds); FD_SET(psv->socket, &afds); } // wait for a message and process it. while (1) { // copy active descriptors memcpy(&rfds, &afds, sizeof(rfds)); // wait for a calling if (select(nfds, &rfds, 0, 0, 0) < 0) { if (errno == EINTR) continue; ERROR("select error", errno); return(2); } // process active descriptors for (fd = 0; fd < nfds; fd++) { // check if active if ( ! FD_ISSET(fd, &rfds)) continue; // active, process according to protocol psv = fd2sv[fd]; switch (psv->protocol) { case UseTCP: doTCP(psv); break; case UseUDP: psv->server(psv->socket); break; default: ERRORD("invalid protocol", psv->protocol, EINVAL); return(2); } } } }