<< Chapter < Page | Chapter >> Page > |
Listing 12 - The program named Java4660a.
/*File Java4660a.java Copyright 1998, R.G.Baldwin
Revised 01/10/14This program performs a simple echo test with localhost
by sending a line of text to the echo port, port 7.The computer must have been previously configured to
support the echo port.The output from this program is:echo: This is an echo test
**********************************************************/import java.net.*;
import java.io.*;import java.util.*;
class Java4660a{public static void main(String[] args){String server = "localhost";
int port = 7; //echo porttry{//Get a socket, connected to the specified server
// on the specified port.Socket socket = new Socket(server,port);
//Get an input stream from the socketBufferedReader inputStream =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Get an output stream to the socket. Note// that this stream will autoflush.
PrintWriter outputStream =new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()),true);//Send line of text to the server
outputStream.println("This is an echo test");//Get echoed line back from server and display it
System.out.println("echo: "+inputStream.readLine());//Close the socketsocket.close();
}//end trycatch(Exception e){
e.printStackTrace();}//end catch
}//end main}//end class Java4660a
Listing 13 - The program named Java4660b.
/*File Sockets04.java Copyright 1998, R.G.Baldwin
Revised 01/10/14This program gets and displays the date and time on the
server at "localhost".It also displays the current date and time in Austin,
TX, or wherever the program happens to be run.The computer must have been previously configured to
support the daytime port.One output from this program was:
Current time at localhost2:39:55 PM 1/10/2014
Current time in Austin, TX:Fri Jan 10 14:39:55 CST 2014
**********************************************************/import java.net.*;
import java.io.*;import java.util.*;
class Sockets04{public static void main(String[] args){String server = "localhost";
int port = 13; //daytime porttry{//Get a socket, connected to the specified server
// on the specified port.Socket socket = new Socket(server,port);//Get an input stream from the socket
BufferedReader inputStream =new BufferedReader(new InputStreamReader(
socket.getInputStream()));System.out.println("Current time at " + server);System.out.println(inputStream.readLine());
System.out.println("Current time in Austin, TX:");System.out.println(new Date());//Close the socket
socket.close();}//end try
catch(Exception e){e.printStackTrace();
}//end catch UnknownHostException}//end main
}//end class Sockets04
Listing 14 - The program named Java4660c.
/*File Java4660c.java Copyright 1998, R.G.Baldwin
Revised 01/10/14This program is a simple http client (web browser)
implemented using sockets.The program implements just enough of the http protocol
to make it capable of getting an html page from anhttp server.
The program, acting as an http client, sends a GETcommand to the server specifying a particular path and
file name.The server is a Tomcat server operating as localhost
on port 8080.As of 01/10/14, the output from this program was as
follows.<html><head><title>Java4570b</title></head><body><form method='get'
action='http://localhost:8080/Java4570b.jsp'><p>Enter a name and press the button</p><p>Name:<input type='text' name='firstName'/></p><input type='submit' value='Submit Name'/><br/><br/>Your session ID and list of names is:<br/>Empty<br/></form></body></html>**********************************************************/
import java.net.*;import java.io.*;
class Java4660c{public static void main(String[] args){String server = "localhost";
int port = 8080; //http port on localhosttry{
//Get a socket, connected to the specified server// on the specified port.
Socket socket = new Socket(server,port);//Get an input stream from the socket
BufferedReader inputStream =new BufferedReader(new InputStreamReader(
socket.getInputStream()));//Get an output stream to the socket. Note
// that this stream will autoflush.PrintWriter outputStream =
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);//Send a GET command to the server
outputStream.println("GET /Java4570b.jsp");//Declare a String to read lines into.
String line = null;//Loop reading and displaying lines until null// is received.
while((line = inputStream.readLine()) != null)System.out.println(line);//Close the socket
socket.close();}//end try
catch(Exception e){e.printStackTrace();
}//end catch}//end main
}//end class Java4660c//=======================================================//
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?