Вы находитесь на странице: 1из 38

Java

16.
20 2003
:
( Sun ) <vyazovick@itc.mipt.ru>
( Sun ) < gene@itc.mipt.ru>
Copyright 2003 Sun , , .

,
. ,
OSI, TCP/IP ,
. ,
java.net .
,
TCP UDP.

Rendered by www.RenderX.com

ii

16. ......................................................................1
1. OSI........................................................................................................ 2
2. Physical layer (layer 1)...................................................................................................... 4
3. Data layer (layer 2)............................................................................................................8
3.1. LLC sublayer............................................................................................................ 9
3.2. MAC sublayer...........................................................................................................9
4. Network layer (layer 3).................................................................................................... 10
4.1. Class A................................................................................................................... 11
4.2. Class B................................................................................................................... 12
4.3. Class CClass DClass E..........................................................................................12
5. Transport layer (layer 4).................................................................................................. 13
5.1. TCP........................................................................................................................ 14
5.2. UDP........................................................................................................................14
6. Session layer (layer 5).....................................................................................................15
7. Presentation layer (layer 6)............................................................................................. 15
8. Application layer (layer 7)................................................................................................15
9. ........................................................................................16
9.1. IPCONFIG (IFCONFIG)......................................................................................... 17
9.2. ARP........................................................................................................................ 18
9.3. Ping........................................................................................................................ 18
9.4. Traceroute.............................................................................................................. 19
9.5. Route......................................................................................................................21
9.6. Netstat.................................................................................................................... 22
9.7. ................................................................... 23
10. java.net............................................................................................................... 24
11. .................................................................................................................. 32
12. .................................................................................................32

Java

Rendered by www.RenderX.com

. 1 37

16.

.
1. OSI........................................................................................................2
2. Physical layer (layer 1)..................................................................................................... 4
3. Data layer (layer 2)........................................................................................................... 8
3.1. LLC sublayer.................................................................................................................. 9
3.2. MAC sublayer.................................................................................................................9

4. Network layer (layer 3)................................................................................................... 10


4.1. Class A......................................................................................................................... 11
4.2. Class B......................................................................................................................... 12
4.3. Class CClass DClass E................................................................................................12

5. Transport layer (layer 4)................................................................................................. 13


5.1. TCP.............................................................................................................................. 14
5.2. UDP..............................................................................................................................14

6. Session layer (layer 5)....................................................................................................15


7. Presentation layer (layer 6)............................................................................................ 15
8. Application layer (layer 7)............................................................................................... 15
9. ....................................................................................... 16
9.1. IPCONFIG (IFCONFIG)............................................................................................... 17
9.2. ARP.............................................................................................................................. 18
9.3. Ping.............................................................................................................................. 18
9.4. Traceroute.................................................................................................................... 19
9.5. Route............................................................................................................................21
9.6. Netstat.......................................................................................................................... 22
9.7. ......................................................................... 23

10. java.net.............................................................................................................. 24
11. ................................................................................................................. 32
Java

Rendered by www.RenderX.com

. 2 37

OSI

12. ................................................................................................ 32

1. OSI

. 80- .
,
.
(,
).
(International Organization for Standardization, ISO),
,
.
1984 . OSI -
(Open Systems Interconnected). .
:

Layer 7

(data)

Layer 6

(data)

Layer 5

(data)

Layer 4

(segment)

Layer 3

(packet)

Layer 2

(frame)

Layer 1

(bit)

,
. OSI-
, .
,
.
. ,
(HTTP, FTP) .
.
7 (),
. ,
(,
, , ,
). , ,
(layer 6).
, 7 ,
6 ( 6
7).
, ,
,
(6-) .
, .
..,
( .1).
Java

Rendered by www.RenderX.com

. 3 37

. , 3-
(.2).
, 7 .
(encapsulation).

. -
, 0 1. ,
, 010110101.
,
(0 - , 1 - ).
.
/ /.


(.3). - .
Java

Rendered by www.RenderX.com

. 4 37

Physical layer (layer 1)

