Thread: Java Chat Application

Results 1 to 9 of 9
  1. #1 Java Chat Application 
    Development Services √

    Oogle's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    3,975
    Thanks given
    650
    Thanks received
    516
    Rep Power
    483
    Firstly, I just want to say I know this isn't the most impressive thing but for me personally this is a big achievement and I'd like to share it here.

    This is the first time I've played around with Java networking.

    You can find the download here and try it out for yourself: https://github.com/Oogle-Boogle/ChatApplication/

    Spoiler for Client GUI:
    Attached image


    A preview of the server side

    Code:
    package Server;
    
    import Client.Client;
    
    import java.awt.*;
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import static Client.Client.messageArea;
    
    public class Engine {
    
        ServerSocket server;
        Socket socket;
    
        BufferedReader br;
        PrintWriter out;
    
        int port = 7777;
    
        /**
         * Constructor..
         */
        public Engine() {
            try {
                System.out.println("Gets here");
                server = new ServerSocket(port);
                System.out.println("Server socket: " + server);
                while (true) {
                    socket = server.accept();
                    br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    out = new PrintWriter(socket.getOutputStream());
    
                    startReading();
                    startWriting();
    
                    System.out.println("Server has successfully loaded and is now ready to be connected to.");
                }
            } catch (Exception e) {
                System.out.println("Error loading the chat Engine.");
                e.printStackTrace();
            }
        }
    
        public void startReading() {
            Runnable read = () -> {
                System.out.println("Reader has started.");
                try {
                    while (true) {
                        String msg = br.readLine();
                        if (msg.equalsIgnoreCase("exit")) {
                            System.exit(0);
                        }
    //                    System.out.println("Server: " + msg + "\n");
                        messageArea.setAlignmentX(messageArea.RIGHT_ALIGNMENT);
                        messageArea.append("Server: " + msg + "\n");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            new Thread(read).start();
        }
    
        public void startWriting() {
            System.out.println("Writer has started.");
            Runnable write = () -> {
                try {
                    while (!socket.isClosed()) {
                        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
                        String content = br1.readLine();
    
                        out.println(content);
                        out.flush();
                        //Now lets close the socket if we type exit.
                        if (content.equalsIgnoreCase("exit")) {
                            socket.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            new Thread(write).start();
        }
    
        public static void main(String[] args) {
            System.out.println("The server is starting...");
             new Engine();
        }
    }


    Attached image

    Attached image
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Bossman

    ISAI's Avatar
    Join Date
    Sep 2012
    Posts
    1,916
    Thanks given
    655
    Thanks received
    1,366
    Rep Power
    5000
    Hehe. Looks good man, good luck!
    Reply With Quote  
     

  4. Thankful user:


  5. #3  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Quote Originally Posted by Oogle View Post
    Firstly, I just want to say I know this isn't the most impressive thing but for me personally this is a big achievement and I'd like to share it here.

    This is the first time I've played around with Java networking.

    You can find the download here and try it out for yourself: https://github.com/Oogle-Boogle/ChatApplication/

    Spoiler for Client GUI:
    Attached image


    A preview of the server side

    Code:
    package Server;
    
    import Client.Client;
    
    import java.awt.*;
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import static Client.Client.messageArea;
    
    public class Engine {
    
        ServerSocket server;
        Socket socket;
    
        BufferedReader br;
        PrintWriter out;
    
        int port = 7777;
    
        /**
         * Constructor..
         */
        public Engine() {
            try {
                System.out.println("Gets here");
                server = new ServerSocket(port);
                System.out.println("Server socket: " + server);
                while (true) {
                    socket = server.accept();
                    br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    out = new PrintWriter(socket.getOutputStream());
    
                    startReading();
                    startWriting();
    
                    System.out.println("Server has successfully loaded and is now ready to be connected to.");
                }
            } catch (Exception e) {
                System.out.println("Error loading the chat Engine.");
                e.printStackTrace();
            }
        }
    
        public void startReading() {
            Runnable read = () -> {
                System.out.println("Reader has started.");
                try {
                    while (true) {
                        String msg = br.readLine();
                        if (msg.equalsIgnoreCase("exit")) {
                            System.exit(0);
                        }
    //                    System.out.println("Server: " + msg + "\n");
                        messageArea.setAlignmentX(messageArea.RIGHT_ALIGNMENT);
                        messageArea.append("Server: " + msg + "\n");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            new Thread(read).start();
        }
    
        public void startWriting() {
            System.out.println("Writer has started.");
            Runnable write = () -> {
                try {
                    while (!socket.isClosed()) {
                        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
                        String content = br1.readLine();
    
                        out.println(content);
                        out.flush();
                        //Now lets close the socket if we type exit.
                        if (content.equalsIgnoreCase("exit")) {
                            socket.close();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            };
            new Thread(write).start();
        }
    
        public static void main(String[] args) {
            System.out.println("The server is starting...");
             new Engine();
        }
    }
    Love to see it, great way to start learning about Java networking - funnily enough, the same approach I took when I was first learning! Stay curious!
    Reply With Quote  
     

  6. Thankful user:


  7. #4  
    Development Services √

    Oogle's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    3,975
    Thanks given
    650
    Thanks received
    516
    Rep Power
    483
    Quote Originally Posted by hc747 View Post
    Love to see it, great way to start learning about Java networking - funnily enough, the same approach I took when I was first learning! Stay curious!
    Going to look into Netty as a few people in discord told me I should do that instead of this just to make a more practical and more user friendly (something along those lines).


    Attached image

    Attached image
    Reply With Quote  
     

  8. Thankful user:


  9. #5  
    (Official) Thanksgiver

    Arham's Avatar
    Join Date
    Jan 2013
    Age
    23
    Posts
    3,415
    Thanks given
    7,254
    Thanks received
    1,938
    Rep Power
    3905
    Quote Originally Posted by hc747 View Post
    Love to see it, great way to start learning about Java networking - funnily enough, the same approach I took when I was first learning! Stay curious!
    I remember getting on a call back in the day with you showing me it as well.
    Attached image
    Attached image
    Quote Originally Posted by MrClassic View Post
    Arham is the official thanker!
    List of my work here!
    Reply With Quote  
     

  10. Thankful user:


  11. #6  
    L O S E Y O U R S E L F
    Format's Avatar
    Join Date
    Aug 2013
    Posts
    2,311
    Thanks given
    1,131
    Thanks received
    1,461
    Rep Power
    5000
    Good on you bro, its still a great achievement and 99% of people wouldn't know where to start let alone have the patience to learn. Im sure you have a greater understanding of networking now, keep up little projects like this and you'll be surprised how far you will go.

    I personally loved the learning experience of network programming the most out of all the categories. Network and graphics programming my two favourites to learn.


    Reply With Quote  
     

  12. Thankful user:


  13. #7  
    Professional Lurker
    null's Avatar
    Join Date
    Mar 2017
    Posts
    121
    Thanks given
    15
    Thanks received
    31
    Rep Power
    67
    Oogle i saw you were asking for project suggestions an another post. I have a suggestion for your next project. Make an application with as the back-end an OOP language such as Java or any language you desire. Have the front-end be web based. For example a while ago i made an application that uses as front-end and C#, AHK and Javascript as back-end
    Reply With Quote  
     

  14. Thankful user:


  15. #8  
    Programmer

    Elysian's Avatar
    Join Date
    Dec 2018
    Posts
    157
    Thanks given
    10
    Thanks received
    79
    Rep Power
    694
    Looks nice! Keep it up, chat applications are always fun to make!
    Reply With Quote  
     

  16. #9  
    Banned
    Join Date
    Aug 2022
    Posts
    6
    Thanks given
    0
    Thanks received
    7
    Rep Power
    0
    Thanks for posting Oogle.
    This could potentially be used for anything really if you think about it whether it being implemented with RSPS even though there is a chat system already, live support, in-game reporting, osgp buying/selling, secret staff conversations, erm... you get the picture. It is impressive - projects like this could go long ways. Keep at it and before you know it Bill Gates will be calling.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. 614 with an Android Chat Application
    By Joris in forum Show-off
    Replies: 38
    Last Post: 05-11-2018, 11:26 AM
  2. [NODE.JS & SOCKET.IO] Chat application
    By Mr. Robot in forum Website Development
    Replies: 2
    Last Post: 07-15-2016, 10:37 PM
  3. [JAVA]My Chatting IRC Bot.
    By 42 in forum Application Development
    Replies: 7
    Last Post: 12-03-2009, 07:17 PM
  4. Communication (Java chat)
    By Java Jamie in forum Application Development
    Replies: 14
    Last Post: 10-12-2009, 01:54 AM
  5. Java Chat Server/Client
    By MVH-_- in forum Application Development
    Replies: 0
    Last Post: 06-11-2009, 08:30 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •