正在加载图片...
An example concurrent ECHO server An example concurrent ECHO server CPechod-echo data until end of fileI while (I)( char buf[BUFSIZE] en= sizeof(fsin) c:recv(fd, buf, sizeof buf, O): if (sock =s INVALID SOCKET number \n", GetLastError OE if Cbeginthread(void (")(void"))TCPechod, STKSIZE. (void*)sock)< o)[ rexitc-beginthread: %s\n, strerror(errno) ce= recv(fd, buf, sizeof buf, O:) if (cc = SOCKET_ ERROR) fprintf(stderr. "echo recv error: Zd\n" GetLo return 1: /not reached n Singly-Threaded, Concurrent Chapter 4: Server Software Design Servers (TCP) Chapter goal: a Data-driven processing in a serve Algorithm and issues s Iterative servers Concurrent connection-oriented Arrange for a single tread to keep TCP connections servers Singly-threaded, concurrent The server uses the arrival of data to trigger processing a Multiprotocol servers Consider previous concurrent ECHO Slave thread spends most time blocked in a call to recv s Multiservice servers uIre time Uniform, efficient management er request often b server concurrency Data-driven processing with a single thread A single thread can perform the same task Thread Structure for A Singly- & Thread Structure for A Singly- hreaded Server threaded server A single thread server must perform the duties of both the master and slave thread Server The server passes the set of socket descriptors as argument to select select returns bit mask that specifies which of the descriptors in the set is ready s The server uses the order descriptors become ready to decide how to The server uses the descriptors to distinguish 88 哈工大计算机学院 李全龙 Network Application Development Server Software Design 43 An example concurrent ECHO server if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); while (1) { alen = sizeof(fsin); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock == INVALID_SOCKET) errexit("accept: error number\n", GetLastError()); if (_beginthread((void (*)(void *))TCPechod, STKSIZE, (void *)ssock) < 0) { errexit("_beginthread: %s\n", strerror(errno)); } } return 1; /* not reached */} 哈工大计算机学院 李全龙 Network Application Development Server Software Design 44 An example concurrent ECHO server /*------------------------------------------------------------------------ * TCPechod - echo data until end of file *------------------------------------------------------------------------ */ int TCPechod(SOCKET fd) { char buf[BUFSIZE]; int cc; cc = recv(fd, buf, sizeof buf, 0); while (cc != SOCKET_ERROR && cc > 0) { if (send(fd, buf, cc, 0) == SOCKET_ERROR) { fprintf(stderr, "echo send error: %d\n", GetLastError()); break;} cc = recv(fd, buf, sizeof buf, 0);} if (cc == SOCKET_ERROR) fprintf(stderr, "echo recv error: %d\n", GetLastError()); closesocket(fd); return 0;} 哈工大计算机学院 李全龙 Network Application Development Server Software Design 45 Chapter 4: Server Software Design Chapter goal: „ Algorithm and issues „ Iterative servers „ Concurrent, connection-oriented servers „ Singly-threaded, concurrent servers „ Multiprotocol servers „ Multiservice servers „ Uniform, efficient management of server concurrency 哈工大计算机学院 李全龙 Network Application Development Server Software Design 46 Singly-Threaded, Concurrent Servers (TCP) „ Data-driven processing in a server „ For applications where I/O dominates the cost of preparing a response to a request, a server can use asynchronous I/O to provide apparent concurrency „ Arrange for a single server thread to keep TCP connections open to multiple clients, and have the server handle a given connection when data arrives „ The server uses the arrival of data to trigger processing „ Consider previous concurrent ECHO server „ Slave thread spends most time blocked in a call to recv „ Concurrent servers that require little processing time per request often behave in a sequential manner „ Data-driven processing with a single thread „ A single thread can perform the same task 哈工大计算机学院 李全龙 Network Application Development Server Software Design 47 Thread Structure for A Singly￾threaded Server server Server application thread Operating system Socket for Connection requests Socket for individual connection … 哈工大计算机学院 李全龙 Network Application Development Server Software Design 48 Thread Structure for A Singly￾threaded Server „ A single thread server must perform the duties of both the master and slave threads „ The server passes the set of socket descriptors as argument to select „ select returns bit mask that specifies which of the descriptors in the set is ready „ The server uses the order in which descriptors become ready to decide how to proceed „ The server uses the descriptors to distinguish between master and slave operations
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有