aspose file tools*
The moose likes Beginning Java and the fly likes Downloading Java 1.5 (or Java 5.0 also Socket programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Downloading Java 1.5 (or Java 5.0 also Socket programming" Watch "Downloading Java 1.5 (or Java 5.0 also Socket programming" New topic
Author

Downloading Java 1.5 (or Java 5.0 also Socket programming

Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
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.


Simon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Please use code tags round your code, and put the indentation in; it makes it much easier to read.

Suggest you use what is called JDK6u6 or JDK6 update 6, which is the most recent stable version available. Remember to use the JDK, not JRE.

Suggest you go through the Java Tutorials That last link gives links to instructions for Windows or Solaris/Linux and NetBeans. I suggest not to use NetBeans, but to use the command-line arguments.
Be sure to remember the name of the folder you are installing Java into.
This is another link to the installation instructions, and this is another downloads link.
If you use Windows, make sure to set the PATH permanently as it says in the instructions and forget that it says "optional." There is usually no need to alter your CLASSPATH. Remember you have to open a new command prompt window to get the PATH applied. There are ways you can reset the PATH for the lifetime of a command window; see Rob Prime's post on this thread for how to do it on Windows, on *nix you can write

export PATH=/usr/java/jdk1.6.0_06/bin/:$PATH

or whatever to set the PATH for the lifetime of the terminal window.

Remember the PATH starts with the drive letter (Windows) or / (*nix) and ends with bin on everything.

Any code which will run on Java5 will also run happily on Java6, so you might as well use the most recent version.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39882
    
  28
Please Post Real Code; use ctrl-c ctrl-v to preserve exactly what you have got. I tried to run what you presented and it won't compile; there are spelling errors:
  • "Buffered reader" instead of "BufferedReader"
  • "import java.net*;" for "import java.net.*;" and
  • "recieve message()" for "receiveMessage()".
  • Please check what you have got against what you can download here, and be very careful about spelling; even the tiniest mistake like those above, or not matching () or {} properly, will throw the compiler.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14352
        
      22

    Originally posted by Simon Evans:
    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)?

    It has not been renamed. Sun maintains two version numbers (1.5.0 and 5.0) for the same thing. See: Version 1.5.0 or 5.0?

    See the download page for Java SE 5. You can find the installation instructions there.


    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Simon Evans
    Ranch Hand

    Joined: Mar 27, 2008
    Posts: 93
    Dear Programmers,
    Thank you for your observations - if only there were the equivalent of Word's spellchecker for Java code.

    Re: Jasper's reply: on page 5, column 2 of Head First Java it says "The next number in the name sequence would be "3" but calling Java 1.5 Java 3 seemed more confusing, so they decided to name it Java 5.0 to match the "5" in version "1.5"

    I have been advised to change the 'Connection' program to replace the DataInputStream object with a BufferedReader object to render program JDK 1.5 compliant, upon which it appears to compile (with the syntax Campbell Ritchie suggested) and run.
    Thank you for your help.
    So long for now
    Yours Simon.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Downloading Java 1.5 (or Java 5.0 also Socket programming