Network Programming

Download Report

Transcript Network Programming

Network Programming
Introduction
Based on Classes in the java.net package
Lecture focuses on:
TCP and UDP
Network programming basics
Identifying a machine
Servers and Clients
Ports and sockets
Data transfer using sockets
1
TCP and UDP
Lots of java programs use Transmission Control
Protocol (TCP)
Connection-based (where java stream sockets
provides continuous data stream), reliable, data
streams will always get there. Also high
overhead. We will focus on TCP/IP sockets.
User Datagram Protocol (UDP) is not
connection-based (connectionless service with
datagram sockets allowing one message, an
unreliable protocol which is much faster, but the
message won’t always get there
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
Servers and Clients
Two machines must connect
Server waits for connection
Client initiates connection (create socket)
Once the connection is made, server &
client look identical
Testing w/o a network
For testing your program, you can do it
w/o network. (server & client on same
machine)
Localhost: the IP address for testing
without a network
Three ways to identify:
Test w/o networking
(Continue)
InetAddress
 addr=InetAddress.getByName(null);
Equivalently:
InetAddress.getByName(“localhost”);
Or using the reserved IP number for the
loopback:
InetAddress.getByName(“127.0.0.1”);
Port number
IP address isn’t 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
Port number (Continue)
When set up client and server, you must
specify IP address and port.
Port number range: 0 to 65,535
1-1024 are reserved, others may be used.
Sockets
Software abstraction
used to represent the
connection between
two machines
Socket is the actual 2way 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();
Data transfer using sockets
Once you have a Socket, you call
getInputStream() and getOutputStream()
to produce the corresponding
InputStream and OutputStream objects
You can convert these to readers and
writers, wrap them in a BufferedReader or
BufferedWriter and PrintWriter
Data transfer using sockets
(continue)
 At the server and client classes
BufferedReader in =
new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true);
Data transfer using sockets
(continue)
From then on, it’s like reading and writing any
other I/O stream!
while (true) {
String str = in.readLine();
if (str.equals("END")) break;
System.out.println("Echoing: " + str);
out.println(str);
}
Example: JabberClient
JabberServer