Chương 2 Networking

33 111 0
Chương 2 Networking

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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? [...]... Overview (cont.) Port (4): Some well-know port 20 : 21 : 22 : 23 : 80 : 139: FTP ( Data) FTP (Control) SSH Telnet HTTP NETBIOS Object Oriented Programming 2 - Chapter 3: Networki 13 2 Implementing Servers  Socket: The Java interface with a TCP connection is a socket  A socket is a Java object which has methods to connect, accept connections and transfer data  Core Networking is in java.net.*  TCP Sockets... Oriented Programming 2 - Chapter 3: Networki 20 3 Sending Email (cont.)  Example: Diagram to send an Email 1 Open socket to your host (using SMTP at port no 25 ) 2 Send following information to the print stream: – HELO sending host – MAIL FROM: sender email address – RCPT TO : recipient email address – DATA : Mail message … – QUIT Object Oriented Programming 2 - Chapter 3: Networki 21 3 Sending Email... listening)  Object Oriented Programming 2 - Chapter 3: Networki 17 2 Implementing Servers (cont.)  Logic diagram to implements a server: 1 It gets a command from the client through an incoming data stream 2 It somehow fetches the information 3 It sends the information to the client through the outging data stream Object Oriented Programming 2 - Chapter 3: Networki 18 2 Implementing Servers (cont.)  Serving... Programming 2 - Chapter 3: Networki 22 4 Advanced Socket programming  Socket Timeout : The read methods will block until data is available  Socket s = new Socket( .);  s.setSoTimeout(10000); // time out after 10 seconds  In JDK1.4, we can uses direct as follow  Socket s=new Socket();  s.connect(new InetSocketAddress(host, port), timeout);  Object Oriented Programming 2 - Chapter 3: Networki 23 4 Advanced... URL(urlString); Object Oriented Programming 2 - Chapter 3: Networki 27 5 URL Connections (cont.)  Retrieving information from a Remote Site URL url=new URL(protocol:resource);  The Java 2 platform supports both HTTP and FTP resources  Using InputStream object to read the content of the resource  InputStream uin=url.openStream;  Object Oriented Programming 2 - Chapter 3: Networki 28 5 URL Connections (cont.)... read the response  Object Oriented Programming 2 - Chapter 3: Networki 25 4 Advanced Socket programming (cont.)  java.net.InetAddress : static InetAddress getByName(String name)  returns the address for “name” Throws UnknownHostException if it’s unknown  This class is the interface from Java to DNS  Object Oriented Programming 2 - Chapter 3: Networki 26 5 URL Connections   The URL and URLConnection... Oriented Programming 2 - Chapter 3: Networki 15 2 Implementing Servers (cont.)    To transmit text only data, we can use InputStream and OutpuStream To transmit binary data, we must turn the streams into DataInputStream and DataOutputStream To transmit serialized objects, we would use ObjectInputStream and ObjectOutputStream instead Object Oriented Programming 2 - Chapter 3: Networki 16 2 Implementing... applications where missing packets can be tolerated, for example, in audio or video streams  Object Oriented Programming 2 - Chapter 3: Networki 31 6 Posting Forms data Object Oriented Programming 2 - Chapter 3: Networki 32 7 Harvesting information from the Web Object Oriented Programming 2 - Chapter 3: Networki 33 ...1 Connecting to a Server (cont.) TCP/IP Overview (cont.) Port (2) :  Recall, with IP network addresses The computer is identified by its 32- bit IP address  The port address is used to identify the specific program or application to send the packet to  Object Oriented Programming 2 - Chapter 3: Networki 11 1 Connecting to a Server (cont.) TCP/IP Overview (cont.) Port (3):... URLConnection conn=url.openConnection(); 2 Set any properties using setXXX method(); 3 Connect to the remote resource by calling the connect method Conn.connect(); 4 After connecting to the server, query the header information 5 Access the resource data.Use getInputStream method to obtain an input stream for reading the info Object Oriented Programming 2 - Chapter 3: Networki 29 5 URL Connections (cont.)  View . 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 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

Ngày đăng: 13/05/2014, 11:01

Mục lục

    Môn Lập trình Java NC

    1. Connecting to a Server

    7. Harvesting information from the Web

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan