24
Network Programming Chapter 11 Lecture 6

Network Programming Chapter 11

  • Upload
    lore

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

Network Programming Chapter 11. Lecture 6. Networks. Client / server. Client. DNS helps in looking up the address. Protocol (http or other). Listens on a “port”. Server. java.net. One of the main strengths of Java is the “embedded” support for network programming - PowerPoint PPT Presentation

Citation preview

Page 1: Network Programming Chapter 11

Network ProgrammingChapter 11

Lecture 6

Page 2: Network Programming Chapter 11

Networks

Page 3: Network Programming Chapter 11

Client / server

Protocol (http or other)

Listens on a “port”

DNS helps in looking up the address

Server

Client

Page 4: Network Programming Chapter 11

java.net

One of the main strengths of Java is the “embedded” support for network programming

Includes core classes with which you can communicate with URLs and create TCP/IP and Datagram sockets.

The classes may be distinguished by whether they support URLs, TCP/IP sockets, or datagram sockets.

Page 5: Network Programming Chapter 11

History: Unix, I/O and IPC

The Unix input/output (I/O) system• Follows the pattern of “open”, “read/write”, “close”

• Inter process communication (IPC) in UNIX was designed to be similar to the file I/O interface. Each process has a set of I/O descriptors that may reflect a file, device, or communication channels (sockets).

• In Unix all processes has stdin, stdout, stderr as default I/O descriptors, that may be redirected to files or devices or sockets

Page 6: Network Programming Chapter 11

Java: Pipes and Sockets Pipe:

• is a mechanism by which two programs running in the same machine can talk to one another. It is just a stream of characters that one end writes and the other reads.

• Contains a buffer so the producer need not pause if the consumer gets behind

• Is in Java used for communication only between threads running in the same JVM

Socket• Is simply a remote pipe. A socket is an endpoint for communication

between two machines.• Is in Java used for interprocess communication• May be used between processes on the same machine

Java supports both pipes and sockets

Page 7: Network Programming Chapter 11

TCP and UDP communications

Stream communication:• The stream communication protocol is known as TCP

(transfer control protocol). Unlike UDP, TCP is a connection-oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both directions (full duplex).

Datagram communication:• The datagram communication protocol, known as UDP

(user datagram protocol), is connectionless, meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket's address.

Page 8: Network Programming Chapter 11

Working with sockets TCP/IP Socket

• Maintains connection for sustained dialog. (like traditional telephone, or a sequential file.)

• More overhead• Reliable – no packet lost without notification

Datagram “Socket”• Does not maintain a connection• Very fast – almost no overhead• Unreliable – can lose packets unknowningly• May hog the network. Does not handle network

congestion.

Page 9: Network Programming Chapter 11

Host and Port

Host• The Ip address of the machine we want to talk

to.

Port number• A number identifying a process on the host.

Page 10: Network Programming Chapter 11

TCP/IP

Lecture 6

Page 11: Network Programming Chapter 11

TCP Client Programs

Usually follow the following steps • Open a socket.

• Open an input stream and/or output stream to the socket.

• Read from and write to the stream.

• Close streams.

• Close sockets.

Remember to handle Exceptions

Page 12: Network Programming Chapter 11

TCP Client socket exampletry {

mysocket = new Socket(“some.host.com", 67);os = new DataOutputStream(mySocket.getOutputStream());is = new DataInputStream(mySocket.getInputStream());

} catch (UnknownHostException e) {

System.err.println("Don't know about host: some.host.com");

} catch (IOException e) {

System.err.println("Couldn't get I/O for the connection to: some.host.com");

} Figure 11-3 p719.

Page 13: Network Programming Chapter 11

TCP server socket

Server-side:• Make a new SocketServer, & call accept on it.

•server = new ServerSocket (port);client_socket = server_socket.accept ();

• This tells it to wait for a client socket to try to connect, then returns a Socket that is connected to the client socket

• You can then ask that connected Socket for an InputStream and an OutputStream, which you can use to send/receive information from the other side of the connection

• Figure 11-2 p715.

Page 14: Network Programming Chapter 11

Datagram (UDP)

Lecture 6

Page 15: Network Programming Chapter 11

Using a Datagram Socket

To receive a packet (server-side)• Call new DatagramSocket( port );

• create a DatagramPacket with an empty byte[] buffer

• call receive on the DatagramSocket with the packet as an argument.

• The received information will be stored in the DatagramPacket you created.

• You can then use DatagramPacket’s access methods to get the data transferred

Page 16: Network Programming Chapter 11

Using a Datagram Socket

To send a packet (client-side)• Call new DatagramSocket()

• create a DatagramPacket complete with data (as a byte[]), length, address, and port.

• Call send on your DatagramSocket with the packet as an argument.

Page 17: Network Programming Chapter 11

URLConnection

Lecture 6

Page 18: Network Programming Chapter 11

Sockets vs. URLConnections

Sockets• Supplies hardware and TCP/IP layers

• You can talk any protocol over this connection

URLConnection subclasses are specific to a particular protocol• URL, HttpURLConnection, for example

Page 19: Network Programming Chapter 11

URLConnections Returned by URL.getConnection(); Provides an InputStream to read the content

of that URL Provides an OutputStream to write to the url

(i.e. for POST request – parameters are in body, not in the query string)

You can query a URLConnection for meta-data, such as date last modified

Good for one exchange – for more, use sockets

Page 20: Network Programming Chapter 11

Remote Objects

Lecture 6

Page 21: Network Programming Chapter 11

Java RMI Java Remote Method Invocation Makes it possible to access remote objects on another

machine, through a network. You don’t actually get a reference to the remote object,

but to a local “stub” object that in turn communicates with the remote object. The stub object is delivered by the ORB (Object Request Broker).

Security is always a big concern in distributed programming, and also in Java RMI.• There is a security framework called “SecurityManager”.

Is not focus in this course, but is briefly mentioned in the end of chapter 11.

Page 22: Network Programming Chapter 11

Common Object Request Broker Architecture (CORBA) Like RMI, but may mix Objects from multiple programming

languages Used to be that RMI and CORBA couldn’t communicate

• CORBA uses Internet Inter-ORB Protocol (IIOP)• RMI uses Java Remote Method Protocol (JRMP)

As of Java 1.3, support for RMI-IIOP interoperability exists – a.k.a. RMI API• Especially useful with Enterprise JavaBeans (EJBs)• CORBA interfaces are defined with Interface Definition

Language (IDL), but you can use an SDK tool called idlj to create the interfaces, stub, and skeleton classes than correspond to the IDL.

Is not focus in this course, but is briefly mentioned in the end of chapter 11.

Page 23: Network Programming Chapter 11

javax.swing

Lecture 6

Page 24: Network Programming Chapter 11

javax.swing.text.html

Contains network protocol (http) oriented classes for creating HTML text editors.