bpushia 0 Newbie Poster

I am trying to get the ST.java (Server Thread) to communicate with the CT.java (Client Thread), i am stuck at this point, i dont know rather it is something to do with the port # and host, or if it has anything to do with the Server thread, please reply thanks.

CT.java Client Thread File

import javax.swing.*;
import java.awt.*; 
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.net.*;
import java.io.*;

class ClientThread extends Thread
{
JTextArea ta1, ta2;

static final String newline = System.getProperty("line.separator");

public ClientThread(String str, JTextArea t1)
{
    super(str);
    ta1 = t1;
}

String m_strHost = "";
String m_strMsg = "";
int m_nPort = -1;

//public static void main(String arg[])
//{
//    int[] a;
//    a = new int[100];
//    //ClientThread ct = new ClientThread(arg[0], arg[1], arg[2]); //The Client thread requires three arguments to run
//    //ct.start(); // Client Thread starts here
//}

public ClientThread() 
{ 
    super("client");
    initThread("CU-109JST11", "7236", "test"); //Arguments are passed to ClientThread

}

    public void initThread(String host, String port, String msg) 
    {

        m_strHost = host; 
        m_strMsg = msg; 
        try
        {
            m_nPort = Integer.parseInt(port);
        }
        catch (Exception e) { m_nPort = 7236; }
    }

    public void run()
    {
        int i = 0;
        while (true)
        {
            SendMessage(m_strMsg);
            try
            {
                sleep(3000);
            }
            catch (Exception e)
            {
            }
        }
    }

    protected void SendMessage(String message)
    {
        Socket client = null;
        DataOutputStream output = null;

        try
        {
            client = new Socket(m_strHost, m_nPort);
        }
        catch (UnknownHostException e)
        {
            return;
        }
        catch (IOException e)
        {
            return;
        }
        try
        {
            output = new DataOutputStream(client.getOutputStream());
        }
        catch (IOException e)
        {
            return;
        }

        try
        {
            output.writeBytes(message);
        }
        catch (IOException e)
        {
            return;
        }

        try
        {
            output.flush();
        }
        catch (IOException e)
        {
            return;
        }

        try
        {
            if (client != null)
            {
                client.close();
            }
        }
        catch (IOException e)
        {
        }
    }
}

public class CT extends JApplet //Java Applet is created
{
DrawPanel Prompt; // The DrawPanel is an instance of JPanel, Prompt is an instance of DrawPanel

public void init()
{
    //Execute a job on the event-dispatching thread:
    //creating this applet's GUI.
    try
    {
        javax.swing.SwingUtilities.invokeAndWait(new Runnable()
        {                                                           //The Original System code is transformed to an Applet based code
            public void run()
            {
                createGUI(); //Creation of GUI
            }
        });
    }
    catch (Exception e)
    {
        System.err.println("createGUI didn't successfully complete"); // !!Error Message of failed attempt to run applet
    }
}

public void createGUI() //The GUI is Created
{
    //Container c = getContentPane();

    add(Prompt = new DrawPanel(), BorderLayout.CENTER); //Adding Prompt to the center 

    JTextArea ta1 = new JTextArea(10,10); //A text area is created for sending and messages
    add(ta1, BorderLayout.CENTER); //The Text Area is added and Centered

    new ClientThread("client", ta1).start();

    setSize(400, 220);  //Size of frame is 400 x 220 pixels
    setVisible(true);
}

public static void main(String[] args) 
{
    CT hws = new CT();  //instantiate this class
}
}

ST.java Server Thread File::::::::::::::::::::::::::::::::::::::::::::::::::

import javax.swing.*;
import java.awt.*; 
import java.net.*;
import java.io.*;

/**
 * A simple server - it listens to incoming messages at a specific port number
 */
class ServerThread extends Thread
{
JTextArea ta1, ta2;

static final String newline = System.getProperty("line.separator");

public ServerThread(String str, JTextArea t1, JTextArea t2)
{
    super(str);
    ta1 = t1;
    ta2 = t2;
    initThread("7236"); //Arguments are passed to ClientThread
}

Socket client = null;   //socket through which to communicate to the client
ServerSocket server;    //the server socket
DataInputStream input = null;   //an input stream to receive incomming messages
MessageThread mt = null;//a thread to handle individual communications

/**
 * The constructor
 * port - the port number to listen to
 */
public ServerThread() 
{ 
    super("client");
    initThread("7236"); //Arguments are passed to ClientThread

}

public void initThread(String port)
{
    try
    {
        int answer = Integer.parseInt(port);//try to get the port number            
        server = new ServerSocket(answer);  //instantiate a new socket object
        answer = server.getLocalPort();     //get the local port in case our wanted port is not available
        ta1.append("Server waiting :");
    } 
    catch (Exception e) { }
}
/**
 * The active component of the thread class
 */
public void run()
{
    //listen for incoming client requests forever until stopped
    while(true)
    {
        try
        {
            if ( server != null )
            {
                client = server.accept(); //if a client requests a connection accept it
            }
            else {  break;  }
        }
        catch (IOException e)
        {
            ta1.append("unable to accept client request");
            continue;
        }

        //if connection was established then try to read the message coming from the
        //client using this thread
        mt = new MessageThread(this, client);
        mt.start();
    }
}

/**
 * Starts the server and listen at a specific port
 * arg[0] - the port number to listen to
 */
//public static void main(String arg[])
//{
//    ServerThread st = new ServerThread(arg[0]);
//    st.start();
//} 
}

/**
 * A simple thread to read client messages
 */
class MessageThread extends Thread
{
JTextArea ta1, ta2;

static final String newline = System.getProperty("line.separator");

public MessageThread(String str, JTextArea t1, JTextArea t2)
{
    super(str);
    ta1 = t1;
    ta2 = t2;

}

protected ServerThread parent;  //the reference to the server 
protected Socket client;        //the socket connected to the client
BufferedReader d;               //a buffer to collect the characters send by the client
BufferedWriter dOut;

/**
 * The constructor
 */
public MessageThread(ServerThread p, Socket c)
{
    super("MessageThread"); //call the constructor of our parent class
    parent = p;             //save the parent for later
    client = c;             //save the client for later
}

/**
 * The avtive component of this thread class
 */
public void run()
{
    String message = "";            //an object to store the message
    DataInputStream input = null;   //an input stream to receive client messages
    DataOutputStream output = null;

    try
    {
        input = new DataInputStream(client.getInputStream());//try to get the input stream
        d = new BufferedReader(new InputStreamReader(input));//try to create the buffer

    }
    catch (IOException e)
    {
        return;
    }

    try
    {
        output = new DataOutputStream(client.getOutputStream());//try to get the input stream
        //dOut = new BufferedWriter(new OutputStreamWriter(output));//try to create the buffer

    }
    catch (IOException e)
    {
        return;
    }

    try
    {
        String temp = d.readLine(); //read the message client sent
        //System.out.print((char) 0x07);
        System.out.println(temp);   //print it to the console
        input.close();              //close the input stream
        //dOut.write("Server Says: "+temp);
        ta2.append("Server Says: " + temp);
        //d.close();                    //close the buffer
        output.flush();
        output.close();
        client.close();             //close the client socket               
    }
    catch (IOException e)
    {
        return;
    }
}
}

public class ST extends JApplet
{
DrawPanel Prompt;

public void init()
{
    //Execute a job on the event-dispatching thread:
    //creating this applet's GUI.
    try
    {
        javax.swing.SwingUtilities.invokeAndWait(new Runnable()
        {
            public void run()
            {
                createGUI(); //Creation of GUI
            }
        });
    }
    catch (Exception e)
    {
        System.err.println("createGUI didn't successfully complete"); // !!Error Message of failed attempt to run applet
    }
}

public void createGUI()
{
    //Container c = getContentPane();

    System.out.println("skdaj");
    //add(Prompt = new DrawPanel(), BorderLayout.CENTER); //Adding Canvas to the center

    JTextArea ta1 = new JTextArea(7,10);

    JTextArea ta2 = new JTextArea(10,10);
    add(ta1, BorderLayout.NORTH);
    add(ta2, BorderLayout.SOUTH);

    new ServerThread("client",ta1, ta2).start();
    new MessageThread("client", ta1, ta2).start();

    setSize(350, 275);
    setVisible(true);
}

public static void main(String[] args)
{
    ST hws = new ST();  //instantiate this class
}
}