Tải bản đầy đủ (.ppt) (33 trang)

Chương 2 Networking

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (458.36 KB, 33 trang )

Môn Lập trình Java NC
Môn Lập trình Java NC
Chương 2: Networking
Chương 2: Networking
2Object Oriented Programming 2 - Chapter 3: Networki
ng
Content
Content
1. Connecting to a Server
1. Connecting to a Server
2. Implementing Servers
2. Implementing Servers
3. Sending Email
3. Sending Email
4. Advanced Socket programming
4. Advanced Socket programming
5. URL Connections
5. URL Connections
6. Posting Forms data
6. Posting Forms data
7. Harvesting information from the Web
7. Harvesting information from the Web
3Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server
1. Connecting to a Server

Using Telnet (debugging tool for network
Using Telnet (debugging tool for network
programming) program.
programming) program.


Ex : telnet time-A.timefreq.bldrdoc.gov 13
Ex : telnet time-A.timefreq.bldrdoc.gov 13
A client connecting to a server port
4Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview
TCP/IP Overview
IP: Internet Protocol:
IP: Internet Protocol:

IP is a protocol for connecting networks
IP is a protocol for connecting networks

IP is a
IP is a
packet-based
packet-based
protocol
protocol

Packets can be dropped, garbled or re-
Packets can be dropped, garbled or re-
ordered and duplicated:
ordered and duplicated:

IP promises nothing but
IP promises nothing but
best-effort

best-effort
delivery
delivery

IP usually runs over ethernet, but not
IP usually runs over ethernet, but not
always
always
5Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
Addressing:
Addressing:

Every packet has a source and a destination
Every packet has a source and a destination

A Host is identified by an IP address.
A Host is identified by an IP address.

IP addresses are 4 bytes
IP addresses are 4 bytes

Example: 129.123.12.43
Example: 129.123.12.43

An IP can belong to only one host in the

An IP can belong to only one host in the
Internet
Internet

Routers use the IP addresses to decide where to
Routers use the IP addresses to decide where to
direct packets
direct packets
6Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
TCP:
TCP:

TCP is a
TCP is a
stream-based
stream-based
protocol over IP
protocol over IP

TCP promises a
TCP promises a
reliable transport
reliable transport

TCP takes care of packet ordering, packet

TCP takes care of packet ordering, packet
loss and data corruption.
loss and data corruption.

A TCP stream is a
A TCP stream is a
connection
connection
.
.
7Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
TCP over IP:
TCP over IP:
IP Header
Src Dst
IP Data
TCP Header
Type:
TCP
TCP Data
Src
Port
Dst
Port
Seq

Num
Application Data
8Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
TCP Connections:
TCP Connections:

A TCP connection is between a
A TCP connection is between a
Client
Client
Host
Host
and a
and a
Server
Server
Host
Host

The
The
Server
Server
is passive; It just waits
is passive; It just waits


The
The
Client
Client
is active
is active

More than one connection is allowed
More than one connection is allowed
between to hosts
between to hosts

TCP connections are 4-tuples
TCP connections are 4-tuples

{<src-host,src-port>,<dst-host,dst-port>}
{<src-host,src-port>,<dst-host,dst-port>}
9Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
Internet Reference Model:
Internet Reference Model:
Application Layer
(HTTP, FTP, DNS, etc.)
Transport Layer
(TCP, UDP)

Network Layer
(IP)
Link and Physical Layer
10Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
Port (1):
Port (1):

A computer usually has one physical
A computer usually has one physical
connection to the network. So all the data
connection to the network. So all the data
from the the network comes through this
from the the network comes through this
connection.
connection.

If all the data comes through the same
If all the data comes through the same
interface how does the computer redirect to
interface how does the computer redirect to
specific applications?
specific applications?
11Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)

1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
Port (2):
Port (2):

Recall, with IP network addresses
Recall, with IP network addresses

The computer is identified by its 32-bit IP address
The computer is identified by its 32-bit IP address

The port address is used to identify the specific
The port address is used to identify the specific
program or application to send the packet to.
program or application to send the packet to.
12Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
Port (3):
Port (3):

Port numbers range from 0 to 65,535 because
Port numbers range from 0 to 65,535 because
ports are represented by 16-bit numbers. The port
ports are represented by 16-bit numbers. The port
numbers ranging from 0 - 1023 are restricted.

