Dear Programmers,
I have been told that past difficulties I have experienced with text code (specifically from the
Java Head First book) is due to not having downloaded the Java 1.5 (or Java 5.0 as it has been renamed).
I have tried several times, but non compilation of the code indicated that again, I went amiss somewhere in the downloading process. Is there a step by step pleb - friendly guide out there to downloading the Java 1.5 (5.0)?
Theres something else I wanted to ask regarding the coding of a Server /Socket program in order to multithread it.
As the Server and Client programs stand, it functions okay, in that there is an interaction between the consoles when they are run seperately, for if one console is shut down, the others program closes, however according to the text, in order to Multithread, one has to modify the Server program, and also run a class called Connection. The text says that threading the client program isn't necessary, although it can be done in the same way as the Server program was threaded.
Anyway this are the pre-threaded programs I have been referring to:-
import java.net.*;
import java.io.*;
class Client extends
Thread {
Socket socket;
BufferedReader in;
PrintStream out;
String IP = "127.0.0.1";
int port = 8000;
String message;
public static void main(String args[])
{
Client client = new Client();
client.getConnection();
client.sendMessage();
client.recieveMessage();
}
private void getConnection()
{
try
{
socket = new Socket(IP, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
}
catch(UnknownHostException e)
{
System.out.println("Client - getConnection();" + e);
}
catch(IOException e)
{
System.out.println("Client - getConnection();" +e);
}
}
private void sendMessage()
{
message = "Hello Server!";
System.out.println("Client sent :-" + message);
out.println(message);
}
public void recieve message()
{
try
{
while(true)
{
message = in.readline();
System.out.println("Client Recieved:" + message);
}
}
catch(IOException e)
{
System.out.println("Client - recieveMessage();" +e);
}
}
}
**************************************************************************
import java.io.*;
import java.net*;
class Server
{
ServerSocket server;
Socket connection;
Buffered reader in;
PrintStream out;
int port = 8000;
String message;
public static void main(String args[])
{
Server server = new Server();
server.getConnection();
server.recieveMessage();
}
private void getConnection()
{
try
{
server = new ServerSocket(port);
System.out.println("Waiting for client..");
connection = server.accept();
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
out = new PrintStream(connection.getOutputStream());
}
catch(IOException e)
{
System.out.print("Server - getConnection();" +e);
}
}
private void recieveMessage()
{
try
{
while(true)
{
message = in.readLine();
sendMessage();
}
}
catch(IOException e)
{
System.out.println("Server - recieveMessage();" + e);
}
}
private void sendMessage()
{
out.println(message);
}
}
***************************************************************************
The programs run okay as they stand, thus far,
program Client producing the following output:-
Client sent:- Hello server!
Client Recieved: Hello server!
and the program Server producing the following output:-
Waiting for client
Anyway, to multithread the server, the text says, takes two steps, these being firstly to recode it so's getConnection() method's initialisation of Socket object 'connection' is replaced by an instantiation of a program 'Connection in an infinite loop ie: while(true) expression's statement, and secondly to run a program called Connection.
So this is the first step, according to the text, ie a rewrite of program Server:-
import java io;
import java.net.*;
class Server
{
ServerSocket server;
Socket connection;
BufferedReader in;
PrintStream out;
int port = 8000;
String message;
public static void main(String args[])
{
Server server = new Server();
server.getConnection();
server.recieveMessage();
}
private void getConnection()
{
try
{
server = new ServerSocket(port);
while (true)
{
System.out.println("Waiting for client..");
new Connection(server.accept());
}
}
}
catch(IOException e)
System.out.println("Server-getConnection();"+e);
}
}
private void recieveMessage()
{
try
{
while(true)
{
message = in.readLine();
sendMessage();
}
}
catch(IOException e)
{
System.out.println("Server- recieve message();" +e);
}
}
private void send Message()
{
out.println(message);
}
}
***************************************************************************
and this is the class Connection that the modified Server program refers to:-
import java io.*;
import java.net.*;
class Connection extends Thread
{
Socket connection;
DataInputStream in;
PrintStream out;
String message;
Connection(Socket connection)
{
this.connection = connection;
try
{
in = new DataInputStream(connection.getInputStream());
out = new PrintStream(connection.getOutputStream());
}
catch(IOException e)
{
System.out.println("Connection - Connection():" + e);
}
start();
}
public void run()
{
try
{
while(true)
{
message = in.readLine();
sendMessage();
}
}
catch(IOException e)
{
System.out.println("Connection - run(): " +e);
}
}
private void sendMessage()
{
out.println(message);
}
}
***************************************************************************
So, there you have it, according to the text, a threaded version of the Server /Client programs, except there is a problem - class Connection doesn't compile. Until I can guage what is wrong with it, I am currently stuck, I don't know if any of you can tell me how it might be altered to run, so's to thread the classes. I realise I am asking quite a lot but nothing ventured, nothing gained, if you don't ask you don't get and all that. Perhaps you could point me in the right direction - as it seems some authorities (ie the authors of the above text) seem to think, as appears fashionable in some circles nowadays, that you don't just learn from the mistakes you have made, but from those of others - absurd, yes, but its not my theory.