Docsity

Java Sockets - Java and the Web - Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Java Sockets - Java and the Web - Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Java Sockets

Reading from and Writing to a

Socket in Java

  • Let's look at a simple example that illustrates how a program can establish a connection to a server program using the Socket class and then, how the client can send data to and receive data from the server through the socket.

Echo Client description

  • EchoClient creates a socket thereby getting a connection to the Echo server. It reads input from the user on the standard input stream, and then forwards that text to the Echo server by writing the text to the socket. The server echoes the input back through the socket to the client. The client program reads and displays the data passed back to it from the server:

Echo Client code

import import java.io.;java.net.;

public public class static void EchoClient main(String[] args){ throws IOException {

Socket PrintWriter echoSocket out = = null;null; BufferedReader in = null; try { echoSocket = new Socket("taranis", 7); out in == new new BufferedReader(new PrintWriter(echoSocket.getOutputStream(), InputStreamReader( true); } catch (UnknownHostException e)^ echoSocket.getInputStream())); { System.err.println("Don't System.exit(1); know about host: taranis."); } catch System.err.println("Couldn't (IOException e) { get I/O for " System.exit(1);^ +^ "the^ connection^ to: taranis."); }

Echo Client walkthrough

Note that EchoClient both writes to

and reads from its socket, thereby sending data to and receiving data from the Echo server. Let's walk through the program and

investigate the interesting parts.

Echo Client try block

  • The three statements in the try block of the main method are critical. These lines establish the socket connection between the client and the server and open a PrintWriter and a BufferedReader on the socket: echoSocket = new Socket("taranis", 7); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream()));

echoSocket = new Socket("taranis", 7);

  • The second argument is the port number. Port number 7 is the port on which the Echo server listens.

out = new PrintWriter (echoSocket.getOutputStream(), true);

  • The second statement gets the socket's output stream and opens a PrintWriter on it.

Sending Data

  • To send data through the socket to the server, EchoClient simply needs to write to the PrintWriter. To get the server's response, EchoClient reads from the BufferedReader. The rest of the program achieves this.

While Loop

  • The while loop reads a line at a time from the standard input stream and immediately sends it to the server by writing it to the PrintWriter connected to the socket:

String userInput;

while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: "+in.readLine()); }

While Loop (continued)

  • The while loop continues until the user types an end-of-input character. That is, EchoClient reads input from the user, sends it to the Echo server, gets a response from the server, and displays it, until it reaches the end-of-input. The while loop then terminates and the program continues, executing the next four lines of code: out.close(); in.close(); stdIn.close(); echoSocket.close();

Housekeeping

out.close(); in.close(); stdIn.close(); echoSocket.close();

  • These lines of code fall into the category of house keeping. A well- behaved program always cleans up after itself, and this program is well-behaved. These statements close the readers and writers connected to the socket and to the standard input stream, and close the socket connection to the server. The order here is important. You should close any streams connected to a socket before you close the socket itself.

The Basics

  • Open a socket.
  • Open an input stream and output stream to the socket.
  • Read from and write to the stream according to the server's protocol.
  • Close the streams.
  • Close the socket.
  • Only step 3 differs from client to client, depending on the server. The other steps remain largely the same. Docsity.com

Writing a Datagram Client and Server

  • The example featured in this section consists of two applications: a client and a server. The server continuously receives datagram packets over a datagram socket. Each datagram packet received by the server indicates a client request for a quotation. When the server receives a datagram, it replies by sending a datagram packet that contains a one-line "quote of the moment" back to the client.