In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito...

8
Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39 Lezione 15: Connettività in rete 1 Fondamenti di Informatica C Lezione 15: Connettività in rete 11/24/02 16:39 Lezione 15: Connettività in rete 2 In questa lezione vedremo: Î I primi rudimenti dello scambio “programmato” di informazioni attraverso la rete Î Un orizzonte incredibilmente vasto 11/24/02 16:39 Lezione 15: Connettività in rete 3 L’identificazione della macchina Î Due forme possibili: l Dotted quad: 192.167.20.92 l 4 x 8 = 32 bit: 256 4 possibili indirizzi (in teoria) = 4.294.967.296 indirizzi Indirizzi pubblici Indirizzi privati: 10.000.000.001 - 10.255.255.254 172.016.000.001 - 172.031.255.254 192.168.000.001 - 192.168.255.254 Indirizzi speciali: – Broadcast Localhost.localdomain (127.0.0.1) l Domain Name System: zeus.ing.unibs.it 11/24/02 16:39 Lezione 15: Connettività in rete 4 Due parole sulla struttura della rete: Nodo Nodo Nodo Nodo Nodo Nodo Nodo Nodo Gateway Gateway Name server

Transcript of In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito...

Page 1: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 1

Fondamenti di Informatica C

Lezione 15:Connettività in rete

11/24/02 16:39Lezione 15: Connettività in rete 2

In questa lezione vedremo:

ÎI primi rudimenti dello scambio “programmato” diinformazioni attraverso la rete

ÎUn orizzonte incredibilmente vasto

11/24/02 16:39Lezione 15: Connettività in rete 3

L’identificazione della macchina

ÎDue forme possibili:l Dotted quad: 192.167.20.92l 4 x 8 = 32 bit: 2564 possibili indirizzi (in teoria) =

4.294.967.296 indirizzi• Indirizzi pubblici• Indirizzi privati:

– 10.000.000.001 - 10.255.255.254– 172.016.000.001 - 172.031.255.254– 192.168.000.001 - 192.168.255.254

• Indirizzi speciali:– Broadcast– Localhost.localdomain (127.0.0.1)

l Domain Name System: zeus.ing.unibs.it

11/24/02 16:39Lezione 15: Connettività in rete 4

Due parole sulla struttura della rete:

Nodo

Nodo

Nodo

Nodo

Nodo

NodoNodo

Nodo

Gateway

Gateway

Name server

Page 2: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 2

11/24/02 16:39Lezione 15: Connettività in rete 5

L’identificazione della macchina

Il package java.netl InetAddress.getByName(<nome>) ritorna un oggetto di tipo

InetAddressl InetAddress.getByName(null) ritorna un oggetto di tipo