.
( 1 ),
,
,
. (2-) ,
. ,
,
. -.
- , -.
7
OSI. , ""
.
() .
,
-,
. -
, , .
(message) ISO
(Protocol Data Unit, PDU).

.

2. Physical layer (layer 1)


OSI -
(physical layer) . .
,
(, ..) .
.
( ,
, , ).
.
:


. -
.
,
( ,
).
. ,
Java

Rendered by www.RenderX.com

. 5 37

, .

, .. ,
.
, "". ..
( ""
), .
.
,
, , .
-
, (
75 ,
- 50 ).
( 10
/), . ,
, ,
.

,
.
("twisted pair") -
. ,
,
, .
. : Cat 3 - 1991 ,
16 ,
. , Cat 5
.
100.
10,100,1000 /. Cat5 Cat
3.
- .
Cat 5 ,
, .
,
, , ,
.
() - , - ,
, .
, ,
. .

:


Java

Rendered by www.RenderX.com

. 6 37

Physical layer (layer 1)

"" .
,
. ,
.
, .
.
: ,
.
, ..
.

. , ,
.. (
).
- , ,
. ( , .
Network adapter) ISA PCI,
(,
, ..).
, ,
-
( ).
""(bus):

,
.

Java

Rendered by www.RenderX.com

. 7 37

""(ring):

,
.
""(star):

Java

Rendered by www.RenderX.com

. 8 37

Data layer (layer 2)

" "(extended star):

"" .
,
"".

3. Data layer (layer 2)


- .
,
.
.
.
Data layer :

LLC sublayer

MAC sublayer

LLC - 3 .
MAC - physical layer.
.

Java

Rendered by www.RenderX.com

LLC sublayer.

. 9 37

3.1. LLC sublayer.


.
(3-)
. LLC ,

. , Ethernet, Token Ring, Frame Relay.

3.2. MAC sublayer.


. , data
layer . ..
data layer ,
MAC-.
-
. MAC- 48 6 ( 8 ),
. 3 OUI.
OUI - Organizational Unique Identifier, IEEE (Institute of Electrical and Electronic Engineers,
- ,
) . 3
.
, : 00:00:B4:90:4C:8C, 00-00-B4-904C-8C, 0000.B490.4C8C - .
, , 0000.1c12.3456.
, 12.3456 - .

00001

,
.
data layer Ethernet.
, MAC- MAC- .
, , , .
MAC- MAC- ,
.
, .
, ,
MAC- FF-FF-FF-FF-FF-FF.
(broadcast),
.
Ethernet - CSMA/CD
(carrier sense multiply access/collision detect).
. ,
.
,
. CSMA/CD
- (collision - ,
).
Java

Rendered by www.RenderX.com

. 10 37

Network layer (layer 3)

, .
("bus")
, (, )
. .. , ,
, .
.
Token Ring .
. ,
Ethernet, . .. ,
,
..
""
,
" ". (hub).
" ".
. Ethernet
CSMA/CD. .
. ,
MAC MAC-, , . ,
"" ( ,
).
. 10 /,
2 /.
, ,
(switch). MAC-address
- Port. .
, ,
, .
, , ,
. , ,
,
"".

4. Network layer (layer 3)


OSI.
2? "" .
, MAC-,
broadcast-. ,
, .
.
. , +7-095-101-12-34 ,
/(095), (101-12-34).
. 101 - ,
, 12-34 .
Java

Rendered by www.RenderX.com

Class A

. 11 37


