Skip to content
Snippets Groups Projects
Commit 190cdc73 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

reformat, improve exception to silence warning

parent ee34f295
No related branches found
No related tags found
No related merge requests found
package tcpclient; package tcpclient;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
/** /**
* Before, we always used telnet to connect to the server. * Before, we always used telnet to connect to the server. Here we are now
* Here we are now writing our own program to do the connection. * writing our own program to do the connection.
* *
* As you will see, when we run this after we start the server * As you will see, when we run this after we start the server we will see the
* we will see the same string telnet printed, sent by the server. * same string telnet printed, sent by the server. The output at the server will
* The output at the server will show different socket pairs for * show different socket pairs for each time we ran it.
* each time we ran it. *
*
* @author mcgredo * @author mcgredo
*/ */
public class TcpClient { public class TcpClient {
public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1 public final static String LOCALHOST = "0:0:0:0:0:0:0:1"; // String constant, i.e. 127.0.0.1
public static void main(String[] args) public static void main(String[] args) {
{ try {
try while (true) {
{ System.out.println("creating socket");
while(true)
{ // We request an IP to connect to ("localhost") and
System.out.println("creating socket"); // port number at that IP (2317). This establishes
// a connection to that IP in the form of the Socket
// We request an IP to connect to ("localhost") and // object; the server uses a ServerSocket to wait for
// port number at that IP (2317). This establishes // connections.
// a connection to that IP in the form of the Socket Socket socket = new Socket(LOCALHOST, 2317); // locohost?
// object; the server uses a ServerSocket to wait for
// connections. // Read the single line written by the server. We'd
Socket socket = new Socket(LOCALHOST, 2317); // locohost? // do things a bit differently if many lines to be read
// from the server, instead of one only.
// Read the single line written by the server. We'd InputStream is = socket.getInputStream();
// do things a bit differently if many lines to be read InputStreamReader isr = new InputStreamReader(is);
// from the server, instead of one only. BufferedReader br = new BufferedReader(isr);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is); String serverMessage = br.readLine();
BufferedReader br = new BufferedReader(isr); System.out.println("==================================================");
System.out.println("Now we're talking!");
String serverMessage = br.readLine(); System.out.println("The message the server sent was " + serverMessage);
System.out.println("=================================================="); // socket gets closed, either automatically/silently this code (or possibly by server)
System.out.println("Now we're talking!"); } // end while(true)
System.out.println("The message the server sent was " + serverMessage); }
// socket gets closed, either automatically/silently this code (or possibly by server) catch (IOException e) {
} // end while(true) System.out.println("Problem with client: "); // describe what is happening
} System.out.println(e);
catch(IOException e) }
{
System.out.println("Problem with client: "); // describe what is happening
System.out.println(e);
}
// program exit: tell somebody about that // program exit: tell somebody about that
System.out.println("client exit"); System.out.println("client exit");
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment