Transcript Socket
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
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.
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
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
TCP and UDP communications
Stream communication:
•
The stream communication protocol is known as TCP
(transfer control protocol). Unlike UDP, TCP is a connectionoriented 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.
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.
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.
TCP/IP
Lecture 6
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
TCP Client socket example
try
{
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.
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.
Datagram (UDP)
Lecture 6
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
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.
URLConnection
Lecture 6
Sockets vs. URLConnections
Sockets
URLConnection subclasses are specific
to a particular protocol
• Supplies hardware and TCP/IP layers
• You can talk any protocol over this connection
• URL, HttpURLConnection, for example
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 metadata, such as date last modified
Good for one exchange – for more, use
sockets
Remote Objects
Lecture 6
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.
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
•
•
As of Java 1.3, support for RMI-IIOP interoperability exists – a.k.a.
RMI API
•
•
CORBA uses Internet Inter-ORB Protocol (IIOP)
RMI uses Java Remote Method Protocol (JRMP)
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.
javax.swing
Lecture 6
javax.swing.text.html
Contains network protocol (http) oriented
classes for creating HTML text editors.