numbers ranging from 0 - 1023 are restricted.
They are reserved for use by well-known services
They are reserved for use by well-known services
such as HTTP and FTP and other system services.
such as HTTP and FTP and other system services.

Remember that certain ports are dedicated to well-
Remember that certain ports are dedicated to well-
known services and you cannot use them. If you
known services and you cannot use them. If you
specify a port that is in use, the creation of the
specify a port that is in use, the creation of the
DatagramSocket
DatagramSocket
will fail
will fail
13Object Oriented Programming 2 - Chapter 3: Networki
ng
1. Connecting to a Server (cont.)
1. Connecting to a Server (cont.)
TCP/IP Overview (cont.)
TCP/IP Overview (cont.)
Port (4):
Port (4):
Some well-know port
Some well-know port
20
20
:
:

FTP ( Data)
FTP ( Data)
21
21
:
:
FTP (Control)
FTP (Control)
22 : SSH
22 : SSH
23
23
:
:
Telnet
Telnet
80
80
:
:
HTTP
HTTP
139:
139:
NETBIOS
NETBIOS
14Object Oriented Programming 2 - Chapter 3: Networki
ng
2. Implementing Servers
2. Implementing Servers


Socket:
Socket:

The Java interface with a TCP connection is a
The Java interface with a TCP connection is a
socket
socket
.
.

A socket is a Java object which has methods to
A socket is a Java object which has methods to
connect, accept connections and transfer data.
connect, accept connections and transfer data.

Core Networking is in java.net.*
Core Networking is in java.net.*

TCP Sockets come in two flavours:
TCP Sockets come in two flavours:
ServerSocket
ServerSocket


and
and
Socket
Socket
.

.
15Object Oriented Programming 2 - Chapter 3: Networki
ng
2. Implementing Servers (cont.)
2. Implementing Servers (cont.)

java.net.Socket
java.net.Socket

Socket(InetAddress address, int port): Creates a
Socket(InetAddress address, int port): Creates a
stream socket and connects it to the specified port
stream socket and connects it to the specified port
number at the specified IP address.
number at the specified IP address.

InputStream getInputStream(): Returns an input
InputStream getInputStream(): Returns an input
stream for this socket.
stream for this socket.

OutputStream getOutputStream(): Returns an
OutputStream getOutputStream(): Returns an
output stream for this socket.
output stream for this socket.

void close(): closes this socket (cuts the connection)
void close(): closes this socket (cuts the connection)
16Object Oriented Programming 2 - Chapter 3: Networki
ng

2. Implementing Servers (cont.)
2. Implementing Servers (cont.)

To transmit
To transmit
text only data
text only data
, we can use
, we can use
InputStream
InputStream
and
and
OutpuStream.
OutpuStream.

To transmit
To transmit
binary data
binary data
,
,
we must turn the
we must turn the
streams into
streams into
DataInputStream
DataInputStream
and
and

DataOutputStream.
DataOutputStream.

To transmit
To transmit
serialized objects
serialized objects
, we would
, we would
use
use
ObjectInputStream
ObjectInputStream
and
and
ObjectOutputStream
ObjectOutputStream
instead.
instead.
17Object Oriented Programming 2 - Chapter 3: Networki
ng
2. Implementing Servers (cont.)
2. Implementing Servers (cont.)

java.net.ServerSocket :
java.net.ServerSocket :

ServerSocket(int port)
ServerSocket(int port)


Creates a server socket on a specified port.
Creates a server socket on a specified port.

Socket accept()
Socket accept()

Listens for a connection to be made to this socket
Listens for a connection to be made to this socket
and accepts it.
and accepts it.

void close()
void close()

closes this socket (stops listening)
closes this socket (stops listening)
18Object Oriented Programming 2 - Chapter 3: Networki
ng
2. Implementing Servers (cont.)
2. Implementing Servers (cont.)

Logic diagram to implements a server:
Logic diagram to implements a server:
1.
1.
It gets a command from the client through an
It gets a command from the client through an
incoming data stream.
incoming data stream.
2.

2.
It somehow fetches the information.
It somehow fetches the information.
3.
3.
It sends the information to the client through
It sends the information to the client through
the outging data stream.
the outging data stream.
19Object Oriented Programming 2 - Chapter 3: Networki
ng
2. Implementing Servers (cont.)
2. Implementing Servers (cont.)

Serving Multiple clients :
Serving Multiple clients :

One ServerSocket can accept multiple clients
One ServerSocket can accept multiple clients
simultaneously.
simultaneously.

Use multithreading to handle them.
Use multithreading to handle them.

One thread waits for “accept()”.
One thread waits for “accept()”.

Open a new thread for each connection.
Open a new thread for each connection.


Example :
Example :
ThreadedEchoServer.java
ThreadedEchoServer.java


EchoClient.java
EchoClient.java
20Object Oriented Programming 2 - Chapter 3: Networki
ng
3. Sending Email
3. Sending Email

Sends e-mail to a remote site using socket
Sends e-mail to a remote site using socket
programming. To send e-mail:
programming. To send e-mail:

Make a socket connection to port 25. SMTP is the
Make a socket connection to port 25. SMTP is the
Simple Mail Transport Protocol that describes the
Simple Mail Transport Protocol that describes the
format for e-mail messages.
format for e-mail messages.

Connect to any server that runs an SMTP service.
Connect to any server that runs an SMTP service.

The server must be willing to accept the request.

The server must be willing to accept the request.

Send a mail header (in the SMTP format, which is
Send a mail header (in the SMTP format, which is
easy to generate), followed by the mail message.
easy to generate), followed by the mail message.
21Object Oriented Programming 2 - Chapter 3: Networki
ng
3. Sending Email (cont.)
3. Sending Email (cont.)

Example:
Example:
Diagram to send an Email
Diagram to send an Email
1.
1.
Open socket to your host (using SMTP at port no.
Open socket to your host (using SMTP at port no.
25).
25).
2.
2.
Send following information to the print stream:
Send following information to the print stream:

HELO
HELO
sending host
sending host


MAIL FROM:
MAIL FROM:
sender email address.
sender email address.

RCPT TO :
RCPT TO :
recipient email address.
recipient email address.

DATA :
DATA :
Mail message
Mail message



QUIT
QUIT
22Object Oriented Programming 2 - Chapter 3: Networki
ng
3. Sending Email (cont.)
3. Sending Email (cont.)
Example: MailTest.java
23Object Oriented Programming 2 - Chapter 3: Networki
ng
4. Advanced Socket programming
4. Advanced Socket programming


Socket Timeout :
Socket Timeout :

The read methods will block until data is available.
The read methods will block until data is available.

Socket s = new Socket(. . .);
Socket s = new Socket(. . .);

s.setSoTimeout(10000); // time out after 10
s.setSoTimeout(10000); // time out after 10
seconds
seconds

In JDK1.4, we can uses direct as follow
In JDK1.4, we can uses direct as follow

Socket s=new Socket();
Socket s=new Socket();

s.connect(new InetSocketAddress(host, port),
s.connect(new InetSocketAddress(host, port),
timeout);
timeout);
24Object Oriented Programming 2 - Chapter 3: Networki
ng
4. Advanced Socket programming
4. Advanced Socket programming
(cont.)
(cont.)


Socket Timeout :
Socket Timeout :

The read methods will block until data is available.
The read methods will block until data is available.

Socket s = new Socket(. . .);
Socket s = new Socket(. . .);

s.setSoTimeout(10000); // time out after 10
s.setSoTimeout(10000); // time out after 10
seconds
seconds

In JDK1.4, we can uses direct as follow
In JDK1.4, we can uses direct as follow

Socket s=new Socket();
Socket s=new Socket();

s.connect(new InetSocketAddress(host, port),
s.connect(new InetSocketAddress(host, port),
timeout);
timeout);
25Object Oriented Programming 2 - Chapter 3: Networki
ng
4. Advanced Socket programming
4. Advanced Socket programming
(cont.)

(cont.)

Half-close :
Half-close :

When a client program sends a request to the
When a client program sends a request to the
server, the server needs to be able to determine
server, the server needs to be able to determine
when the end of the request occurs.
when the end of the request occurs.

You can close the output stream of a socket,
You can close the output stream of a socket,
thereby indicating to the server the end of the
thereby indicating to the server the end of the
request data, but keep the input stream open so
request data, but keep the input stream open so
that you can read the response.
that you can read the response.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×