Written by Amir Kirsh Edited by Liron Blecher Agenda Downloading a web page TCP Client TCP Server Whats beyond 3 Downloading a web page public static void main String args String line ID: 560518
Download Presentation The PPT/PDF document "Java Networking" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Java Networking
Written by Amir Kirsh, Edited by Liron BlecherSlide2
Agenda
Downloading a web page
TCP ClientTCP ServerWhat’s beyondSlide3
3
Downloading a web page
public static void main (String args[]) {
String line;
try {
URL u = new URL(args[0]);
DataInputStream htmlPage =
new DataInputStream(u.openStream());
while ((line = htmlPage.readLine()) != null) { System.out.println(line);
}
}
catch (Exception e) {
System.err.println(e); // naive treatment
}
}Slide4
4
Dealing with URL encoding
public static void main (String args[]) {
String line;
try {
URL u = new URL(
URLEncoder.encode(
args[0], Charset.forName("UTF-8") ) );
DataInputStream htmlPage =
new DataInputStream(u.openStream());
while ((line = htmlPage.readLine()) != null) { System.out.println(line);
}
}
catch (Exception e) {
System.err.println(e); // naive treatment
}
}Slide5
Agenda
Downloading a web page
TCP ClientTCP ServerWhat’s beyondSlide6
6
Simple TCP Echo Client
String
line = "";
try
(Socket
socket
= new Socket("
localhost
", 7000)) {
BufferedReader
inputStream
= new
BufferedReader
(new
InputStreamReader
(
socket.getInputStream
()));
PrintStream
outputStream
= new
PrintStream
(
socket.getOutputStream
());
BufferedReader
userInput
= new
BufferedReader
(new
InputStreamReader
(System.in
));
while
(!
line.equals
("!")) {
line
=
userInput.readLine
();
outputStream.println
(line
);
System.out.println
(
inputStream.readLine
());
}
}Slide7
7
Simple TCP Echo Client – cont’
public static void main(String[]
args
) {
try
{
…
}
catch (Exception e) {
System.err.println
(e);
}
}Slide8
Agenda
Downloading a web page
TCP ClientTCP ServerWhat’s beyondSlide9
9
Simple TCP Echo Server
String line = "";
try
(
ServerSocket
server = new
ServerSocket
(7000)) {
Socket
socket
=
server.accept
(); // blocking
BufferedReader
inputStream
= new
BufferedReader
(new
InputStreamReader
(
socket.getInputStream
()));
PrintStream
outputStream
=
new
PrintStream
(
socket.getOutputStream
());
while
(!
line.equals
("!")) {
line
=
inputStream.readLine
();
outputStream.println
(line
);
System.out.println
(line);
}
}Slide10
10
Simple TCP Echo Server – cont’
public static void main(String[]
args
) {
try
{
…
}
catch (Exception e) {
System.err.println
(e);
}
}Slide11
demo
examples.streams.simple
11Slide12
Agenda
Downloading a web page
TCP ClientTCP ServerWhat’s beyondSlide13
13
What’s beyond
UDP
java.net.DatagramSocket
Multicast
java.net.MulticastSocket
Selector and Channels (and nio in general)
java.nio.channels
Servlets (and JSP)
Web Services
RMI; EJBSlide14
demo
examples.streams.advanced
14