Example Servers Pt 1 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Example Servers Pt 1

Description:

Title: Iterative Connectionless Servers (UDP) Author: Bob Cotter Last modified by: Bob Cotter Created Date: 9/26/1997 3:09:25 PM Document presentation format – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 31
Provided by: BobCo64
Category:
Tags: example | servers | usage

less

Transcript and Presenter's Notes

Title: Example Servers Pt 1


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

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

3
Iterative Connectionless Servers (UDP)
  • / passUDP.cpp - passiveUDP /
  • include ltwinsock.hgt
  • 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
passivesock (service, protocol, qlen)
  • / passsock.cpp - passivesock /
  • include ltstdlib.hgt
  • include ltstring.hgt
  • include ltwinsock.hgt
  • void errexit(const char , ...)
  • u_short portbase 0 / For test servers /
  • /-----------------------------------------------
  • passivesock - allocate bind a server socket
    using TCP or UDP
  • ----------------------------------------------/

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
  • SOCKET s // socket descriptor
  • int type //socket type (SOCK_STREAM,SOCK_DGRAM)
  • memset(sin, 0, sizeof(sin))
  • sin.sin_family AF_INET
  • sin.sin_addr.s_addr INADDR_ANY

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
  • SOCKET s // socket descriptor
  • int type //socket type (SOCK_STREAM,SOCK_DGRAM)
  • memset(sin, 0, sizeof(sin))
  • sin.sin_family AF_INET
  • sin.sin_addr.s_addr INADDR_ANY

