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 22 of 22

Thread: CHAT PROJECT

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default CHAT PROJECT

    i am writing the code for multiuser chat with GUI and i wanna create a quiz game where the all the users i.e the clients can aniticipate in the game. is there a way where we can combine the game and the users together in a single gui screen. the game is on the left box of the gui and chat space on the right. is it possible and can anyone please help me with this project??


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,151
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: CHAT PROJECT

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Is what you've described possible? Of course.

    Can we help you? Of course, when you have some code and/or specific questions that we can answer.

    How much Java/Swing knowledge and experience do you have? If little, I suggest you start with the Java Swing Tutorials. Ignore Oracle's bias towards using the Netbeans GUI Builder - Netbeans by itself is fine - and learn to code Swing by hand.

    Good luck!

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    thank you. i am using swing for the chat module. but i really need help with the connections strings. please help me?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    need help with the connections strings.
    Please explain what problems you are having.
    What do you mean by "connections strings"?

    Do you have an overall detailed design for how the programs in this project will work? Is there a server? How do the clients interact with the server?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    ok so there are two modules.first the multiuser chat having one server and many clients. i have created that using sockets and swing for gui. second is the GAME. now i wanna have a game beside the chat box which is QUIZ where all the clients are able to play the game alngside apart from chatting.. now the server is the main who changes the question and each client can answer them when it is there turn.. now the problem is how do i view the game in the chat screen alongside the chatbox to the clients?? and everytime the server changes the question it should change in all the client screen as well..is there any code where i can connect the game to the chat?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    how do i view the game in the chat screen
    What is the chat screen? For example a text area where text can be appended.
    What will be displayed in the chat screen?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,151
    My Mood
    Mellow
    Thanks
    187
    Thanked 660 Times in 647 Posts

    Default Re: CHAT PROJECT

    Have you written any client/server, chat, or networking apps in Java? There are several tutorials on the 'net that cover all of the basics helpful to understand these topics that you should complete, if you haven't already. With experience writing introductory exercises on these topics, console and graphical, you'll have a better understanding of how to connect the basic concepts to a create the project you're imagining.

  8. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    ok chat screen is the entire gui.. in which their is the chat box to chat and the quiz game. chat box is the text area where the text is displayed

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    That is one small detail in the design. What about the rest of the design for the project?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    im so sorry for the late reply.. i was actually coding for the design using swing.for now i have created the display.. i have written the code for chat client and chat server.. im running this project in eclipse... the thing is tht the code is working fine as it displays in the eclipse output area.. i have connected the code to the button and chat area of the display.. but the text doesnt appear in the chat area..but works fine in eclipse..can u please look into the code .. i can send the file... ??

    --- Update ---

    im so sorry for the late reply.. i was actually coding for the design using swing.for now i have created the display.. i have written the code for chat client and chat server.. im running this project in eclipse... the thing is tht the code is working fine as it displays in the eclipse output area.. i have connected the code to the button and chat area of the display.. but the text doesnt appear in the chat area..but works fine in eclipse..can u please look into the code .. i can send the file... ??

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    Post the code here.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT code

    this is the code i wrote... im havin a problem. please help me figure it out..
    i used eclipse.. and these are three different classes... ChatServer, ChatClient and NewFrame
    =Norm;150947]
     
    //ChatServer:
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
     
     
    public class ChatServer implements Runnable 
    {
     
        ServerSocket chatSer;
        Socket chatServer;
     
        BufferedReader br=null;
        BufferedWriter bw=null;
     
        NewFrame chatServerObj;
     
        public void startChatServer( String d1) throws Exception
        {
            try
            {
               //       chatServerObj=obj1;
            	      System.out.println("Server in accept Mode" + d1 );
            	      Integer port1=Integer.parseInt(d1);
                      chatSer=new ServerSocket(port1);  
                      chatServer = chatSer.accept();     
                      System.out.println("Server Connected to Client");
                      br=new BufferedReader(new InputStreamReader(chatServer.getInputStream()));
                      bw=new BufferedWriter(new OutputStreamWriter(chatServer.getOutputStream()));
     
                      bw.write("Hello...welcome to Chat..Lets Play");
                      bw.newLine();
                      bw.flush();
     
                      Thread th=new Thread(this);
                      th.start();
     
                     //System.out.println("Thred for client is created.. for read..");
                     //System.out.println("\n\nClient is connected..: IP : ");
               }     
               catch(Exception e)
               {
                     //str="Network Connection Problem..";
                     //JOptionPane.showConfirmDialog(F1, str + "  " + e.getMessage());
               }
            } 
     
            public void sendData(String data) throws Exception
            {
                try
                {
                	System.out.print(" ACCEPT Data "+ data);
                     bw.write(data);
                     bw.newLine();
                     bw.flush();
     
                }
                catch(Exception e)
                {
                    System.out.print("Error Data "+ e.getMessage());
                }
            }
     
     
     
        //thread for reading the data...!!    
        public void run()
        {
     
            String data;
            try
            {
                while(true)
                {
     
                      data=br.readLine();
                      System.out.println("Server side Data : "+data);
                      handleDataRead(data);
                }     
            }       
     
     
     
     
            catch (Exception e)
            {
                System.out.print("In read Server.."+e.getMessage());
            }
        }		
     
        public void handleDataRead(String data) throws Exception
        {
            try
            {
            	System.out.println("In handle data read - Server:"+data);
                chatServerObj.chatMessage_1(data);
     
            }
            catch(Exception e)
            {}    
        }
     
        public void dataPass(String data) throws Exception
        {
            try
            {
                //will be called whenever the data need to be passed between cleint and server...!! 
                bw.write(data);
                bw.newLine();
                bw.flush();
     
            }
            catch(Exception e)
            {
     
            }    
        }    
    }
     
     
    //ChatClient:
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    //package justchat;
     
    import java.awt.Color;
    import java.util.Random;
    import java.util.Collections;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
     
    import java.io.*;
    import java.net.*;
    import java.net.Socket.*;
    public class ChatClient implements Runnable 
    {
     
     
        Socket chatClient;
     
        BufferedReader br=null;
        BufferedWriter bw=null;
     
        NewFrame chatClientObj;
     
        public void startConnectToServer(String d1) throws Exception
        {
            try
            {
                        //chatClientObj=obj1;
            			Integer port1=Integer.parseInt(d1);
                      chatClient=new Socket("localhost",port1);  
     
     
                      br=new BufferedReader(new InputStreamReader(chatClient.getInputStream()));
                      bw=new BufferedWriter(new OutputStreamWriter(chatClient.getOutputStream()));
     
                      bw.write("Hello...welcome to Bingo V1.0..Lets Play");
                      bw.newLine();
                      bw.flush();
     
                      Thread th=new Thread(this);
                      th.start();
     
                     //System.out.println("Thred for client is created.. for read..");
                     //System.out.println("\n\nClient is connected..: IP : ");
               }     
               catch(Exception e)
               {
                     //str="Network Connection Problem..";
                     //JOptionPane.showConfirmDialog(F1, str + "  " + e.getMessage());
               }
            } 
     
            public void sendData(String data) throws Exception
            {
                try
                {
                     bw.write(data);
                     bw.newLine();
                     bw.flush();
     
                }
                catch(Exception e)
                {
                    //System.out.print("Error Data "+ e.getMessage());
                }
            }
     
     
     
        //thread for reading the data...!!    
        public void run()
        {
     
            String data;
            try
            {
                while(true)
                {
     
                      data=br.readLine();
                      System.out.println("Client Side data : "+data);
                      handleDataRead(data);
                }     
            }       
            catch (Exception e)
            {
                System.out.print("In chat Server.."+e.getMessage());
            }
        }		
     
        public void handleDataRead(String data) throws Exception
        {
            try
            {
            	System.out.println("In handle data read - Client :"+data);
            	String dataCopy=data;
            	System.out.println("Data Copy - Client :"+dataCopy);
     
            	//dataPass(dataCopy);
                chatClientObj.chatMessage_1(data);
     
            }
            catch(Exception e)
            {
            	//System.out.println(e.pr());
            }    
        }
     
        public void dataPass(String data) throws Exception
        {
            try
            {
     
            	System.out.println("Data Pass - Client :"+data);
     
                //will be called whenever the data need to be passed between cleint and server...!! 
                //bw.write(data);
                //bw.newLine();
                //bw.flush();
     
            }
            catch(Exception e)
            {
     
            }    
        }
     
    }
     
     
    //GUI(CHAT DESIGN) class name: NewFrame
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
    import javax.swing.border.Border;
     
    //import GUIchat.ActionHandler;
     
    public class NewFrame extends JFrame  {
     
    	/**
    	 * 
    	 */
     
    	private static final long serialVersionUID = 1L;
     
    	JButton	but_start,but_send, but_connect;
    	JLabel lab_port;
        JTextField txt_port;
        JTextArea txt_chat,txt_send;
     
        //client server objects..
       // ChatServer serverobj=new ChatServer();
       // ChatClient clientObj1=new ChatClient();
     
     
     
     
        int flagWho;
     
       public void chatMessage_1(String data)throws Exception
    	{
    		System.out.print("in Chat Message..."+ data);
    		try
    		{
    			System.out.println("in Chat Message..."+ data);
    			txt_send.getText();
     
    		}
    		catch(Exception e)
    		{}
    	}
     
     
    	NewFrame()
    	{
    		ActionListener actionHandler = new ActionHandler();
    		setTitle("CHAT WINDOW");
    		JPanel panel = new JPanel();
            getContentPane().add(panel);
     
            panel.setLayout(null);
            panel.setToolTipText("A Panel container");
     
            lab_port = new JLabel("port NO:");
            lab_port.setBounds(10, 10, 90, 30);
     
            but_connect = new JButton("Connect Server");
            but_connect.setBounds(170, 10, 150, 30);
            but_connect.addActionListener(actionHandler);
     
     
            but_start = new JButton("Start Server");
            but_start.setBounds(320, 10, 150, 30);
            but_start.addActionListener(actionHandler);
           // btn1.setToolTipText("Client port number");
     
            txt_chat=new JTextArea();
            Border border = BorderFactory.createLineBorder(Color.BLACK);
            txt_chat.setBounds(10,50,400,300);
            txt_chat.setBorder(border);
     
            txt_send=new JTextArea();
            Border border1 = BorderFactory.createLineBorder(Color.DARK_GRAY);
            txt_send.setBounds(10,380,400,60);
            txt_send.setBorder(border1);
     
            but_send = new JButton("  SEND  ");
            but_send.setBounds(430,380,90,60);
            but_send.addActionListener(actionHandler);
     
            txt_port=new JTextField();
            txt_port.setBounds(65,10,70,30);
     
            panel.add(txt_send);
            panel.add(txt_chat);
            panel.add(txt_port);
            panel.add(but_connect);
            panel.add(but_start);
            panel.add(but_send);
            panel.add(lab_port);
     
    		setSize(700,500);
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);	
     
    	}
     
     
     
    	private class ActionHandler implements ActionListener
    	{
    	      public void actionPerformed(ActionEvent evt) 
    	      {
    	         Object source = evt.getSource();
    	         ChatServer serverobj=new ChatServer();
    	         ChatClient clientObj1=new ChatClient();
    	         //server Code
    	         if (source == but_start)
    	         {
    	        	String d1=txt_port.getText();
    	        	System.out.println(txt_port.getText());
    	        	flagWho=1;
     
    	            try 
    	            {
    					serverobj.startChatServer(d1);
     
     
    	            } catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    	         }      
    	         else
     
    	        	 //client connecting to server
    	        	 if (source == but_connect)
    		         {
     
    		        	System.out.println("Connecting in Progress");
     
    		            try 
    		            {
    		            	flagWho=0;
    		            	String d1=txt_port.getText();
    		            	clientObj1.startConnectToServer(d1);
     
     
    		            } catch (Exception e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    		         } 
     
     
    	         //send data
    	         if (source == but_send)
    	         {
     
    	        	System.out.println("Send Data...");
    	            //ChatClient clientObj1=new ChatClient();
    	            try 
    	            {
    	            	if(flagWho==1)
    	            	{
     
    	            	 //server send data
    	            		serverobj.sendData(txt_send.getText().toString());	
    	            	}
    	            	else
    	            	{
    	            		//client send data..
    	            		clientObj1.sendData(txt_send.getText().toString());
    	            	}
     
     
     
     
    	            } catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    	         } 
     
     
     
     
    	      }
    	}   
     
    	//Method for Display the chat Data..
     
     
     
    	public static void main(String[]args)
    	{
    		SwingUtilities.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				NewFrame c1=new NewFrame();
    				c1.setVisible(true);
    			}
    		});
    	}	
     
    }


    --- Update ---

    you can specify any number for port when running the server appilcation.

    --- Update ---

    please give the port number and click start server . open another chat screen.. which is the client and give the same port number and click connect server.
    Last edited by Norm; June 25th, 2014 at 11:45 AM. Reason: changed quote to code tags

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    How do you test the code? I get a window with input areas and buttons. I entered a port and pressed Start Server. The program then hung and I had to cancel it.

    open another chat screen..
    Where is the chat screen? There is only one class with a main() method.

    NOTE: I see a lot of catch blocks that do not have calls to the printStackTrace() method. It should be called in all the catch blocks.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    hmmm.. ok give the port number and click start server.. once you do. u can see a msg in the eclipse output screen. and then u open another chat screen and write the same port number and say connect server..u will again see an output in the screen. now in the server chat screen type a word in the chat area and click send. the problem is the word is not appearing on the client chat screen but it appears on the eclipse ouput screen.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    You speak of a chat screen and a server chat screen. Where are they? There is only one program with a main() method.


    A problem with the code: it is not responsive when started. The Close window X does not work.


    The "Send" button should not be enabled until the server/client is started.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    yes it is one main method.. please run the application twice. now you ll have two chat screen..?? the first time you run the app tht is the server and the second time to run the app again tht is the client.. and yes the X buttn doesnt close unless u open two chat screens. now specify the port number in the server and click start server. simultaneously in the other screen type the same port number and click connect server. let me know what happens

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    One problem I see is that objects are being created every time the actionPerformed() method is called.
    Normally only one object is created and it's reused, instead of a new one being created at every action event.

    --- Update ---

    Here is the console:
    Running: java -client -cp . CS_NewFrame

    Connecting in Progress
    Server Connected to Client
    Client Side data : Hello...welcome to Chat..Lets PlayS waiting for data
    Server side Data : Hello...welcome to Bingo V1.0..Lets Play
    In handle data read - Server:Hello...welcome to Bingo V1.0..Lets Play
    S waiting for data

    In handle data read - Client :Hello...welcome to Chat..Lets Play
    Data Copy - Client :Hello...welcome to Chat..Lets Play
    Send Data... flagWho=0
    C sendData datat=dddd

    In handle data read - Server:dddd
    S waiting for data
    Server side Data : dddd
    0 error(s)
    In read Server..Connection reset
    1 error(s)


    Where does the code show what the client read?

    The use of print() instead of println() runs many of the messages together on one line making it harder to read the debug
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    yeah tht seems to be the problem...in the action performed function...
    //send data
    if (source == but_send)
    {

    System.out.println("Send Data...");
    //ChatClient clientObj1=new ChatClient();
    try
    {
    if(flagWho==1)
    {

    //server send data
    serverobj.sendData(txt_send.getText().toString());
    }
    else
    {
    //client send data..
    clientObj1.sendData(txt_send.getText().toString()) ;
    }

    this code is not executing.. the connection is fine..but the data is not being passed from the server to client or client to server..which im not able to understand why? could u help me fix the code.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    Please wrap the code in code tags.

    Make sure ALL catch blocks have a call to the printStackTrace() method.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    yes i did tht.. im still not able to send the message from the server to the client in the chat screen. the message is in the string "data" the function chatmessage_1 is suppose to act like the mediate between the server and the client... which carries the "data"

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,442
    Thanks
    49
    Thanked 2,218 Times in 2,191 Posts

    Default Re: CHAT PROJECT

    yes i did tht..
    Please post the new code with all the changes.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Jun 2014
    Posts
    11
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: CHAT PROJECT

    ok i will..ill do it by tomoro..its midnight here and got to get some sleep. thank you

Similar Threads

  1. Replies: 1
    Last Post: April 6th, 2014, 05:16 PM
  2. Java School Project Help; Project #2, In Danger of Failing.....
    By john++ in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 02:35 AM
  3. chat box
    By abhiv44 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 2nd, 2013, 08:52 AM
  4. Chat using TCP/IP, chat text,Voice,Icon,send file ....
    By hien281191 in forum Totally Off Topic
    Replies: 2
    Last Post: September 13th, 2013, 02:56 PM
  5. chat system
    By Dikshanta in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 21st, 2013, 10:42 AM