InetAddress che si riferisce al calcolatore su cui gira ilprogramma chiamante (oppure ancheInetAddress.getByName(“localhost”)oInetAddress.getByName(“127.0.0.1”)

11/24/02 16:39Lezione 15: Connettività in rete 6

Le porte

ÎUn indirizzo IP non è sufficiente per identificare unserver, perché sulla stessa macchina possono esistere piùserver

ÎLe porte sono identificate da un numero a 16 bit, cheindica il servizio associato a quella porta

ÎLe porte da 1 a 1024 sono riservate al sistemaÎC’è una convenzione internazionale sull’uso delle porte:

http://www.iana.org/assignments/port-numbers

11/24/02 16:39Lezione 15: Connettività in rete 7

I socket (1)

l The socket is the software abstraction used torepresent the “terminals” of a connectionbetween two machines.

l For a given connection, there’s a socket on eachmachine, and you can imagine a hypothetical“cable” running between the two machines witheach end of the “cable” plugged into a socket. Ofcourse, the physical hardware and cablingbetween machines is completely unknown. Thewhole point of the abstraction is that we don’thave to know more than is necessary.

11/24/02 16:39Lezione 15: Connettività in rete 8

I socket (2)

ÎIn Java, you create a socket to make the connectionto the other machine, then you get anInputStream and OutputStream (or, with theappropriate converters, Reader and Writer)from the socket in order to be able to treat theconnection as an I/O stream object.

ÎThere are two stream-based socket classes: aServerSocket that a server uses to “listen” forincoming connections and a Socket that a clientuses in order to initiate a connection.

Page 3: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 3

11/24/02 16:39Lezione 15: Connettività in rete 9

I socket (3)

ÎOnce a client makes a socket connection, theServerSocket returns (via the accept( ) method)a corresponding Socket through whichcommunications will take place on the server side.

ÎFrom then on, you have a true Socket to Socketconnection and you treat both ends the same waybecause they are the same. At this point, you usethe methods getInputStream( ) andgetOutputStream( ) to produce thecorresponding InputStream andOutputStream objects from each Socket.

11/24/02 16:39Lezione 15: Connettività in rete 10

Riassumendo: Client Vs. Server (1)

InetAddress addr = InetAddress.getByName("frank.ing.unibs.it");InetAddress addr = InetAddress.getByName("frank.ing.unibs.it");

ServerSocket s = new ServerSocket(PORTNUMBER); ServerSocket s = new ServerSocket(PORTNUMBER);

Socket socket = s.accept();Socket socket = s.accept();

Socket socket = new Socket(addr, PORTNUMBER);Socket socket = new Socket(addr, PORTNUMBER);

Attenzione! accept()blocca l’esecuzione del

metodo chiamante!

11/24/02 16:39Lezione 15: Connettività in rete 11

Riassumendo: Client Vs. Server (2)

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

11/24/02 16:39Lezione 15: Connettività in rete 12

Riassumendo: Client Vs. Server (3)

String str = in.readLine();String str = in.readLine();

String str = in.readLine();String str = in.readLine();

out.println(str);out.println(str);

out.println(”bla bla");out.println(”bla bla");

Page 4: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 4

11/24/02 16:39Lezione 15: Connettività in rete 13

Riassumendo: Client Vs. Server (4)

socket.close();socket.close();

s.close();s.close();

socket.close();socket.close();

11/24/02 16:39Lezione 15: Connettività in rete 14

Vediamo un semplice server...

ÎQuesta non è una applet, ma non importa!

11/24/02 16:39Lezione 15: Connettività in rete 15

E un client che lo usi...

ÎQuesto è il client

11/24/02 16:39Lezione 15: Connettività in rete 16

Dove si va da qui?

ÎGet some information from that machine overthere and move it to this machine here, or viceversa. This is accomplished with basic networkprogramming.

ÎConnect to a database, which may live across anetwork. This is accomplished with Java DataBaseConnectivity (JDBC), which is an abstraction awayfrom the messy, platform-specific details of SQL(the structured query language used for mostdatabase transactions).

Page 5: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 5

11/24/02 16:39Lezione 15: Connettività in rete 17

E inoltre...

ÎProvide services via a Web server. This isaccomplished with Java’s servlets and Java ServerPages (JSPs).

ÎExecute methods on Java objects that live onremote machines transparently, as if those objectswere resident on local machines. This isaccomplished with Java’s Remote MethodInvocation (RMI).

ÎUse code written in other languages, running onother architectures. This is accomplished using theCommon Object Request Broker Architecture(CORBA), which is directly supported by Java.

11/24/02 16:39Lezione 15: Connettività in rete 18

Ancora...

ÎIsolate business logic from connectivity issues,especially connections with databases includingtransaction management and security. This isaccomplished using Enterprise JavaBeans (EJBs).EJBs are not actually a distributed architecture,but the resulting applications are usually used in anetworked client-server system.

ÎEasily, dynamically, add and remove devices froma network representing a local system. This isaccomplished with Java’s Jini.

11/24/02 16:39Lezione 15: Connettività in rete 19

High School/Jr.High

10 PRINT "HELLO WORLD"

20 END

11/24/02 16:39Lezione 15: Connettività in rete 20

First year in College

program Hello(input, output) begin writeln('Hello World') end.

Page 6: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 6

11/24/02 16:39Lezione 15: Connettività in rete 21

Senior year in College

(defun hello (print (cons 'Hello (list 'World))))

11/24/02 16:39Lezione 15: Connettività in rete 22

New professional

(include) <stdio.h

void main(void)

{

char *message[] = {"Hello ", "World"}; int i;

for(i = 0; i <2; ++i)

printf("%s", message[i]);

printf("\n");

}

11/24/02 16:39Lezione 15: Connettività in rete 23

Seasoned professional

class string { private: int size; char *ptr; public: string() : size(0), ptr(new char('\0')) {} string(const string &s) : size(s.size) { ptr = new char[size + 1]; strcpy(ptr, s.ptr); } ~string() { delete [] ptr; } friend ostream &operator <<(ostream &, const string

&); string &operator=(const char *); }; ostream &operator<<(ostream &stream, const string &s) { return(stream << s.ptr); } string &string::operator=(const char *chrs) { if (this != &chrs) {

delete [] ptr;

size = strlen(chrs);

ptr = new char[size + 1];

strcpy(ptr, chrs);

}

return(*this);

}

int main()

{ string str;

str = "Hello World";

cout << str << endl;

return(0);

}

11/24/02 16:39Lezione 15: Connettività in rete 24

Master Programmer

[ uuid(2573F8F4-CFEE-101A-9A9F-00AA00342820) ] library LHello { // bring in the master library importlib("actimp.tlb"); importlib("actexp.tlb"); // bring in my interfaces include "pshlo.idl" [ uuid(2573F8F5-CFEE-101A-9A9F-00AA00342820) ] cotype THello { interface IHello; interface IPersistFile; }; }; [ exe,

importheader(windows.h); importheader(ole2.h); importheader(except.hxx); importheader("pshlo.h"); importheader("shlo.hxx"); importheader("mycls.hxx"); // needed typelibs importlib("actimp.tlb"); importlib("actexp.tlb"); importlib("thlo.tlb"); [

uuid(2573F891-CFEE-101A-9A9F-00AA00342820),aggregatable

]

coclass CHello

{

cotype THello;

};

};

include "ipfix.hxx"

extern HANDLE hEvent;

class CHello : public CHelloBase

{

public:

IPFIX(CLSID_CHello);

CHello(IUnknown *pUnk);

~CHello();

HRESULT __stdcall PrintSz(LPWSTR pwszString);

private:

static int cObjRef; };

include windows.h

include ole2.h

include stdio.h

include stdlib.h

include "thlo.h"

include "pshlo.h"

include "shlo.hxx"

include "mycls.hxx"

int CHello::cObjRef = 0;

CHello::CHello(IUnknown *pUnk) : CHelloBase(pUnk)

Page 7: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 7

11/24/02 16:39Lezione 15: Connettività in rete 25

Apprentice Hacker

/usr/local/bin/perl

$msg="Hello, world.\n";

if ($#ARGV = 0) { while(defined($arg=shift(@ARGV))) {

$outfilename = $arg;

open(FILE, "" . $outfilename) || die "Can't write $arg: $!\n";

print (FILE $msg);

close(FILE) || die "Can't close $arg: $!\n";

}

} else { print ($msg);

}

1;

11/24/02 16:39Lezione 15: Connettività in rete 26

Experienced Hacker

include stdio.h

define S "Hello, World\n"

main(){exit(printf(S) == strlen(S) ? 0 : 1);}

11/24/02 16:39Lezione 15: Connettività in rete 27

Seasoned Hacker

% cc -o a.out ~/src/misc/hw/hw.c

% a.out

11/24/02 16:39Lezione 15: Connettività in rete 28

Guru Hacker

% cat

Hello, world. ^D

Page 8: In questa lezione vedremo: Fondamenti di Informatica C ex Uni/Sito webmail/Dida/2002/traspalezionij... · Lezione 15: Connettività in rete 11/24/02 16:39 8 I socket (2) ÎIn Java,

Fondamenti di Informatica CRiccardo Cassinis 11/24/02 16:39

Lezione 15: Connettività in rete 8

11/24/02 16:39Lezione 15: Connettività in rete 29

New Manager

10 PRINT "HELLO WORLD"

20 END

11/24/02 16:39Lezione 15: Connettività in rete 30

Middle Manager

mail -s "Hello, world." bob@b12

Bob, could you please write me a program that prints "Hello,

world."? I need it by tomorrow.

^D

11/24/02 16:39Lezione 15: Connettività in rete 31

Senior Manager

% zmail jim

I need a "Hello, world." program by thisafternoon.

11/24/02 16:39Lezione 15: Connettività in rete 32

Chief Executive

% letter letter: Command not found. % mail To: ^X ^F ^C % help mail help: Command not found. % damn! !: Event unrecognized % logout