lecture 26
play

Lecture 26 Log into Linux. Copy files from - PowerPoint PPT Presentation

Lecture 26 Log into Linux. Copy files from /home/hwang/cs375/lecture26/*.* Reminder: Project 8 due today. Final project has been posted. Due by 8:00am, Monday, December 13. Questions? Tuesday, November 30 CS 375 UNIX System


  1. Lecture 26  Log into Linux. Copy files from /home/hwang/cs375/lecture26/*.*  Reminder: Project 8 due today.  Final project has been posted. Due by 8:00am, Monday, December 13.  Questions? Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 1

  2. Outline  Internet (IP) sockets  Concurrent servers  Introduction to X windows  Window managers  Remote clients Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 2

  3. Internet (IP) Sockets  IP or Internet sockets can be used to communicate between two processes on two different machines connected by a network. (Or for IPC on a single machine.)  An IP socket is an address/port-number pair and uniquely identifies an endpoint.  A IP socket pair consists of both the server and client sockets. Every TCP connection is uniquely associated with a socket pair. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 3

  4. Internet (IP) Sockets Client Server Client 198.69.10.2 206.62.226.35 102.43.83.204 client1 server client1 198.69.10.2:1500 (*:21, *.*) 102.43.83.204:1501 206.62.226.35:21 206.62.226.35:21 server server (child1) (child2) 206.62.226.35:21 206.62.226.35:21 198.69.10.2:1500 102.43.83.204:1501 client2 server (child3) 198.69.10.2:1501 206.62.226.35:21 206.62.226.35:21 198.69.10.2:1501 Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 4

  5. Internet (IP) Sockets  Just as for local sockets, the server will call socket( ) , bind( ) , listen( ) , and accept( ) .  First call socket( ) to create the socket: sfd = socket(domain, type, protocol);  The domain parameter is now PF_INET. type is usually either SOCK_DGRAM (UDP) or SOCK_STREAM (TCP). (We will use SOCK_STREAM.) The protocol should be 0. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 5

  6. Internet (IP) Sockets  bind( ) is used to assign an address (TCP port) to the socket. Address family is now AF_INET. struct sockaddr_in in_addr; // network struct sockaddr *p_addr = (struct sockaddr *)(&in_addr); in_addr.sin_family = AF_INET; in_addr.sin_port = htons(2400); in_addr.sin_addr.s_addr = htonl(INADDR_ANY); int len = sizeof(struct sockaddr_in); bind(sfd, p_addr, len); Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 6

  7. Internet (IP) Sockets  On the server we bind our socket to a particular IP address and port number.  An IP address of INADDR_ANY will bind to all local IP addresses. We could instead bind to a single IP address on a single interface.  Our server will bind to port 2400 in this example. Note the use of the htonl( ) and htons( ) routines. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 7

  8. Internet (IP) Sockets  Instead of binding to a particular port, if we specify a port number of 0 in the address we will be assigned an available port number dynamically. The getsockname( ) routine can then be used to find the port number: getsockname(sfd, p_addr, &addrlen); cout << “Port #: ” << ntohs(in_addr.sin_port) << endl;  Note the use of ntohs( ) . Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 8

  9. Internet (IP) Sockets  The server then uses listen( ) and accept( ) just as for UNIX sockets. result = listen(sfd, qlength); nfd = accept(sfd, p_addr, &len);  The p_addr parameter is a pointer to a sockaddr structure (that is really of type sockaddr_in). This will be filled with connecting client info (IP address and port number). Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 9

  10. Internet (IP) Sockets  On the client we need only socket( ) and connect( ) . We use the server IP and port numbers in the address: cfd = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in in_address; struct sockaddr *address = (struct sockaddr *)(&in_address); in_address.sun_family = AF_INET; in_address.sin_port = htons(2400); in_address.sin_addr.s_addr = inet_addr("10.10.0.9"); // csserver int len = sizeof(struct sockaddr_in); res = connect(cfd, address, len); Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 10

  11. Internet (IP) Sockets  In this example inet_addr( ) was used to convert an IP number from dotted-quad to network byte order. Typically the IP number would be obtained via gethostbyname( ) .  Since our socket file descriptor is not bound to a local endpoint, connect( ) will do this for us. This is usually what we want. You could use bind( ) to bind to a particular local port number before the call to connect( ) . Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 11

  12. Internet (IP) Sockets  read( ) and write( ) can be used to send data via the socket.  There also are recv( ) and send( ) routines that can used and often are recommended because they provide more options via their flags parameter. Consult the man pages, if you want to use these. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 12

  13. Example  network_server.cpp is an example server. It is similar to the local_server.cpp from last lecture but has been modified to use a TCP/IP socket instead of a local (UNIX) socket. It illustrates dynamic port assignment and the use of getsockname( ) .  network_client.cpp is a similarly modified version of local_client.cpp . It requires two command-line arguments, an IP address (dotted-quad notation) and a port number. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 13

  14. Concurrent Server  Skeleton code for a concurrent server ... listenfd = socket( ... ); bind( listenfd, ... ); listen(listenfd, ... ); while ( true ) { connfd = accept( listenfd, ... ); if( (pid = fork()) == 0) { close( listenfd ); doit(connfd); // or exec(...) close( connfd ); // if not exec'd exit(0); } close( connfd ); // parent } Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 14

  15. Concurrent Server  It is important that the parent call close( ) for each connected socket returned by accept( ) . This ensures that the parent does not run out of file descriptors, but more importantly it ensures that when the child closes the connected socket the client connection will be terminated. (This does not happen until the open reference count on the socket reaches zero.) Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 15

  16. Introduction to X Windows  The X Window System (aka X or X11) was released by MIT in 1984. The X.Org Foundation currently leads X development. X is available as free software.  X has features that still are not found on other display systems.  X is not an integral part of the OS. It is an application layer on top of the kernel. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 16

  17. Introduction to X Windows  X was designed to be used over a network.  On most windowing systems, the application draws directly on the screen. In X, client and server processes are used to draw images. The server takes care of drawing and managing screen contents. The client (application) sends graphics requests (draw window here, etc.) to the X server. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 17

  18. Introduction to X Windows  The client (i.e., application) and server (i.e., display) communicate over a network and may be running on different machines. This allows the ability to run programs on one machine while displaying graphics on a different one.  The client and server processes can even be running on different OSes. X servers (and clients) are available for UNIX, Windows and Mac OS. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 18

  19. Introduction to X Windows  The X server is responsible for displaying graphics. A server usually is running on the user's (local) machine. (This is not a requirement, but displaying graphics on a remote machine just tends not to be that useful.)  The X server accepts requests for graphical output from clients and sends back user input (from keyboard, mouse or touchscreen) to clients. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 19

  20. Introduction to X Windows  The base X system provides mechanism, not policy . The base system can draw overlapped, tiled, or pop-up windows. Whether and how these should be displayed is human interface policy and is left up to the window manager. The network protocol is the only interface to the base system. Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 20

  21. Introduction to X Windows High Level Application X Toolkit Window, Low level API (Xlib) Session X Network Protocol Managers Base System (Server) Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 21

  22. Window Managers  The window manager is a special client that acts as interface between user and X server.  The window manager provides window decorations (borders), maintains window focus, window iconification, and menus (that can be used to launch applications).  There are many, many window managers available: twm, mwm, FVWM, AfterStep, Blackbox, KWin (KDE), Metacity (Gnome) ... Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend