Click here to load reader

主從架構和 UNIX Socket

Embed Size (px)

DESCRIPTION

主從架構和 UNIX Socket. 簡介. 網際網路提供基本的通訊服務,通訊協定軟體無法主動和遠端電腦通訊,而是由兩端的應用程式主導整個通訊:一端啟動通訊,而另一端接受之。 應用程式並不一直等待任何訊息到達,而是期待在和外界通訊前先和通訊協定軟體溝通。應用程式告知本機的通訊軟體,其所期待的特定訊息型態為何。當碰巧有吻合的新進訊息出現時,通訊協定軟體會將訊息轉交給等待的應用程式。. 簡介 ( Cont.). 當然要通訊的兩個應用程式不能同時等待相同的訊息型態,必須是一方主動開始,此時另一方被動的等待。 - PowerPoint PPT Presentation

Citation preview

  • UNIX Socket

  • (Cont.) (client-server) (client) (server)

  • (Cont.)Client server client server server clientClient server server clientServer client client serverserver Server (Ex: file server)

  • Cloud

    { 1

    h

    h

    h

    Internet

    A{ 1

    h

    h

    h

    { 2

    h

    h

    h

    A{ 2

  • (identifier) client client server server client

  • (Cont.)TCP 16 bits (protocol port number) server TCP

  • concurrency () (thread) concurrency client client client client thread thread thread

  • thread thread concurrent

  • (Cont.) N client N+1 thread

  • TCP client port number TCP segment client port number TCP segment (source port) port number (destination port) TCP client client

  • connection oriented connectionless client connection oriented connectionless

  • ()

  • (Cont.)

  • (API)API (Application Program Interface) API (procedure) API API API API

  • Socket APISocket API BSD UNIX Berkley Univ. UNIX TCP/IP (procedure) OS Microsoft Windows (Winsock) UNIX OS (Ex: Solaris) socket API sockets

  • Sockets Socket Socket BSD Unix OS socket Unix OS socket OS socket API socket OS socket Socket socket API socket ()

  • Sockets Socket (Cont.) socket OS OS socket socket

  • Socket UNIX I/OUNIX socket I/O UNIX I/O open-read-write-close open descriptor read write

  • Socket UNIX I/O (Cont.) socket descriptor socket socket client server socket (option) socket

  • socket

    A{

    getprotobyname

    socket

    bind

    listen

    accept

    send

    close

    {

    gethostbyname

    getprotobyname

    socket

    connect

    recv

    close

  • Socket API Functions gethostname int gethostname(char *name, int namelen)struct protoent *getprotobyname(const char *name) returns a pointer to a protoent structure (protocol entity)the members of which contain the fields of an entry in the network protocol database. accept getpeername client

  • Socket API Functions (Cont.)Socket socket descriptordescriptor = socket (protofamilytypeprotocol)Protofamily socket PF_INET (AF_INET) TCP/IP protocolPF_DECnet DEC type socket connection-oriented data stream ( SOCK_STREAM) connectionless message ( SOCK_DGRAM)

  • Socket API Functions (Cont.)Protocol socket Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type (e.g., TCP/IP).

  • Socket API Functions (Cont.)close socket (Winsock closesocket)close (socket)socket socket descriptor socket connection-oriented close socket socket

  • Socket API Functions (Cont.)bind socket bind socket port server port bind (socketlocaladdraddrlen)socket port number descriptorlocaladdr addrlen

  • Socket API Functions (Cont.) socket socket API sockaddr struct sockaddr { /* IPv4 */ u_char sa_len; /* total length of the address */ u_char sa_family; /* family of the address */ char sa_data[14]; /* the address itself */ };

  • Socket API Functions (Cont.)struct socketaddr_in { u_char sa_len; u_char sa_family; u_short sin_port; struct inaddr sin_addr; /* IP address of the computer */ char sin_zero[8]; /*unused, for padding with the same size as sockaddr */ };

  • Socket API Functions (Cont.)multihomed capabilityIP IP MAC Multi-homed IP Support . IP/Hostname, ftp.heaven.netftp.hell.netMulti-homed IP Support Login IP Anonymous Login Hostname

  • Socket API Functions (Cont.) (multi-homed) IP socket API INADDR_ANY IP IP port number

  • Socket API Functions (Cont.)listen port number (ex: bind) OS socket client listen (socketqueuesize)socket port number descriptorqueuesize socket (request queue) OS request queue request queue request queue

  • Socket API Functions (Cont.)accept connectionless socket bind connection-oriented listen socket client

  • Socket API Functions (Cont.) connection-oriented accept client request queue accept client client server newsock=accept (socketcaddresscaddresslen)socket descriptorcaddress sockaddr caddresslen

  • Socket API Functions (Cont.)accept caddress caddresslen socket socket descriptor socket descriptor socket socket socket socket

  • Socket API Functions (Cont.)connect connect connect (socketsaddresssaddresslen)socket socket descriptorsaddress sockaddr saddresslen sockaddr connection-oriented TCP connect connect accept

  • Socket API Functions (Cont.) connectionless connect socket socket connectionless connect connectionless client

  • Socket API Functions (Cont.)send socket send send (socket*datalengthflags)data length flags flags 0 0MSG-OOB (out of band)MSG_PEEKMSG_PEEKrecvrecvfrom MSG_DONTROUTE local routing table

  • Socket API Functions (Cont.)sendto sendmsg client server connectionless sendto (socket*datalengthflags*destaddressaddresslen)sendmsg sendto sendmsg (socket*msgstructflags)

  • Socket API Functions (Cont.)msgstruct msghdr struct msghdr { void *msg_name; /* protocol address */ socklen_t msg_namelen; /* size of protocol address */ struct iovec *msgiov; /* scatter/gather array */ size_t msg_iovlen; /* # elements in msg_iov */ void *msg_control; /* ancillary data */ socketlen_t msg_controllen; /* length of ancillary data */ int msg_flags; /* flags returned by recvmsg() */};

  • Socket API Functions (Cont.)msg_name points to a data structure which the sender stores the destination addressmsgiov msg_iovlen array of input/output buffer (buffer iovec structure)struct iovec { void *iov_base; /* starting address of buffer */ size_t iov_len;}msg_control msg_controllen (E.g., hop limit, next hop address)msg_flags recvmsg sendmsg

  • Socket API Functions (Cont.)recv recv socket recv (socket*bufferlengthflags)buffer length buffer flags MSG_OOBMSG_PEEKrecvfrom socket client recvfrom

  • Socket API Functions (Cont.)recvfrom (socket*bufferlengthflags*sndaddress*saddrlen) sndaddress saddrlen recvfrom sendto recvfrom

  • Socket API Functions (Cont.)sendmsg recvmsg recvfrom recvmsg (socket*msgstructflags)msgstruct sendmsg

  • Socket Socket read/write /read write socket descriptorread write socket read/write descriptor descriptor socket I/O

  • socket socket optionssetsockopt socket option setsockopt socket option setsockopt(socketleveloptnameoptvaloptlen)level= SOL_SOCK or TCP leveloptionsSO_BROADCAST (broadcast)SO_KEEPALIVE (keeps connections active by enabling the periodic transmission of messages)

  • socket (Cont.)gethostbyaddr gethostbyname IP

  • Socketthread thread thread socketSocket reference count socket socket reference count 1 reference countsocket process/thread parent child process reference count 1

  • Socketthread (Cont.)Status of client-server before call to accept returnsclientconnect()serverlistenfdconnectionrequest

  • Socketthread (Cont.)Status of client-server after return from acceptclientconnect()serverlistenfdconnectionconnfd

  • Socketthread (Cont.)Status of client-server after fork returns

    clientconnect()serverlistenfdconnectionconnfdlistenfdconnfdforkserver(child)

  • Socketthread (Cont.)Status of client-server after parent and child close appropriate socketsclientconnect()serverlistenfdconnectionconnfdserver (child)

  • Socketthread (Cont.) process/thread socket close reference count thread thread socket thread socket close

  • Socket Functions for Elementary TCP Client-Server

    socket()

    TCP client

    connect()

    write()

    read()

    close()

    socket()

    TCP server

    listen()

    accept()

    read()

    write()

    bind()

    read()

    well-known port

    blocks until connection from client

    connection establishmentTCP three-way handshake

    data (request)

    process request

    data (reply)

    end-of-file notification

    close()

  • Socket Functions for UDP Client-Server

    socket()

    UDP client

    sendto()

    recvfrom()

    close()

    socket()

    UDP server

    recvfrom()

    sendto()

    bind()

    well-known port

    blocks until datagram received from a client

    data (request)

    process request

    data (reply)

  • An ExampleA Simple Daytime Client#include unp.h int main (int argc, char **argv) { int sockfd, n; char recvline[MAXLINE+1]; struct sockaddr_in servaddr; if(argc!=2) err_quit(usage: a.out );

  • An ExampleA Simple Daytime Client (Cont.) if((sockfd=socket(AF_INET, SOCK_STREAM,0)) < 0) err_sys(socket error);

    bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(13); if(inet_pton(AF_INET, argv[1], &servaddr.sin_addr)

  • An ExampleA Simple Daytime Client (Cont.) while ((n=read(sockfd, recvline, MAXLINE)) >0) { recvline[n]=0; /* null terminate */ if (fputs(recvline, stdout) = = EOF) err_sys(fputs error); } if (n>0) error_sys(read error); exit(0); }

  • An ExampleA Simple Daytime Server#include unp.h #include

    int main(int argc, char **argv) { int listenid, connfd; struct sockaddr_in servaddr; char buf[MAXLINE]; time_t ticks;

    listenid=socket(AF_INET, SOCK_STREAM,0);

  • An ExampleA Simple Daytime Server (Cont.) bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(13);

    bind(listenid, (SA *)&servaddr, sizeof(servaddr));

    listen(listenfd, LISTENQ); for( ; ; ) { connfd=accept(listenfd, (SA *)NULL, NULL);

  • An ExampleA Simple Daytime Server (Cont.) ticks=time(NULL); snprintf(buff, sizeof(buff), %.24s\r\n, ctime(&ticks)); write(connfd, buff, strlen(buff));

    close(connfd); } }

  • ReferenceUNIX Network Programming:Networking APIsSockets and XTI, W. R. StevensPH. PTR1998.