Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Example Servers Pt 1 Objective: To discuss key aspects of various server implementations.

Similar presentations


Presentation on theme: "1 Example Servers Pt 1 Objective: To discuss key aspects of various server implementations."— Presentation transcript:

1 1 Example Servers Pt 1 Objective: To discuss key aspects of various server implementations

2 2 Server Example Outline n Support Functions –passiveUDP ( ) –passivesock ( ) n Iterative - Connectionless (UDPTimed.cpp) n Iterative - Connection-Oriented (TCPdtd.cpp) n Concurrent Connection-Oriented (TCPEchod.cpp) n Single Process Concurrent (TCPMechod.cpp)

3 3 Iterative Connectionless Servers (UDP) /* passUDP.cpp - passiveUDP */ #include SOCKET passivesock(const char *, const char *, int); /*-------------------------------------------------- * Create a passive socket for use in a UDP server *------------------------------------------------*/ SOCKET passiveUDP(const char *service) { return passivesock(service, "udp", 0); }

4 4 passivesock (service, protocol, qlen) /* passsock.cpp - passivesock */ #include voiderrexit(const char *,...); u_shortportbase = 0; /* For test servers */ /*----------------------------------------------- * passivesock - allocate & bind a server socket using TCP or UDP *----------------------------------------------*/

5 5 passivesock (service, protocol, qlen) SOCKET passivesock(const char *service, const char *transport, int qlen) { struct servent*pse;// Service info entry struct protoent *ppe;// Protocol info entry struct sockaddr_in sin;// Internet address SOCKETs;// socket descriptor inttype;//socket type (SOCK_STREAM,SOCK_DGRAM) memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;

6 6 passivesock (service, protocol, qlen) SOCKET passivesock(const char *service, const char *transport, int qlen) { struct servent*pse;// Service info entry struct protoent *ppe;// Protocol info entry struct sockaddr_in sin;// Internet address SOCKETs;// socket descriptor inttype;//socket type (SOCK_STREAM,SOCK_DGRAM) memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY;

7 7 passivesock (service, protocol, qlen) /* Map service name to port number */ if ( pse = getservbyname(service, transport) ) sin.sin_port = htons(ntohs((u_short)pse->s_port) + portbase); else if ((sin.sin_port = htons((u_short)atoi(service)))==0) errexit("can't get \"%s\" service \n", service); /* Map protocol name to protocol number */ if ( (ppe = getprotobyname(transport)) == 0) errexit("can't get \"%s\" protocol \n", transport); /* Use protocol to choose a socket type */ if (strcmp(transport, "udp") == 0) type = SOCK_DGRAM; else type = SOCK_STREAM;

8 8 passivesock (service, protocol, qlen) /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s == INVALID_SOCKET) errexit(”Socket Error: %d\n", GetLastError()); /* Bind the socket */ if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) errexit(”Bind Error: %s port: %d\n", service, GetLastError()); if (type==SOCK_STREAM && listen(s, qlen)== SOCKET_ERROR) errexit(”can’t listen on %s port: %d\n", service, GetLastError()); return s; }

9 9 passivesock (service, protocol, qlen) /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s == INVALID_SOCKET) errexit(”Socket Error: %d\n", GetLastError()); /* Bind the socket */ if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) errexit(”Bind Error: %s port: %d\n", service, GetLastError()); if (type==SOCK_STREAM && listen(s, qlen)== SOCKET_ERROR) errexit(”can’t listen on %s port: %d\n", service, GetLastError()); return s; }

10 10 Iterative Connectionless Servers TIME Server /* UDPtimed.cpp - main */ #include SOCKETpassiveUDP(const char *); voiderrexit(const char *,...); #defineWINEPOCH2208988800// Windows epoch #defineWSVERS MAKEWORD(2, 0) /*-------------------------------------------------- * main - Iterative UDP server for TIME service *------------------------------------------------ */

11 11 Iterative Connectionless Servers TIME Server int main(int argc, char *argv[]) { struct sockaddr_in fsin;// From address of client char*service = "time";// service name or port # charbuf[2048];//"input" buffer; any size >1 packet SOCKETsock; // server socket time_tnow; // current time intalen; // from-address length WSADATAwsadata;

12 12 Iterative Connectionless Servers TIME Server switch (argc) { case1: break; case2: service = argv[1]; break; default: errexit("usage: UDPtimed [port]\n"); } if (WSAStartup(WSVERS, &wsadata)) errexit("WSAStartup failed\n"); sock = passiveUDP(service);

13 13 Iterative Connectionless Servers TIME Server while (1) { alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) == SOCKET_ERROR) errexit("recvfrom: error %d\n",GetLastError()); (void) time(&now); now = htonl((u_long)(now + WINEPOCH)); (void) sendto(sock, (char *)&now, sizeof(now), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } return 1;/* not reached */ }

14 14 Iterative Connectionless Servers TIME Server while (1) { alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) == SOCKET_ERROR) errexit("recvfrom: error %d\n",GetLastError()); (void) time(&now); now = htonl((u_long)(now + WINEPOCH)); (void) sendto(sock, (char *)&now, sizeof(now), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } return 1;/* not reached */ }

