Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Which protocol this simple chat application is using?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Which protocol this simple chat application is using?

    Which protocol this simple chat application is using?
    TCP or UDP?

    Server.java
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Server extends JFrame{
     
       private JTextField userText;
       private JTextArea chatWindow;
       private ObjectOutputStream output;
       private ObjectInputStream input;
       private ServerSocket server;
       private Socket connection;
     
       //constructor
       public Server(){
          super("Messenger");
          userText = new JTextField();
          userText.setEditable(false);
          userText.addActionListener(
             new ActionListener(){
                public void actionPerformed(ActionEvent event){
                   sendMessage(event.getActionCommand());
                   userText.setText("");
                }
             }
          );
          add(userText, BorderLayout.NORTH);
          chatWindow = new JTextArea();
          add(new JScrollPane(chatWindow));
          setSize(300,150);
          setVisible(true);
       }
     
       //set up and run the server
       public void startRunning(){
          try{
             server = new ServerSocket(6789, 100);
             while(true){
                try{
                   waitForConnection();
                   setupStreams();
                   whileChatting();
                }catch(EOFException eofException){
                   showMessage("\n Server ended the connection! ");
                }finally{
                   closeCrap();
                }
             }
          }catch(IOException ioException){
             ioException.printStackTrace();
          }
       }
     
       //wait for connection, then display connection information
       private void waitForConnection() throws IOException{
          showMessage(" Waiting for someone to connect... \n");
          connection = server.accept();
          showMessage(" Now connected to " + connection.getInetAddress().getHostName());
       }
     
       //get stream to send and receive data
       private void setupStreams() throws IOException{
          output = new ObjectOutputStream(connection.getOutputStream());
          output.flush();
          input = new ObjectInputStream(connection.getInputStream());
          showMessage("\n Streams are now setup! \n");
       }
     
       //during the chat conversation
       private void whileChatting() throws IOException{
          String message = " You are now connected! ";
          sendMessage(message);
          ableToType(true);
          do{
             try{
                message = (String) input.readObject();
                showMessage("\n" + message);
             }catch(ClassNotFoundException classNotFoundException){
                showMessage("\n I dont know what that user sent!");
             }
          }while(!message.equals("CLIENT - END"));
       }
     
       //close streams and sockets after you are done chatting
       private void closeCrap(){
          showMessage("\n Closing connections... \n");
          ableToType(false);
          try{
             output.close();
             input.close();
             connection.close();
          }catch(IOException ioException){
             ioException.printStackTrace();
          }
       }
     
       //send a message to client
       private void sendMessage(String message){
          try{
             output.writeObject("SERVER - " + message);
             output.flush();
             showMessage("\nSERVER - " + message);
          }catch(IOException ioException){
             chatWindow.append("\n ERROR: I CANT SEND THAT MESSAGE");
          }
       }
     
       //updates chatWindow
       private void showMessage(final String text){
          SwingUtilities.invokeLater(
             new Runnable(){
                public void run(){
                   chatWindow.append(text);
                }
             }
          );
       }
     
       //let the user type stuff into their box
       private void ableToType(final boolean tof){
          SwingUtilities.invokeLater(
             new Runnable(){
                public void run(){
                   userText.setEditable(tof);
                }
             }
          );
       }
     
    }

    Client.java

    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Client extends JFrame{
     
       private JTextField userText;
       private JTextArea chatWindow;
       private ObjectOutputStream output;
       private ObjectInputStream input;
       private String message = "";
       private String serverIP;
       private Socket connection;
     
       //constructor
       public Client(String host){
          super("Client - User !");
          serverIP = host;
          userText = new JTextField();
          userText.setEditable(false);
          userText.addActionListener(
             new ActionListener(){
                public void actionPerformed(ActionEvent event){
                   sendMessage(event.getActionCommand());
                   userText.setText("");
                }
             }
          );
          add(userText, BorderLayout.NORTH);
          chatWindow = new JTextArea();
          add(new JScrollPane(chatWindow), BorderLayout.CENTER);
          setSize(300,150);
          setVisible(true);
       }
     
       //connect to server
       public void startRunning(){
          try{
             connectToServer();
             setupStreams();
             whileChatting();
          }catch(EOFException eofException){
             showMessage("\n Client terminated connection");
          }catch(IOException ioException){
             ioException.printStackTrace();
          }finally{
             closeCrap();
          }
       }
     
       //connect to server
       private void connectToServer() throws IOException{
          showMessage("Attempting connection... \n");
          connection = new Socket(InetAddress.getByName(serverIP), 6789);
          showMessage("Connected to: " + connection.getInetAddress().getHostName() );
       }
     
       //set up streams to send and receive messages
       private void setupStreams() throws IOException{
          output = new ObjectOutputStream(connection.getOutputStream());
          output.flush();
          input = new ObjectInputStream(connection.getInputStream());
          showMessage("\n Your streams are now good to go! \n");
       }
     
       //while chatting with server
       private void whileChatting() throws IOException{
          ableToType(true);
          do{
             try{
                message = (String) input.readObject();
                showMessage("\n" + message);
             }catch(ClassNotFoundException classNotfoundException){
                showMessage("\n I dont know that object type");
             }
          }while(!message.equals("SERVER - END"));
       }
     
       //close the streams and sockets
       private void closeCrap(){
          showMessage("\n closing chat down...");
          ableToType(false);
          try{
             output.close();
             input.close();
             connection.close();
          }catch(IOException ioException){
             ioException.printStackTrace();
          }
       }
     
       //send messages to server
       private void sendMessage(String message){
          try{
             output.writeObject("CLIENT - " + message);
             output.flush();
             showMessage("\nCLIENT - " + message);
          }catch(IOException ioException){
             chatWindow.append("\n something messed up sending message!");
          }
       }
     
       //change/update chatWindow
       private void showMessage(final String m){
          SwingUtilities.invokeLater(
             new Runnable(){
                public void run(){
                   chatWindow.append(m);
                }
             }
          );
       }
     
       //gives user permission to type crap into the text box
       private void ableToType(final boolean tof){
          SwingUtilities.invokeLater(
             new Runnable(){
                public void run(){
                   userText.setEditable(tof);
                }
             }
          );      
       }
    }
    Last edited by Kamrava; April 7th, 2014 at 06:44 AM.


  2. #2
    Junior Member Crash's Avatar
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Which protocol this simple chat application is using?

    If i'm not mistaken it's using TCP since its using Socket and not DatagramSocket but i'm not sure.

Similar Threads

  1. Need help regarding a chat application problem.
    By foxtrot in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 26th, 2014, 11:05 AM
  2. How can i create a chat application
    By Celestine in forum Java Theory & Questions
    Replies: 7
    Last Post: July 28th, 2013, 12:34 PM
  3. Chat application
    By Celestine in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 27th, 2013, 12:22 PM
  4. mobile chat application
    By teemone2001 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 21st, 2011, 03:33 AM
  5. Chat application problem
    By fembiz in forum Java Networking
    Replies: 1
    Last Post: December 11th, 2009, 05:21 AM