.


:




:

































 

 

 

 


URL (Uniform Resource Locator). 3

URL. 5

. 5

. 6

.. 8

 


 

Java Network. − TCP/IP.

/ , − , − . − , . - .

TCP/IP IP-. 32- , , , 0 255. IP- , . IP , . DNS (Domain Name Sever) IP-. IP- InetAddress java.net:

// IP-

import java.net.*;

public class MyLocal {

public static void main(String[] args){

InetAddress myIP = null;

try {

myIP = InetAddress.getLocalHost();}

catch (UnknownHostException e) {}

System.out.println(myIP);

}

}

 

getByName IP- :

// IP-

import java.net.*;

public class IPfromDNS {

public static void main(String[] args){

InetAddress bsu = null;

try {

bsu = InetAddress.getByName("www.ya.ru"); }

catch (UnknownHostException e){ }

System.out.println(bsu);

}

}

 

IP- , 217.21.43.2:31. 1 1024 , , . , : 20 FTP-, 21 FTP-, 23 TELNET, 53 DNS, 80 HTTP, 110 POP3, 119 NNTP.


URL (Uniform Resource Locator)

URL: http://ya.ru/

:

http . HTTP : File Transfer Protocol (FTP), Gopher, File, News.

ya.ru/ . /index.html)

. , , ( .. HTTP) :

( )

( , ; HTML, -)

.

Java URL java.net. url :

URL yandex = new URL("http://ya.ru/");

 

URL. , , . URL (relative) .

http://news.yandex.ru/Russia/

URL yandex = new URL("http://ya.ru/");

URL yandexRussia = new URL(yandex, "Russia/")

 

, 4 2 ( ):

new URL("http", "www.gamelan.com", "/pages/Gamelan.net.html");

new URL("http://www.gamelan.com/pages/Gamelan.net.html"); //

 

:

new URL("http", "www.gamelan.com", 80, "pages/Gamelan.network.html");

// : http://www.gamelan.com:80/pages/Gamelan.network.html


(, ), URL ( = %20). java.net.URI, toURL() URL:

// http://foo.com/hello world/

URI uri = new URI("http", "foo.com", "/hello world/", "");

URL url = uri.toURL();

 

URL - MalformedURLException.

URL:

getProtocol

getAuthority

( ya.ru:80)

getHost

getPort

. , -1

getPath

getQuery

query

getFile

getPath getQuery (, )

getRef

URL .

:

import java.net.*;

import java.io.*;

 

public class ParseURL {

public static void main(String[] args) throws Exception {

URL aURL = new URL("http://java.sun.com:80/docs/books/tutorial"

+ "/index.html?name=networking#DOWNLOADING");

System.out.println("protocol = " + aURL.getProtocol());

System.out.println("authority = " + aURL.getAuthority());

System.out.println("host = " + aURL.getHost());

System.out.println("port = " + aURL.getPort());

System.out.println("path = " + aURL.getPath());

System.out.println("query = " + aURL.getQuery());

System.out.println("filename = " + aURL.getFile());

System.out.println("ref = " + aURL.getRef());

}

}

 

:

protocol = http

authority = java.sun.com:80

host = java.sun.com

port = 80

path = /docs/books/tutorial/index.html

query = name=networking

filename = /docs/books/tutorial/index.html?name=networking

ref = DOWNLOADING

 

openStream() URL ( , , -).

URL

URL openConnection URLConnection ( , , java.net.HttpURLConnection). - , ( URLConnection.connect).

:

try {

URL yahoo = new URL("http://www.yahoo.com/");

URLConnection yahooConnection = yahoo.openConnection();

yahooConnection.connect();

 

} catch (MalformedURLException e) { // new URL() failed

...

} catch (IOException e) { // openConnection() failed

...

}

connect, .. java , (, getInputStream, getOutputStream), .

URLConnection, , URLConnection.getInputStream ( IO-)

URL yahoo = new URL("http://www.yahoo.com/");

URLConnection yc = yahoo.openConnection();

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

 

, , ( getOutputStream), output true ( setDoOutput(true)).

URL url = new URL(args[0]);

URLConnection connection = url.openConnection();

connection.setDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

 

− , . IP-. IP- , , . , . , . , . , , . , , . .

Socket. IP- (80 HTTP). , Java DNS- IP-:

try { socket = new Socket("localhost",8080); }

catch (IOException e){ System.out.println(": " + e); }

 

. ServerSocket accept(), :

Socket socket = null;

try { server = new ServerSocket(8080);

socket = server.accept(); }

catch (IOException e) { System.out.println(": " + e); }

 

getInputStram() getOutputStram() PrintStream , .

"!" getOutputStream() Socket. getInputStream(). close() Socket. ! .


import java.io.*;

import java.net.*;

 

public class MyServerSocket{

public static void main(String[] args) throws Exception{

Socket s = null;

try {//

ServerSocket server = new ServerSocket(8030);

s = server.accept();

PrintStream ps = new PrintStream(s.getOutputStream());

ps.println("!");

ps.flush();

s.close(); //

}catch (IOException e){System.out.println(": " + e); }

}

}

 

:

import java.io.*;

import java.net.*;

 

public class MyClientSocket {

public static void main(String[] args) {

Socket socket = null;

try {//

socket = new Socket("www.bsu.by", 8080);

BufferedReader dis = new BufferedReader(new

InputStreamReader(socket.getInputStream()));

String msg = dis.readLine();

System.out.println(msg);

} catch (IOException e) {System.out.println(": " + e); }

}

}

getOutputStream(), getInputStream().

, , getLocalHost() InetAddress IP- , Internet.


1. -. .

(IP nickname) , .

2. -. .

, nickname ( ) IP .

( ) .

3. 2 :

a. , -

b. , ftp- ( ftp- ).

4. SMTP.

5. POP3.



<== | ==>
1. | -
:


: 2016-09-03; !; : 730 |


:

:

, , . , .
==> ...

1323 - | 1165 -


© 2015-2024 lektsii.org - -

: 0.04 .