15 15 Iterative Connection Oriented TCPdaytimed /* TCPdtd.cpp - main, TCPdaytimed */ #include void errexit(const char *,...); void TCPdaytimed(SOCKET); SOCKET passiveTCP(const char *, int); #define QLEN5 #define WSVERSMAKEWORD(2, 0) /*------------------------------------------------- * main - Iterative TCP server for DAYTIME service *------------------------------------------------ */

16 16 Iterative Connection Oriented TCPdaytimed void main(int argc, char *argv[]){ structsockaddr_in fsin;//From address of client char*service = "daytime"; //Service or port # SOCKETmsock, ssock;// master & slave sockets intalen;// from-address length WSADATA wsadata; switch (argc) { case1:break; case2:service = argv[1]; break; default:errexit("usage: TCPdaytimed [port]\n"); }

17 17 Iterative Connection Oriented TCPdaytimed if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); while (1) { alen = sizeof(struct sockaddr); ssock = accept(msock,(struct sockaddr*)&fsin, &alen); if (ssock == INVALID_SOCKET) errexit("accept failed: %d\n", GetLastError()); TCPdaytimed(ssock); (void) closesocket(ssock); }

18 18 Iterative Connection Oriented TCPdaytimed void TCPdaytimed(SOCKET fd) { char*pts;// pointer to time string time_tnow;// current time (void) time(&now); pts = ctime(&now); (void) send(fd, pts, strlen(pts), 0); }

19 19 Concurrent Connection-Oriented TCPechod.c /* TCPechod.cpp - main, TCPechod */ #include,, #defineQLEN5 // max connection queue length #defineSTKSIZE16536 #defineBUFSIZE4096 #defineWSVERSMAKEWORD(2, 0) SOCKETmsock, ssock;// master & slave sockets intTCPechod(SOCKET); voiderrexit(const char *,...); SOCKETpassiveTCP(const char *, int);

20 20 Concurrent Connection-Oriented TCPechod.c int main(int argc, char *argv[]) { char*service = "echo";// service name, port # structsockaddr_in fsin;// address of a client intalen;// length of client's address WSADATAwsadata; switch (argc) { case1:break; case2:service = argv[1]; break; default:errexit("usage: TCPechod [port]\n"); } if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n");

21 21 Concurrent Connection-Oriented TCPechod.c msock = passiveTCP(service, QLEN); while (1) { alen = sizeof(fsin); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock == INVALID_SOCKET) errexit("accept error %d\n", GetLastError()); if (_beginthread((void (*)(void *))TCPechod, STKSIZE, (void *)ssock) < 0) errexit("_beginthread: %s\n", strerror(errno)); } return 1;/* not reached */ }