.
.
, - IP, IPX.
IP. IPX
, , .
, 3- , (router).
, (LAN) (WAN).
(
LAN) 3 .
3 , . , . ..
-
. ( hop')
"routing the packet". "next hop",
, - ,
, ..

.
.
- IP. .

. IP-
- ,
( ).
IP-. IP- 32- ,
4 . - network
host. network IP-
, network-ID,
, "" .
IP- -
. (host) . ..
4- , ,
. 3-
host-. IP-.
(class A, class B, class C).
Class A

Class B

Class C

4.1. Class A
,
.
- 0-126(127
Java

Rendered by www.RenderX.com

. 12 37

Network layer (layer 3)

).
.
2^24 ( ) - 16 777 214
.
10.0.0.0-10.255.255.255 .

.

4.2. Class B
B ,
- . B 128 191
( ). B 65534
- 216 ( ).

: 172.16.0.0-172.31.0.0.

4.3. Class CClass DClass E


C .
192 223.
. , C 28( )
254
:
192.168.0.0-192.168.255.255.
multicast-. 224.0.0.0-239.255.255.255.
.
240.0.0.0-247.255.255.255.
. IP-,
, .
- 112.0.0.0, - 112.2.3.4.
.
- - (broadcast). ,
. -
( , ). ,
171.10.0.0 16 , 171.10.255.255. 171.10.0.0
, 171.10.255.255.
.

. .
A,B C .. subnet field
. subnet field , (..
3- , 2
B, 1 C ). ,
Java

Rendered by www.RenderX.com

Class E

. 13 37

. ,
- 2 . ,
, - -.
, - ,
.
- - .. - 16
. -
. .. -
- .
.
, IP- ,
. . IP-
. , network-part subnet-part
1, , host-part 0.
.
: 255.0.0.0,
B: 255.255.0.0, C: 255.255.255.0.
- , 1,
- 0. 255.255.255.192 , -
64.
IP- MAC-.
IP- MAC- ARP-(Address Resolution Protocol,
). ARP-
. IP- .
, MAC
ARP-,
MAC- . RARP (Reverse ARP - ARP)
- MAC- IP-. ,
, , BOOTP, DHCP.
broadcast- - ARP-. ARP-
"IP- , MAC- - ???", RARP- "MAC-
, IP - ???".
IP-, RARP-
IP- ( - DHCP -
- ).

5. Transport layer (layer 4)


TCP/IP 4 OSI. TCP/IP
- TCP UDP. TCP
.
TCP UDP
TCP

UDP

Java

Rendered by www.RenderX.com

. 14 37

Transport layer (layer 4)


TCP

UDP

5.1. TCP
TCP/IP TCP IP. IP - 3
- ,
. TCP - 4 - .
. TCP . -
(sequence number - x). , ,
(sequence number + 1 - x+1)
sequence number(y). sequence number,
1. ( sequence number ,
+1(x+1)), sequence number y+1
. sequence
number, .
(window) TCP.
. , ,
.
"". ,
, , .
- "" 5,

5 , .
TCP , " "
.
TCP-.

5.2. UDP
TCP - UDP . UDP
, user datagram .
, - UDP
. .
,
. , UDP . ,
- TCP,
.
UDP - /,
. UDP
- ,
- ,
Java

Rendered by www.RenderX.com

UDP

. 15 37

- UDP-
.
TCP UDP (port)
.
. ,
. , FTP 21.
: 255 - ,
255-1023 - - ,
1023 - .

6. Session layer (layer 5)


, , session layer. 5 ,
. ,
.
-"",
.

7. Presentation layer (layer 6)


,
. - .
, , .
- ,
. 6- : data formatting(presentation), data encryption, data compression. ,

session layer. . ,
Extended Binary Coded Decimal Interchange Code (EBCDIC)
. American
Standard Code for Information Interchange (ASCII). Presentation layer
.
-
.
.
.
..
.

8. Application layer (layer 7)


, .
OSI.
presentation layer.
- .
Java

Rendered by www.RenderX.com

. 16 37

.
. -
: client-request, server-response, client-request, server-response ..
, - URL,
-. application layer
:

telnet -

dns - domain name system

e-mail -

DNS.
IP-. , , web-.
. IP-, 194.87.0.50
- IP- . IP-
.
IP-. .. 194.87.0.50 www.ru.
- . .
. : .ru, .us, .uk -
, .edu - .edu - educational sites, .com - commercial sites, .gov - government sites, .org
- non-profit sites, .net - network service.
.
.. -.
,
- ( test.ru test.ru,
node1.host1.test.ru -
host1.test.ru,
)

9.
, ,
.
. 2 , Microsoft Windows NT Sun Solaris.
, OSI.
OSI ,
,
.
, - 2 .
- Windows Solaris (
). Windows
OSI.
.

-, Solaris.
. ,
. .. OSI
Java

Rendered by www.RenderX.com

IPCONFIG (IFCONFIG)

. 17 37

,
. Solaris ,
OC .

( ,
),
. - . ,
-. .. Ethernet ATM
. ,
-
. Ethernet FDDI
. - .
.
.

9.1. IPCONFIG (IFCONFIG)


, ,
. 3- () IP
, .. Windows
ipconfig. IP-,
(netmask), (default gateway).
ipconfig all, ,
, , MAC- ..

Solaris IP-
ifconfig. , IP-, , MAC.

Java

Rendered by www.RenderX.com

. 18 37

9.2. ARP
, arp-.
MAC- IP-.
arp. , arp a
MAC-.

MAC- .
, IP-
. ARP-
. ,
MAC-IP .
,
() .
IP-,
broadcast-frame .. ARP-.
ARP- IP- .
ARP-,
IP- MAC-. , ARP-
ARP-. MAC-
,
(
MAC-,
ARP-, ,
).

9.3. Ping
,
, OSI
. ping.
(layer 3), ICMP (Internet Control
Message Protocol) .
: ping 194.87.0.50 ( 194.87.0.50 IP-
Java

Rendered by www.RenderX.com

Traceroute

. 19 37

).
.
ICMP .
ICMP - (echo-request)
- (echo-reply).
- .
- -
, ,
,
.
, echo-request echo-reply ,
.
-, , ,
OSI.

, 32 ,
, TTL (time to live ).
ping. ,
echo request , echo reply ,
.

9.4. Traceroute
traceroute ICMP
. traceroute TTL
1 30. ,
, TTL . TTL
. ,
Java

Rendered by www.RenderX.com

. 20 37

,
, .
,
. , TTL
.
IP TTL 0, 1,
. (
, .
, TTL 0).
, ,
, ICMP " " (time exceeded). traceroute
, IP , ICMP ,
IP .
, Traceroute. IP
TTL, . ,
, ( TTL 1) ICMP
(time exceeded). ,
. Traceroute TTL, 2,
IP . ,
.
, ICMP ,
. UDP , (
30000) port unreachable. ,
traceroute, ,
, TTL (
traceroute TTL 30).
traceroute
traceroute to netserv1.chg.ru (193.233.46.3), 30 hops max, 38 byte packets
1 n3-core.mipt.ru (194.85.80.1) 1.508 ms 0.617 ms 0.798 ms
2 mipt-gw-eth0.mipt.ru (193.125.142.177) 2.362 ms 2.666 ms 1.449 ms
3 msu-mipt-atm0.mipt.ru (212.16.1.1) 5.536 ms 5.993 ms 10.431 ms
4 M9-LYNX.ATM6-0.11.M9-R2.msu.net (193.232.127.229) 12.994 ms 7.830 ms
6.816 ms
5 Moscow-BNS045-ATM4-0-3.free.net (147.45.20.37) 12.228 ms 7.041 ms 8.731
ms
6 ChgNet-gw.free.net (147.45.20.222) 77.103 ms 75.234 ms 92.334 ms
7 netserv1.chg.ru (193.233.46.3) 96.627 ms 94.714 ms 134.676 ms

IP- , TTL
30 38 . TTL,
IP .
TTL 3 .
. 3- 3-
, ,
.
(, traceroute) .
Java

Rendered by www.RenderX.com

Route

. 21 37

, ,
TTL N TTL N+1.
, .
Solaris traceroute. IP-
, . ,
, traceroute (, , ).
Windows tracert. Solaris (tracert
netserv1.chg.ru). tracert traceroute .
traceroute (,
, TTL ).
- . ,
net unreachable:
Moscow-BNS045-ATM4-0-3.free.net (147.45.20.37)
995.257 ms

947.327 ms !N

996.548 ms !N

!N 147.45.20.37 , .
: msu-mipt-atm0.mipt.ru (212.16.1.1) 5.536 ms !H 5.993 ms !H 10.431
ms !H. !P - protocol unreachable.

9.5. Route
route.
:
Windows:
route print

, , ,
, . ,
192.168.1.1. 192.168.1.0
netmask 255.255.255.0 .
.
route ADD 157.0.0.0 MASK 255.0.0.0

157.55.80.1
Java

Rendered by www.RenderX.com

. 22 37

157.0.0.0 , 255.0.0.0 , 157.55.80.1 ,


.
: route DELETE 157.0.0.0
Solaris
netstat r.

route:
route add net 157.6 157.6.1.20, 157.6 , 157.6.1.20
, .
: route del net 157.6

9.6. Netstat
netstat ,
. , -
web-, , netstat, :
TCP

jawa:3687

www.ru:http

ESTABLISHED

TCP ( tcp,udp),
, www.ru:http -
, , ESTABLISHED , tcp .
Windows netstat an (
n DNS- , IP-). ,
,
, .. . TCP 139 Netbios- (
).

Java

Rendered by www.RenderX.com

. 23 37

Solaris
netstat. .

9.7.
1. IP-, .
2. arp-.

Java

Rendered by www.RenderX.com

. 24 37

java.net

3. ping. IP-
; IP-
194.87.0.50. arp- (
).
4. traceroute. DNS-
IP- .
5. netstat. - netstat .

10. java.net
Java java.net,
URL. uniform resource locator (URL),
:
protocol://host:port/resource

protocol - , ; host - IP- DNS , ; port - (


, ); resource , , :
ftp://myserver.ru/pub/docs/Java/JavaCourse.txt

openStream(), InputStream,
. ,
LineNumberReader http://www.ru .
import java.io.*;
import java.net.*;
public class Net {
public static void main(String args[]) {
try {
URL url = new URL("http://www.ru");
LineNumberReader r = new LineNumberReader(new
InputStreamReader(url.openStream()));
String s = r.readLine();
while (s!=null) {
System.out.println(s);
s = r.readLine();
}
System.out.println(r.getLineNumber());
r.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Java

Rendered by www.RenderX.com

. 25 37

}
}

, , ,
. MalformedURLException
, c URL .
URLConnection,
URL.openConnection(). - getInputStream() ( URL.openStream()) getOutputStream(),
,
( web- ).
URLConnection .
, , java.net
HttpURLConnection. , URL URLConnection

.
java.net - TCP
UDP. InetAddress,
-, IP. ,
:
InetAddress getLocalHost()
InetAddress getByName(String name)
InetAddress[] getAllByName(String name)

IP- , Java-.
, .
DNS-, IP, , ,
"67.11.12.101". IP- .
TCP- Socket ServerSocket.
ServerSocket - .
- , .
accept(),
, - .
, Socket
:
try {
ServerSocket ss = new ServerSocket(3456);
Socket client=ss.accept(); // ,

} catch (IOException e) {
e.printStackTrace();
}

Socket.
- (
InetAddress) . , ,
getInputStream() getOutputStream().

Java

Rendered by www.RenderX.com

. 26 37

java.net

try {
Socket s = new Socket("localhost", 3456);
InputStream is = s.getInputStream();
is.read();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

UnknownHostException,
,
, .
InetAddress,
.
Socket -
getInputStream() getOutputStream(). :
import java.io.*;
import java.net.*;
public class Server {
public static void main(String args[]) {
try {
ServerSocket ss = new ServerSocket(3456);
System.out.println("Waiting...");
Socket client=ss.accept();
System.out.println("Connected");
client.getOutputStream().write(10);
client.close();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

10 . ,
close() .
:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
try {
Socket s = new Socket("localhost", 3456);
InputStream is = s.getInputStream();
System.out.println("Read: "+is.read());
Java

Rendered by www.RenderX.com

. 27 37

s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

, , - 10,
.
. -, ServerSocket ,
.
, , ,
?
( ), .
,
. , .
Socket
, , (
).
-, setSoTimeout(int timeout) ServerSocket,
,
. "" ,
. ,
.
,
accept(), .
, .

, ,
accept(). :
import java.io.*;
import java.net.*;
public class NetServer {
public static final int PORT = 2500;
private static final int TIME_SEND_SLEEP = 100;
private static final int COUNT_TO_SEND = 10;
private ServerSocket servSocket;
public static void main(String[] args) {
NetServer server = new NetServer();
server.go();
}
public NetServer() {
Java

Rendered by www.RenderX.com

. 28 37

java.net

try{
servSocket = new ServerSocket(PORT);
}catch(IOException e){
System.err.println("Unable to open Server Socket : " + e.toString());
}
}
public void go() {
// -
class Listener implements Runnable{
Socket socket;
public Listener(Socket aSocket){
socket = aSocket;
}
public void run(){
try{
System.out.println("Listener started");
int count = 0;
OutputStream out = socket.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
PrintWriter pWriter = new PrintWriter(writer);
while(count<COUNT_TO_SEND){
count++;
pWriter.print(((count>1)?",":"")+ "Say" + count);
sleeps(TIME_SEND_SLEEP);
}
pWriter.close();
}catch(IOException e){
System.err.println("Exception : " + e.toString());
}
}
}
// , accept()
System.out.println("Server started");
while(true){
try{
Socket socket = servSocket.accept();
Listener listener = new Listener(socket);
Thread thread = new Thread(listener);
thread.start();
}catch(IOException e){
System.err.println("IOException : " + e.toString());
}
}
}
public void sleeps(long time) {
Java

Rendered by www.RenderX.com

. 29 37

try{
Thread.sleep(time);
}catch(InterruptedException e){
}
}
}

. ,
, .
import java.io.*;
import java.net.*;
public class NetClient implements Runnable{
public static final int PORT = 2500;
public static final String HOST = "localhost";
public static final int CLIENTS_COUNT = 5;
public static final int READ_BUFFER_SIZE = 10;
private String name = null;
public static void main(String[] args) {
String name = "name";
for(int i=1; i<=CLIENTS_COUNT; i++){
NetClient client = new NetClient(name+i);
Thread thread = new Thread(client);
thread.start();
}
}
public NetClient(String name) {
this.name = name;
}
public void run() {
char[] readed = new char[READ_BUFFER_SIZE];
StringBuffer strBuff = new StringBuffer();
try{
Socket socket = new Socket(HOST, PORT);
InputStream in = socket.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
while(true){
int count = reader.read(readed, 0, READ_BUFFER_SIZE);
if(count==-1)break;
strBuff.append(readed, 0, count);
Thread.yield();
}
} catch (UnknownHostException e) {
e.printStackTrace();
Java

Rendered by www.RenderX.com

. 30 37

} catch (IOException e) {
e.printStackTrace();
}
System.out.println("client " + name + "
}

java.net

read : " + strBuff.toString());

UDP. ,
DatagramSocket. :
DatagramSocket()
DatagramSocket(int port)
DatagramSocket(int port, InetAddress laddr)

,
. , ,
, ,
. , ,
.
.
DatagramPacket.
:
DatagramPacket(byte[] buf, int length, InetAddress address, int port)

( length),
. send()
DatagramSocket.
try {
DatagramSocket s = new DatagramSocket();
byte data[]={1, 2, 3};
InetAddress addr = InetAddress.getByName("localhost");
DatagramPacket p = new DatagramPacket(data, 3, addr, 3456);
s.send(p);
System.out.println("Datagram sent");
s.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

DatagramPacket,
,
( ).
, .
receive() DatagramSocket ( ServerSocket.accept()

Java

Rendered by www.RenderX.com

. 31 37

, ).
:
try {
DatagramSocket s = new DatagramSocket(3456);
byte data[]=new byte[3];
DatagramPacket p = new DatagramPacket(data, 3);
System.out.println("Waiting...");
s.receive(p);
System.out.println("Datagram received: "+data[0]+", "+data[1]+", "+data[2]);
s.close();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

, , ,
,
.
,
, received.
import java.io.*;
import java.net.*;
public class DatagramDemoServer {
public static final int PORT = 2000;
private static final int LENGTH_RECEIVE = 1;
private static final byte[] answer = ("received").getBytes();
private DatagramSocket servSocket = null;
private boolean keepRunning = true;
public static void main(String[] args) {
DatagramDemoServer server = new DatagramDemoServer();
server.service();
}
public DatagramDemoServer() {
try{
servSocket = new DatagramSocket(PORT);
}catch(SocketException e){
System.err.println("Unable to open socket : " + e.toString());
}
}
protected void service() {
DatagramPacket datagram;
InetAddress clientAddr;
Java

Rendered by www.RenderX.com

. 32 37

int clientPort;
byte[] data;
while(keepRunning){
try{
data = new byte[LENGTH_RECEIVE];
datagram = new DatagramPacket(data, data.length);
servSocket.receive(datagram);
clientAddr = datagram.getAddress();
clientPort = datagram.getPort();
data = getSendData(datagram.getData());
datagram = new DatagramPacket(data, data.length, clientAddr, clientPort);
servSocket.send(datagram);
}catch(IOException e){
System.err.println("I/O Exception : " + e.toString());
}
}
}
protected byte[] getSendData(byte b[]) {
byte[] result = new byte[b.length+answer.length];
System.arraycopy(b, 0, result, 0, b.length);
System.arraycopy(answer, 0, result, b.length, answer.length);
return result;
}
}

11.

. OSI
. ,
.

12.
16-1. OSI.
a.) OSI :
7 -
6 -
5-
4-
3 -
2
1
Java

Rendered by www.RenderX.com

. 33 37

16-2. ?
.
a.)
(, , ).

( 0 1, , 010110101).
.
:

16-3. MAC- LLC-.


a.) MAC- .
. MACsublayer . LLCsublayer . LLC
, .
16-4. IP-?

64.12.8.130

224.180.224.5

172.16.0.1

194.86.87.256

195.149.20.130
a.) :

A;

D;

B, private;

C.

16-5. ARP- RARP-?


a.) IP-
MAC- . IP- MAC ARP- (Address Resolution Protocol). ARP .
IP-
Java

Rendered by www.RenderX.com

. 34 37

. MAC- ,
arp-, ,

.
RARP MAC-
IP-. , ,
, BOOTP, DHCP.
broadcast arp-. ARP-
IP- , MAC- - ???, RARP MAC- , IP - ???. , DHCP
IP-
. broadcast
MAC-. MAC IP, rarp-,
IP-
( ).
16-6. , ?
a.) .
, IP- (network
part), , host part (,
). , IP . , network-part
1, , host-part, 0.
.
:
255.0.0.0, B: 255.255.0.0, C: 255.255.255.0.
, ( ) ,
, 1, 0.
255.255.255.192 , -
64.
16-7. TCP UDP?
a.) TCP-
. UDP
. TCP-
,
, HTML-, FTP.
UDP ,
:
.
16-8. , ,
.
a.) ,
.
OSI.
Java

Rendered by www.RenderX.com

. 35 37

,
. ,
, Extended Binary Coded Decimal Interchange Code (EBCDIC) American Standard Code for Information Interchange (ASCII). ,
, , presentation layer
.
: ,
,
.
,
, .
,
,

.
16-9. Ping traceroute .
a.) ping traceroute
. ICMP. Ping
.
ping echo- IP- . echo-
, echo-.
echo- ,
. .. echo- ,
,
, .
Traceroute ICMP
.
, .
16-10. ?
?
a.) , ,
,
.
.
, ,
,
.
, ,
IP- ,
.
,
default gateway .

route.
Java

Rendered by www.RenderX.com

. 36 37

16-11. TCP
Java-? ?
a.) -, ServerSocket , accept().

.
Socket IP .
accept() Socket
.
16-12. UDP ?
a.) -,
DatagramSocket. DatagramPacket ,
, IP- .
DatgramSocket ,
. DatagramPacket , DatagramSocket.receive(). DatagramSocket.send()
16-13. URL ?
a.)
b.)
c.) , (upload) .

Java

Rendered by www.RenderX.com

Оценить