Java Networking: by The Java Tutorials Al/networking/overview/index - HTML
Java Networking: by The Java Tutorials Al/networking/overview/index - HTML
URI uri =
new URI("http", "example.com", "/hello world/", "");
URL url = uri.toURL();
!!! URLs are "write-once" objects. Once you've created a URL object, you cannot change
any of its attributes (protocol, host name, filename, or port number).
URL objects. Methods
import java.net.*;
import java.io.*;
public class ParseURL {
public static void main(String[] args) throws Exception {
BufferedReader in =
new BufferedReader(new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Connecting to a URL
try {
URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
catch (IOException e) {
// openConnection() failed
// ...
}
Reading from a URLConnection
import java.net.*;
import java.io.*;
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Writing to a URLConnection
import java.io.*;
import java.net.*;
public class Reverse {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java Reverse “+
"http://<location of your servlet/script>"+ " string_to_reverse");
System.exit(1);
}
String stringToReverse = URLEncoder.encode(args[1], "UTF-8");
BufferedReader in =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}
}
Socket
• A socket is one end-point of a two-way
communication link between two programs
running on the network.
• Socket classes are used to represent the
connection between a client program and a server
program.
• The java.net package provides two classes
-Socket and ServerSocket - that
implement the client side of the connection and
the server side of the connection, respectively.
The client and server can now communicate by writing to or reading from their sockets.
Class Socket
• implements one side of a two-way connection
between your Java program and another
program on the network
• sits on top of a platform-dependent
implementation, hiding the details of any
particular system from your Java program
• instead of relying on native code, your Java
programs can communicate over the network in
a platform-independent fashion
ServerSocket class
• implements a socket that servers can use to
listen for and accept connections to clients
Reading from and Writing to a Socket
The example program implements a client, EchoClient, that connects to an echo server. The
echo server receives data from its client and echoes it back. The example EchoServer
implements an echo server. (Alternatively, the client can connect to any host that supports the
Echo Protocol.)
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try ( // try-with-resources statement
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader ( new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader ( new InputStreamReader(System.in))
)
1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the server's
protocol.
4. Close the streams. java EchoServer 7
5. Close the socket. java EchoClient echoserver.example.com 7
Example. EchoClient
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println("Usage: java EchoClient <host name> <port number>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try (
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in))
) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
Writing the Server Side of a Socket
The example program implements a client, EchoClient, that connects to an echo server. The
echo server receives data from its client and echoes it back. The example EchoServer
implements an echo server. (Alternatively, the client can connect to any host that supports the
Echo Protocol.)
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);
try ( // try-with-resources statement
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader ( new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn =
new BufferedReader ( new InputStreamReader(System.in))
)
1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the server's
protocol.
4. Close the streams.
5. Close the socket.
Example. EchoServer
import java.net.*;
import java.io.*;
public class EchoServer {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: java EchoServer <port number>");
System.exit(1);
}
int portNumber = Integer.parseInt(args[0]);
try (
ServerSocket serverSocket = new ServerSocket(Integer.parseInt(args[0]));
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader( new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
Running program
java EchoServer 7
java EchoClient echoserver.example.com 7
Knock Knock Example
• https://docs.oracle.com/javase/tutorial/netw
orking/sockets/clientServer.html