22 22 Concurrent Connection-Oriented TCPechod.c int TCPechod(SOCKET fd){ charbuf[BUFSIZE]; intcc; cc = recv(fd, buf, sizeof buf, 0); while (cc != SOCKET_ERROR && cc > 0) { if (send(fd, buf, cc, 0) == SOCKET_ERROR) { printf("send error: %d\n",GetLastError()); break;} cc = recv(fd, buf, sizeof buf, 0); } if (cc == SOCKET_ERROR) printf("recv error: %d\n", GetLastError()); closesocket(fd); return 0; }

23 Managing multiple sockets 23 StartCreate Master Socket

24 Managing multiple sockets 24 First Client ConnectionSecond Client Connection

25 25 Single-Process Concurrent TCPmechod.c void main(int argc, char *argv[]){ char*service = "echo";// service name/port # structsockaddr_in fsin;// From address of client SOCKETmsock;// master server socket fd_setrfds;// read file descriptor set fd_setafds;// active file descriptor set intalen;// from-address length WSADATAwsdata; unsigned intfdndx; switch (argc) { case1:break; case2:service = argv[1]; break; default:errexit("usage: TCPmechod [port]\n"); }

26 26 Single-Process Concurrent TCPmechod.c if (WSAStartup(WSVERS, &wsdata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); FD_ZERO(&afds); FD_SET(msock, &afds); while (1) { memcpy(&rfds, &afds, sizeof(rfds)); if(select(FD_SETSIZE,&rfds,(fd_set*)0,(fd_set*)0, (struct timeval *)0) == SOCKET_ERROR) errexit("select error: %d\n", GetLastError());

27 27 Single-Process Concurrent TCPmechod.c if (WSAStartup(WSVERS, &wsdata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); FD_ZERO(&afds); FD_SET(msock, &afds); while (1) { memcpy(&rfds, &afds, sizeof(rfds)); if(select(FD_SETSIZE,&rfds,(fd_set*)0,(fd_set*)0, (struct timeval *)0) == SOCKET_ERROR) errexit("select error: %d\n", GetLastError());

28 28 Single-Process Concurrent TCPmechod.c if (WSAStartup(WSVERS, &wsdata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); FD_ZERO(&afds); FD_SET(msock, &afds); while (1) { memcpy(&rfds, &afds, sizeof(rfds)); if(select(FD_SETSIZE,&rfds,(fd_set*)0,(fd_set*)0, (struct timeval *)0) == SOCKET_ERROR) errexit("select error: %d\n", GetLastError());

29 29 Single-Process Concurrent TCPmechod.c if (FD_ISSET(msock, &rfds)) { SOCKETssock; alen = sizeof(fsin); ssock = accept(msock,(struct sockaddr *)&fsin,&alen); if (ssock == INVALID_SOCKET) errexit("accept: error %d\n",GetLastError()); FD_SET(ssock, &afds); } for (fdndx=0; fdndx<rfds.fd_count; ++fdndx){ SOCKET fd = rfds.fd_array[fdndx]; if (fd != msock && FD_ISSET(fd, &rfds)) if (echo(fd) == 0) { (void) closesocket(fd); FD_CLR(fd, &afds); }}

30 30 Single-Process Concurrent TCPmechod.c // echo - echo one buffer of data, returning byte count int echo(SOCKET fd) { charbuf[BUFSIZE]; intcc; cc = recv(fd, buf, sizeof buf, 0); if (cc == SOCKET_ERROR) errexit("echo recv error %d\n", GetLastError()); if (cc && send(fd, buf, cc, 0) == SOCKET_ERROR) errexit("echo send error %d\n", GetLastError()); return cc; }


Download ppt "1 Example Servers Pt 1 Objective: To discuss key aspects of various server implementations."

Similar presentations


Ads by Google