Computer Science Canada

Live Chat

Author:  S_Grimm [ Thu Oct 23, 2008 6:20 pm ]
Post subject:  Live Chat

Our programming class is divided into to class. the senior class(grade 12's) (4 people) and the junior class (grade 11's) (24 people). so the grade 12 class decided that we would like to be able to talk to each other without disturbing the teacher while he is teaching the grade 11 class. Hence the idea of a "Live Chat" was created. I have already O.K.ed this with the teacher, he say's it's a great idea... And i managed to get the basic program running. I can send and receive messages on two different computers on the network. unfortunately, I can only sen and receive in a specific order. ie. send, receive, send, receive or vise-versa.
Java:

import java.net.*;
import java.io.*;
public class chatServer
{
    public static void main (String[] args) throws IOException
    {
        InetAddress lclAdr = InetAddress.getLocalHost ();
        System.out.println ("Your IP address is " + lclAdr.getHostAddress ());
        String datavar;
        ServerSocket ss;
        ss = new ServerSocket (5055);
        Socket mySocket;
        mySocket = ss.accept ();
        InputStreamReader isr = new InputStreamReader (mySocket.getInputStream ());
        BufferedReader br = new BufferedReader (isr);
        PrintWriter pw = new PrintWriter (mySocket.getOutputStream (), true);
        InputStreamReader reader = new InputStreamReader (System.in);
        BufferedReader input = new BufferedReader (reader);
        datavar = br.readLine ();
        System.out.println (datavar);
        pw.println (input.readLine ());
    }
}


That is the Server or Host computer.
Java:

import java.net.*;
import java.io.*;
public class chatClient
{
    public static void main (String[] args) throws IOException
    {
        InetAddress lclAdr = InetAddress.getLocalHost ();
        System.out.println ("Your IP address is " + lclAdr.getHostAddress ());
        Socket mySocket = new Socket ("192.168.1.100", 5055); //input your server or host ip address
        String datavar;
        InputStreamReader isr = new InputStreamReader (mySocket.getInputStream ());
        BufferedReader br = new BufferedReader (isr);
        PrintWriter pw = new PrintWriter (mySocket.getOutputStream (), true);
        pw.print ("IP Address of connecting computer : " + lclAdr.getHostAddress ());
        datavar = br.readLine ();
    }
}

that is the Client or remote computer. it works, but i want to be able to say open it up to 4 people AND allow multiple incoming messages in a row (ie receive, receive, send, receive) from any of the people connected. Any help/ advice would be appreciated.
P.S. Please don't knock the idea. The class is a high school programming class, and we really don't like to shout halfway across the room, over the grade elevens and the teacher. and we don't really want to move across the class either.

Author:  HellblazerX [ Thu Oct 23, 2008 7:27 pm ]
Post subject:  Re: Live Chat

You might want to look into Threads. Threads are like processes in Turing, but not done so poorly. Basically you'll want separate threads for your server and clients, that way they won't interfere with one another if one goes idle. I did do some networking in high school, but it's been a while, so I've pretty much forgotten it all. I'll dig around my harddrive and see if I can find the files for it.

Author:  S_Grimm [ Thu Oct 23, 2008 7:37 pm ]
Post subject:  RE:Live Chat

thanks. ill look into it.
edit: i was digging around in some stuff i had on my hard drive, and managed to find and modify this from javanotes5.0.2
edit 2: updated the code.
Java:

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

public class ChatServer extends JApplet implements ActionListener, Runnable
{
    private JTextArea textbox; //shows transmitted and recived messages
    private JTextField input;  // A text-input box
    private Thread chat;
    private boolean running;  // If False, quits applet
    /**
     * Initialize the applet.
     */

    public void init ()
    {
        JPanel content = new JPanel ();
        content.setBackground (Color.LIGHT_GRAY);
        content.setLayout (new BorderLayout (3, 3));
        content.setBorder (BorderFactory.createLineBorder (Color.GRAY, 3));
        textbox = new JTextArea ();
        textbox.setEditable (false);
        content.add (new JScrollPane (textbox), BorderLayout.CENTER);
        JPanel bottom = new JPanel ();
        bottom.setBackground (Color.LIGHT_GRAY);
        bottom.setLayout (new BorderLayout (3, 3));
        content.add (bottom, BorderLayout.SOUTH);
        input = new JTextField ();
        input.setBackground (Color.WHITE);
        input.addActionListener (this);
        bottom.add (input, BorderLayout.CENTER);
        JButton send = new JButton ("Send");
        send.addActionListener (this);
        bottom.add (send, BorderLayout.EAST);
        bottom.add (new JLabel ("Text to send:"), BorderLayout.WEST);
        setContentPane (content);
    }


    //Allows user to click Send button
    public void actionPerformed (ActionEvent evt)
    {
        postMessage ("SENT: " + input.getText ());
        input.selectAll ();
        input.requestFocus ();
        if (running == false)
        {
            chat = new Thread (this);
            running = true;
            chat.start ();
        }
    }


    private void postMessage (String message)
    {

        // InetAddress lclAdr = InetAddress.getLocalHost ();
        // textbox.append (lclAdr.getHostAddress () + "\n"); //should allow to post your ip address.

        textbox.append (message + "\n");
        //allows scrolling of text
        textbox.setCaretPosition (textbox.getDocument ().getLength ());
    }

/*        ServerSocket SS;
        Socket mySocket;
        SS = new ServerSocket (5055);
        mySocket = SS.accept ();
*/


    public void run ()
    {
        while (running)
        {
           /* try
            {
     
            }
            catch (Exception e)
            {
            }*/

        }
    }


    //When applet is stopped, make sure that the thread will terminate by setting running to false.

    public void stop ()
    {
        running = false;
        chat = null;
    }
}


The Issue I have with this now is trying to run an ServerSocket on it to accept incoming connections. I also need to be able to use a PrintWriter in this, by in order to do it, i need to call ServerSocket and a Socket variable. When i try to put the code into the actionPerformed section, it refuses to reconize the Socket. any ideas. (ps. When (if) this gets running, feel free to use it, modify it, or download and delete it)

Author:  S_Grimm [ Mon Oct 27, 2008 10:57 am ]
Post subject:  Re: Live Chat

Alright. I got the server to open up multiple ports and be able to recieve data. so far it is working... except for the fact i can only send or recive in that order. I couldn't find a tutorial on threads, before anyone asks. ill post the code. note: you need to manually punch in your ip address in the client program. shouldn't be hard to find where, i have a default ip address in there (192.168.0.100), just change it and make sure the port number (the number beside the ip address is 5050). if you are using 2 client computers change the port on the second one to 5051.

please note that you need at least 2 computers. one to run chatserverv2p4, and one to run chatclientv2p1 (p stands for point ie v2p4 is version 2.4)


: