
























































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























































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."); }
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.
echoSocket = new Socket("taranis", 7);
out = new PrintWriter (echoSocket.getOutputStream(), true);
String userInput;
while ((userInput = stdIn.readLine()) != null) { out.println(userInput); System.out.println("echo: "+in.readLine()); }
out.close(); in.close(); stdIn.close(); echoSocket.close();