Network Programming: Package
Network Programming: Package
Introduction
Based on Classes in the java.net
package
TCP and UDP
Network programming basics
Identifying a machine
Servers and Clients
Ports and sockets
Data transfer using sockets
1
Network Programming
Basics
The classes in java.net: Java
programs can use TCP or UDP to
communicate over the Internet. The
URL, URLConnection (for
Web applications), Socket,
and ServerSocket (client-server
applications) classes all use TCP to
communicate over the network.
The DatagramPacket,
DatagramSocket, and
MulticastSocket classes are for
use with UDP.
Network Programming
Basics
Historically error-prone, difficult,
complex
JAVA has complete network package,
java.net
I/O stream library works quite well for
TCP/IP
Threading is also very useful and
relatively easy here
Identifying a Machine
Uniquely identify a machine from all
the others in the world
IP (Internet Protocol) address that
can exist in two forms:
Identify a Machine
(Continue)
DNS(Domain Name Service) form
java.sun.com
Dotted quad form:
123.255.28.120
static InetAddress.getByName()
produces java object containing
address
Equivalently:
InetAddress.getByName(localhost);
Port number
IP address isnt enough to identify a
unique server
many servers can exist on one
machine
port number: Unique in a Machine
Not a physical location, but a
software abstraction to represent a
service
Sockets
Software
abstraction used to
represent the
connection
between two
machines
Socket is the actual
2-way connector.
Sockets
The following sequence establishes
a socket from the client class
InetAddress addr =
InetAddress.getByName(null);
Socket socket =
new Socket(addr, 8080);
Sockets
At the server class, the following
establishes the server port, and
waits for a client request to establish
a socket
ServerSocket s =
new
ServerSocket(8080);
Socket socket = s.accept();