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

lecture 26
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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?

slide-2
SLIDE 2

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 2

Outline

 Internet (IP) sockets  Concurrent servers  Introduction to X windows  Window managers  Remote clients

slide-3
SLIDE 3

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-4
SLIDE 4

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 4

Internet (IP) Sockets

Server 206.62.226.35 server (*:21, *.*) server (child2) server (child1) 206.62.226.35:21 198.69.10.2:1500 server (child3) client1 198.69.10.2:1500 206.62.226.35:21 client2 Client 198.69.10.2 client1 Client 102.43.83.204 198.69.10.2:1501 206.62.226.35:21 102.43.83.204:1501 206.62.226.35:21 206.62.226.35:21 198.69.10.2:1501 206.62.226.35:21 102.43.83.204:1501

slide-5
SLIDE 5

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-6
SLIDE 6

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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);

slide-7
SLIDE 7

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-8
SLIDE 8

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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( ).

slide-9
SLIDE 9

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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).

slide-10
SLIDE 10

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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);

slide-11
SLIDE 11

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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( ).

slide-12
SLIDE 12

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-13
SLIDE 13

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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

  • f 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.

slide-14
SLIDE 14

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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 }

slide-15
SLIDE 15

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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

  • n the socket reaches zero.)
slide-16
SLIDE 16

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-17
SLIDE 17

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-18
SLIDE 18

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-19
SLIDE 19

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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

  • utput from clients and sends back user input

(from keyboard, mouse or touchscreen) to clients.

slide-20
SLIDE 20

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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.

slide-21
SLIDE 21

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 21

Introduction to X Windows

Window, Session Managers Low level API (Xlib) X Network Protocol Base System (Server) High Level X Toolkit Application

slide-22
SLIDE 22

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 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) ...

slide-23
SLIDE 23

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 23

Window Managers

 A window manager is not required, but when

  • ne is running, some server-client interaction is

redirected through the window manager. In particular, a request for a new window is redirected to the window manager and the manager decides on the initial window position.

 A window manager will usually re-parent (i.e.,

adopt) a window by adding a frame and title bar.

slide-24
SLIDE 24

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 24

Remote Clients

 By default only clients running on the same

machine as the server are allowed access to the server. Access to remote clients must be granted specifically.

 X supports several access methods (see the

Xsecurity man page). Only host-based access control is discussed here.

 On the local (X server) machine:

$ xhost +oreo.ibm.com # allow oreo $ xhost + # allow all

slide-25
SLIDE 25

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 25

Remote Clients

 On the remote machine we must tell the clients

where the server is located. This is done either with the “-display” argument to the client or by setting the DISPLAY environment variable:

$ xclock -display pc10.ibm.com:0.0

  • r

$ export DISPLAY pc10.ibm.com:0.0 $ xterm &

slide-26
SLIDE 26

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 26

Remote Clients

 The display name of the local X server is of the

form:

hostname:displaynumber.screennumber

 The hostname may be either a name or an IP

  • address. The displaynumber and

screennumber are usually 0 (unless you are running multiple X servers or screens).

slide-27
SLIDE 27

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 27

Remote Clients

 If ssh is used with the “-X” or “-Y” arguments to

connect to the remote machine, you do not need to use “xhost” on the server machine or set the DISPLAY variable on the remote

  • machine. ssh automatically sets the DISPLAY

variable to a dummy server on the remote machine and then tunnels the X socket traffic back to the local machine.

slide-28
SLIDE 28

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 28

Remote Clients

 The geometry option allows you to specify not

  • nly the size but also the position. The format

is “XSIZExYSIZE+XOFF+YOFF”:

$ xclock -geometry 100x100+50+50 $ xeyes -geometry 50x50-0-100

 Here are a few other standard options:

$ xterm -fg cyan -bg black $ xterm -title "ssh to csserver" $ xterm -iconic

slide-29
SLIDE 29

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 29

KDE and GNOME

 KDE and GNOME are complete desktop

  • environments. They include standard sets of

applications for home, office, education, development and administration.

 Each provides configurable “look and feel”

(themes), unified access to documentation, drag and drop protocols, IPC protocols (CORBA -GNOME and DCOP-KDE).

 You can run KDE applications under GNOME

and GNOME applications under KDE.

slide-30
SLIDE 30

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 30

Toolkits

 Programmers typically do not program directly

in Xlib. Instead a higher order toolkit is used. There are many toolkits available. Some popular ones are:

Toolkit Language Shipped With Tk Tcl Tcl, Python GTK+ C Gnome Qt C++ KDE FLTK C++ — wxWidgets C++ —

slide-31
SLIDE 31

Tuesday, November 30 CS 375 UNIX System Programming - Lecture 26 31

Toolkits

 Bindings for C, C++, Perl, Tcl, and Python are

available for all of the toolkits. They are also available for Windows and the Mac.

 Tk is the oldest. It was originally developed for

  • Tcl. It is now included with Python.

 GTK+ was developed to support the GIMP, but

it is now the standard GNOME toolkit.

 Qt is the toolkit used by KDE.  FLTK and wxWidgets were designed to be

cross-platform toolkits.