JAVA 22 Networking
JAVA 22 Networking
JAVA 22 Networking
Introduction
• Java is practically a synonym for Internet programming.
• There are a number of reasons for this, not the least of
which is its ability to generate secure, cross platform,
portable code.
• However, one of the most important reasons that Java is
the premier language for network programming are the
classes defined in the java.net package.
• They provide an easy-to-use means by which
programmers of all skill levels can access network
resources.
• This chapter explores the java.net package.
NETWORKING BASICS
A Network Is...
• node
– any device on the network
• host
– a computer on the network
• address
– computer-readable name for host
• host name
– human-readable name for host
A Network Does...
• datagram (or “packet”)
– little bundle of information
– sent from one node to another
• protocol
– roles, vocabulary, rules for communication
• IP
– the Internet Protocol
The Three ‘I’s
• internet
– any IP-based network
• Internet
– the big, famous, world-wide IP network
• intranet
– a corporate LAN-based IP network
• extranet
– accessing corporate data across the Internet
Socket
• At the core of Java’s networking support is the concept of a socket.
• A socket identifies an endpoint in a network.
• Sockets are at the foundation of modern networking because a socket
allows a single computer to serve many different clients at once, as
well as to serve many different types of information.
• This is accomplished through the use of a port, which is a numbered
socket on a particular machine.
• A server process is said to "listen" to a port until a client connects to it.
• A server is allowed to accept multiple clients connected to the same
port number, although each session is unique.
• To manage multiple client connections, a server process must be
multithreaded or have some other means of multiplexing the
simultaneous I/O.
Sockets
• A network socket is a lot like an electrical socket.
• Socket: a two-way connection
• Internet Protocol (IP) is a low-level routing protocol that
breaks data into small packets and sends them to an address
across a network, which does not guarantee to deliver said
packets to the destination.
• Transmission Control Protocol (TCP) is a higher-level protocol
that manages to robustly string together these packets,
sorting and retransmitting them as necessary to reliably
transmit your data.
• A third protocol, User Datagram Protocol (UDP), sits next to
TCP and can be used directly to support fast, connectionless,
unreliable transport of packets.
Ports
• Once a connection has been established, a higher-level
protocol ensues, which is dependent on which port you are
using.
• TCP/IP reserves the lower 1,024 ports for specific protocols.
• Many of these will seem familiar to you if you have spent
any time surfing the Internet.
• Port number 21 is for FTP; 23 is for Telnet; 25 is for e-mail;
43 is for whois; 80 is for HTTP; 119 is for netnews—and the
list goes on.
• It is up to each protocol to determine how a client should
interact with the port.
Ports
• Port: a meeting place on a host
1. one service per port
2. 1-1023 = well-known services
3. 1024+ = experimental services, temporary
Well-Known Ports
• 20,21: FTP
• 23: telnet
• 25: SMTP
• 43: whois
• 80: HTTP
• 119: NNTP
• 1099: RMI
Sockets and Ports
Socket Socket
Server
Addresses
• Every computer on the Internet has an address.
• An Internet address is a number that uniquely identifies each
computer on the Net.
• There are 32 bits in an IP address, and often refer to them as a
sequence of four numbers between 0 and 255 separated by
dots
• The first few bits define which class of network, lettered A, B,
• C, D, or E, the address represents.
• Most Internet users are on a class C network, since there are
over two million networks in class C.
IP Addresses
• The first byte of a class C network is between
192 and 224, with the last byte actually
identifying an individual computer among the
256 allowed on a single class C network.
• IP Address: identifies a host
• DNS: converts host names / domain names
into IP addresses.
Protocols for socket communication
• Socket communication takes place via a protocol.
1. Internet Protocol (IP) is a low-level routing protocol that
breaks data into small packets and sends them to an address
across a network, which does not guarantee to deliver said
packets to the destination.
2. Transmission Control Protocol (TCP) is a higher-level protocol
that manages to robustly string together these packets,
sorting and retransmitting them as necessary to reliably
transmit data.
3. A third protocol, User Datagram Protocol (UDP), sits next to
TCP and can be used directly to support fast, connectionless,
unreliable transport of packets.
HTTP Protocol
• For example, HTTP is the protocol that web browsers
and servers use to transfer hypertext pages and images.
• It is a quite simple protocol for a basic page-browsing
web server.
• Here’s how it works.
– When a client requests a file from an HTTP server, an action
known as a hit, it simply sends the name of the file in a
special format to a predefined port and reads back the
contents of the file.
– The server also responds with a status code to tell the client
whether or not the request can be fulfilled and why.
TCP/IP Protocol Suit
Physical Network
TCP/UDP/IP
IP
raw packets
the “Internet Layer”
TCP
data stream
reliable, ordered
the “Transport Layer”
UDP
user datagrams (packets)
unreliable, unordered
the “Transport Layer”
Basics of network programming: Overview
java.net
TCP/IP
Network OS
JAVA AND NETWORKING
The Socket Class
pw.close();
// close everything
br.close();
sock.close();
} catch (Throwable e) {
System.out.println("Error " + e.getMessage());
e.printStackTrace();
}
}
}
Server program
/** Server program using TCP */
public class Tserver {
class WriteServer {
public static int serverPort = 998;
public static int clientPort = 999;
public static int buffer_size = 1024;
public static DatagramSocket ds;
public static byte buffer[] = new byte[buffer_size];