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

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

CS 4700 / CS 5700 Network Fundamentals
Socket Programming Application Programming Interface.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
ISP – 9 th Recitation Socket programming – Client side.
1 Socket Programming A crash-course in network programming…
CSTP FS97CS490D (cotter)1 Sockets Programming in UNIX References: Internetworking with TCP/IP Vol III - BSD version UNIX Network Programming - W. Richard.
Client Software Design Objectives: Understand principles of C/S design, with focus on clients Review Windows implementations of Socket functions.
Cs423-cotter1 Example Client Program Reference Comer & Stevens, Chapter 7.
1 Sockets Programming in Linux References: Internetworking with TCP/IP Vol III - Linux version UNIX Network Programming - W. Richard Stevens.
Tutorial on Socket Programming Data types and structures for writing client- server programs.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
Fall 2000Datacom 11 Socket Programming Review Examples: Client and Server-Diagnostics UDP versus TCP Echo.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Sirak Kaewjamnong Computer Network Systems
The Application Layer Application Services (Telnet, FTP, , WWW) Reliable Stream Transport (TCP) Connectionless Packet Delivery Service (IP) Unreliable.
CS252: Systems Programming Ninghui Li Based on slides by Prof. Gustavo Rodriguez-Rivera Topic 17: Project 5.
Lab #1: Network Programming using Sockets By J. H. Wang Nov. 28, 2011.
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS A: Windows Networking A.2. Windows Sockets.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
The Sockets Library and Concepts Rudra Dutta CSC Spring 2007, Section 001.
Example Servers Pt 2 Objective: To discuss key aspects of various server implementations.
Elementary TCP Sockets UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Introduction to Socket
Socket Programming Lab 1 1CS Computer Networks.
Cs423-cotter1 Concurrency Issues in Client/Server Applications Chapters 15,16, 28.
Sockets Socket = abstraction of the port concept: –Application programs request that the operating system create a socket when one is needed –O.S. returns.
Chapter 2 Applications and Layered Architectures Sockets.
C Programming in Linux Sockets. All Internet Applications use Sockets to Communicate Servers use passive sockets to listen Clients use active sockets.
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
Network Programming By J. H. Wang Nov. 21, Outline Introduction to network programming Socket programming –BSD Socket –WinSock –Java Socket Exercises.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
实验讲解 1 、套接字的使用实例. D.E.Comer,D.L.Stevens, TCP/IP 网络互连技术 卷 III: 客户服务器编程和应用 Windows 套接字版 清华大学出版社 清华大学出版社 ServerClient Time.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
1 Dimension of Server Designs r Iterative vs Concurrent r Connection-oriented vs. connectionless r Stateful and stateless m Constrained by application.
Client-Server Programming and Applications. References Douglas Comer, David Stevens, “Internetworking with TCP/IP: Client-Server Programming and Applications”,
Socket programming in C. Socket programming with TCP Client must contact server server process must first be running server must have created socket (door)
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
1 Vehicle Networking Networks Instruction 1 – Echo client/server in C Jeroen Voeten ES, 2012.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
Sockets and Beginning Network Programming
Socket programming in C
Socket programming Péter Verhás August 2002
Part 2 Socket Programming UDP.
Socket Programming in C
Berkeley API Socket Programming
TCP Sockets Programming
Berkeley API Socket Programming
Chapter 06. UDP Server/Client.
Socket Programming(1/2)
Socket Programming Neil Tang 09/08/2008
Internet Networking recitation #8
Berkeley API Socket Programming
Socket programming in C
Presentation transcript:

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

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 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 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 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 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 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 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 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 Iterative Connectionless Servers TIME Server /* UDPtimed.cpp - main */ #include SOCKETpassiveUDP(const char *); voiderrexit(const char *,...); #defineWINEPOCH // Windows epoch #defineWSVERS MAKEWORD(2, 0) /* * main - Iterative UDP server for TIME service * */

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 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 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 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 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 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 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 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 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 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 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 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; }

Managing multiple sockets 23 StartCreate Master Socket

Managing multiple sockets 24 First Client ConnectionSecond Client Connection

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 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 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 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 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 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; }