/* posted Today 17:07 Profile for Simon Evans Email Simon Evans Dear Programmers, I am currently experiencing difficulty with a Client side program, which runs in response to a Server program, or at least would, if I could get it in order. The difficulty stems from the fact that I can't subclsss (extend) it from the Jframe and Thread class using extends JFrame, Thread as the compiler doesn't like it, so I had the notion of putting the main method in an instantiating program, and declaring that to extend Thread and have the Client class extend JFrame. The problem is that the Client program has an ActionListener/Performed method - which will call another method(sendMessage()) within Client program - it must be called from within the Action method - but without the object of class Client it can't call the sendMessage(), and with the Client Object being declared within the ActionListener method - it will just keep recreating the clsss Client - upon the ActionListener being activated ie: pressing the 'SEND' button keeps recreating another ClientGUI, complete with 'SEND' button, for the undending pressing of. Anyhow I'll put the code below,as it can tell it better than I can, Clsss Server runs okay, but without ClientGUI its obselete. ClientGUI creates the GUI but without the ActionPerformed method it is a non-starter. */ //*************************************************************************** import java.net.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.util.*; import java.awt.event.*; import java.awt.Window.*; import java.awt.Label; public class ClientServerTest { // extends Thread { //********************************************* // Starts here public static void main(String args[]){ Server.main(new String[]{}); // start the server final ClientGUI cGUI = new ClientGUI(); Runtime.getRuntime().addShutdownHook(new Thread(){ // @Override public void run() { // NOT called??? System.out.println("Shutting down"); cGUI.shutdown(); } }); } // end main() } // end class ClientServerTest //************************************************************************** class ClientGUI extends JFrame { Socket socket; BufferedReader in; PrintStream out; String host; int port = 1234; // String message; private JFrame frame; private JTextField TeF = new JTextField(25); JTextArea TeA = new JTextArea(20,30); JScrollPane scroll = new JScrollPane(TeA); JButton b1 = new JButton("SEND"); String fileName = " "; String savedFile = " "; JPanel west = new JPanel(); JPanel east = new JPanel(); JPanel south = new JPanel(); MyJButtonListener myJButtonListener; final public static String ShutDownMsg = "ShuttingDown"; private void setupGUI() { TeA.setEditable(false); // output only TeA.setBackground(Color.lightGray); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(BorderLayout.EAST,east); west.add(scroll); c.add(BorderLayout.WEST,west); south.add(TeF); south.add(b1); c.add(BorderLayout.SOUTH,south); myJButtonListener = new MyJButtonListener(this); //<<<<<<<<<<< b1.addActionListener(myJButtonListener); setSize(400,380); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //---------------------------------------- // Constructor ClientGUI(){ super("Client - Server test"); setupGUI(); getConnection(); receiveMessage(); } public void getConnection() { try { host = InetAddress.getLocalHost().getHostAddress(); socket = new Socket(host,port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintStream(socket.getOutputStream()); // System.out.println("Client - getConn out=" + out); }catch(UnknownHostException e){ System.out.println("Client - getConnection():" + e); }catch(IOException e) { System.out.println("Client - getConnection():" + e); } } public void sendMessage(String message) { message = TeF.getText(); System.out.println("Client sent:- " + message); // System.out.println("out= " + out); out.println(message); } public void receiveMessage() { try { while (true){ String message = in.readLine(); System.out.println("Client Received:" + message); TeA.append(message + "\n"); TeF.setText( ""); // clear input area TeF.requestFocus(); } // end while() }catch(IOException e) { System.out.println("Client - receiveMessage():" + e); } } public void shutdown() { sendMessage(ShutDownMsg); } //--------------------------------------------- class MyJButtonListener implements ActionListener{ ClientGUI clientgui; MyJButtonListener(ClientGUI cg) { clientgui = cg; // save } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("SEND")) { // ClientGUI clientgui = new ClientGUI(); clientgui.sendMessage(TeF.getText()); }else{ System.out.println("unknown ae=" + e); } } } } // end class ClientGUI //*************************************************************************** class Server extends Thread { ServerSocket server; Socket connection; BufferedReader in; PrintStream out; int port = 1234; // String message; public static void main(String args[]) { Server server = new Server(); server.start(); } public void run() { getConnection(); recieveMessage(); } private void getConnection() { try { server = new ServerSocket(port); System.out.println("Waiting for client..."); connection = server.accept(); System.out.println("Server got conection: " + connection); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); out = new PrintStream(connection.getOutputStream()); } catch(IOException e) { System.out.println("Server - getConnection();" + e); } } private void recieveMessage() { try { while(true) { String message = in.readLine(); System.out.println("Server - recMsg msg=" + message); sendMessage(message); } } catch(IOException e) { System.out.println("Server -receiveMessage():" + e); } } private void sendMessage(String message){ out.println(message); } } // end class Server //*************************************************************************** /*As you may know, you have to run the Server program first, then the MainMethod to launch the ClientGUI program. I think the methods in ClientGUI are coded alright - to put input to text field to input and output streams and to transfer message thus initialised to the textArea. Anyway thank you much for your help, particularly as most people out there are preoccupied with loot before they're prepared to offer any help. Yours Simon */ /* >>>> Sample output: <<<< Waiting for client... Server got conection: Socket[addr=/127.0.0.1,port=1162,localport=1234] Client - getConn out=java.io.PrintStream@64c34e Client sent:- sdsdsdf out= java.io.PrintStream@64c34e Server - recMsg msg=sdsdsdf Client Received:sdsdsdf */