7
passivesock (service, protocol, qlen)
  • / Map service name to port number /
  • if ( pse getservbyname(service, transport)
    ) sin.sin_port htons(ntohs((u_short)pse-gts_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
passivesock (service, protocol, qlen)
  • / Allocate a socket /
  • s socket(PF_INET, type, ppe-gtp_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 (typeSOCK_STREAM listen(s, qlen)
    SOCKET_ERROR)
  • errexit(cant listen on s port d\n",
    service, GetLastError())
  • return s

9
passivesock (service, protocol, qlen)
  • / Allocate a socket /
  • s socket(PF_INET, type, ppe-gtp_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 (typeSOCK_STREAM listen(s, qlen)
    SOCKET_ERROR)
  • errexit(cant listen on s port d\n",
    service, GetLastError())
  • return s

10
Iterative Connectionless ServersTIME Server
  • / UDPtimed.cpp - main /
  • include lttime.hgt
  • include ltwinsock.hgt
  • SOCKET passiveUDP(const char )
  • void errexit(const char , ...)
  • define WINEPOCH 2208988800 // Windows epoch
  • define WSVERS MAKEWORD(2, 0)
  • /------------------------------------------------
    --
  • main - Iterative UDP server for TIME service
  • ------------------------------------------------
    /

11
Iterative Connectionless ServersTIME Server
  • int main(int argc, char argv)
  • struct sockaddr_in fsin // From address of
    client
  • char service "time"// service name or port
  • char buf2048//"input" buffer any size gt1
    packet
  • SOCKET sock // server socket
  • time_t now // current time
  • int alen // from-address length
  • WSADATA wsadata

12
Iterative Connectionless ServersTIME Server
  • switch (argc)
  • case 1
  • break
  • case 2
  • service argv1
  • break
  • default
  • errexit("usage UDPtimed port\n")
  • if (WSAStartup(WSVERS, wsadata)) errexit("WSASta
    rtup failed\n")
  • sock passiveUDP(service)

13
Iterative Connectionless ServersTIME 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
Iterative Connectionless ServersTIME 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
Iterative Connection OrientedTCPdaytimed
  • / TCPdtd.cpp - main, TCPdaytimed /
  • include ltstdlib.hgt
  • include ltwinsock.hgt
  • include lttime.hgt
  • void errexit(const char , ...)
  • void TCPdaytimed(SOCKET)
  • SOCKET passiveTCP(const char , int)
  • define QLEN 5
  • define WSVERS MAKEWORD(2, 0)
  • /------------------------------------------------
    -
  • main - Iterative TCP server for DAYTIME service
  • ------------------------------------------------
    /

16
Iterative Connection OrientedTCPdaytimed
  • void main(int argc, char argv)
  • struct sockaddr_in fsin //From address of
    client
  • char service "daytime" //Service or port
  • SOCKET msock, ssock // master slave sockets
  • int alen // from-address length
  • WSADATA wsadata
  • switch (argc)
  • case 1 break
  • case 2 service argv1
  • break
  • default errexit("usage TCPdaytimed
    port\n")

17
Iterative Connection OrientedTCPdaytimed
  • 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
Iterative Connection OrientedTCPdaytimed
  • void TCPdaytimed(SOCKET fd)
  • char pts // pointer to time string
  • time_t now // current time
  • (void) time(now)
  • pts ctime(now)
  • (void) send(fd, pts, strlen(pts), 0)

19
Concurrent Connection-OrientedTCPechod.c
  • / TCPechod.cpp - main, TCPechod /
  • include ltstdio.hgt, ltwinsock.hgt, ltprocess.hgt
  • define QLEN 5 // max connection queue length
  • define STKSIZE 16536
  • define BUFSIZE 4096
  • define WSVERS MAKEWORD(2, 0)
  • SOCKET msock, ssock // master slave sockets
  • int TCPechod(SOCKET)
  • void errexit(const char , ...)
  • SOCKET passiveTCP(const char , int)

20
Concurrent Connection-OrientedTCPechod.c
  • int main(int argc, char argv)
  • char service "echo"// service name, port
  • struct sockaddr_in fsin // address of a client
  • int alen // length of client's address
  • WSADATA wsadata
  • switch (argc)
  • case 1 break
  • case 2 service argv1
  • break
  • default errexit("usage TCPechod
    port\n")
  • if (WSAStartup(WSVERS, wsadata) ! 0)
  • errexit("WSAStartup failed\n")

21
Concurrent Connection-OrientedTCPechod.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) lt 0)
  • errexit("_beginthread s\n",
    strerror(errno))
  • return 1 / not reached /

22
Concurrent Connection-OrientedTCPechod.c
  • int TCPechod(SOCKET fd)
  • char bufBUFSIZE
  • int cc
  • cc recv(fd, buf, sizeof buf, 0)
  • while (cc ! SOCKET_ERROR cc gt 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()) clo
    sesocket(fd)
  • return 0

23
Managing multiple sockets
Start
Create Master Socket
24
Managing multiple sockets
First Client Connection
Second Client Connection
25
Single-Process ConcurrentTCPmechod.c
  • void main(int argc, char argv)
  • char service "echo"// service name/port
  • struct sockaddr_in fsin // From address of
    client
  • SOCKET msock // master server socket
  • fd_set rfds // read file descriptor
    set fd_set afds // active file descriptor
    set int alen // from-address
    length WSADATA wsdata
  • unsigned int fdndx
  • switch (argc)
  • case 1 break
  • case 2 service argv1
  • break
  • default errexit("usage TCPmechod port\n")

26
Single-Process ConcurrentTCPmechod.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) er
    rexit("select error d\n", GetLastError())

27
Single-Process ConcurrentTCPmechod.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) er
    rexit("select error d\n", GetLastError())

28
Single-Process ConcurrentTCPmechod.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) er
    rexit("select error d\n", GetLastError())

29
Single-Process ConcurrentTCPmechod.c
  • if (FD_ISSET(msock, rfds))
  • SOCKET ssock
  • 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 (fdndx0 fdndxltrfds.fd_count fdndx)
  • SOCKET fd rfds.fd_arrayfdndx
  • if (fd ! msock FD_ISSET(fd, rfds))
  • if (echo(fd) 0)
  • (void) closesocket(fd)
  • FD_CLR(fd, afds)

30
Single-Process ConcurrentTCPmechod.c
  • // echo - echo one buffer of data, returning byte
    count
  • int echo(SOCKET fd)
  • char bufBUFSIZE
  • int cc
  • 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
Write a Comment
User Comments (0)
About PowerShow.com