Sunday, January 15, 2012

Java - Simple console chat app

මෙන්න මචං මගේ ගානේ ජාවා වලට කැමති කට්ටියට සරල Chat application එකක්. කට්ටියට පොඩි පොඩි changes ටිකක් කරලා පට්ට chat එකක් හදා ගන්න මේක උදව්වක් වෙයි කියලා හිතනවා.
මෙන්න server එකේ code එක.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Thusitha
 */

public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(12345);
            while (true) {
                Socket incoming = serverSocket.accept();
                Runnable r = new ThreadedServer(incoming);
                Thread t = new Thread(r);
                t.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class ThreadedServer implements Runnable {
    private Socket incoming;
    private Scanner type = new Scanner(System.in);
    private Scanner reader;
    private PrintWriter writer;
    public ThreadedServer(Socket i) throws IOException {
        incoming = i;
        reader = new Scanner(incoming.getInputStream());
        new Thread(new Runnable() {
            public void run() {
                try {
                    InputStream inStream = incoming.getInputStream();
                    while (incoming.isConnected()) {
                        while (reader.hasNext()) {
                            System.out.println(reader.nextLine());
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                try {
                    OutputStream outStream = incoming.getOutputStream();
                    writer = new PrintWriter(outStream, true);
                    while (incoming.isConnected()) {
                        writer.println(type.nextLine());
                    }
                } catch (IOException ex) {
                    Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();
    }
    public void run() {
    }
}
මුලින්ම මේ code එක notepad file එකට copy කරලා Server.java කියලා java file විදියට save කරගන්න. ඊට පස්සේ පහල තියන code එක වෙන notepad file එකකට  copy කරලා Client.java විදියට save කරගන්න. අන්තිමට files දෙකම compile කරගන්න.

මෙන්න client එකේ code එක.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Thusitha
*/

class Client {
    private Scanner clientReader;
    private Socket s;
    private PrintWriter clientwriter;
    private Scanner clientType = new Scanner(System.in);
    public Client() {
        try {
            s = new Socket("localhost", 12345);
            clientReader = new Scanner(s.getInputStream());
            new Thread(new Runnable() {
                public void run() {
                    while (s.isConnected()) {
                        while (clientReader.hasNext()) {
                            System.out.println(clientReader.nextLine());
                        }
                    }
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();
            new Thread(new Runnable() {
                public void run() {
                    try {
                        OutputStream outStream = s.getOutputStream();
                        clientwriter = new PrintWriter(outStream, true);
                        while (s.isConnected()) {
                            clientwriter.println(clientType.nextLine());
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ThreadedServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }).start();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args) throws Exception {
        new Client();
    }
}

දැන් අපිට මේක test කරන්න පුලුවන්. මුලින්ම Server.java file එක run කරන්න. ඊට පස්සේ අලුතෙන් command prompt එකක් open කරලා Client.java file එක run කරන්න. දැන් මොනවා හරි type (ටයිප්) කරලා Enter ඔබලා බලන්න. ටයිප් කරන ටික අනික් command prompt එකේ පේනවා.

එහෙනම් කට්ටියට ජය වේවා

No comments:

Post a Comment