Results 1 to 14 of 14
  1. #1
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default Instant Messaging Program

    I want to try to make an instant messaging program. I'm having problems with it, and was wondering if someone could help me.

    View class:

    Java Code:
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    
    public class View extends JFrame implements ActionListener
    {
    	Client myClient;
    	Server myServer;
    	private String myMessage;
    	private JTextField myMessageTextField;
    	private JTextField mySendMessageTextField;
    	private JButton myEnterButton;
    	private boolean isClient;
    	
    	public View(boolean isClient)
    	{
    		this.isClient = isClient;
    		if(isClient)
    		{
    			myClient = new Client(this);
    		}
    		else
    		{
    			myServer = new Server(this);
    		}
    		
    		Container contentPane = this.getContentPane();
    		this.setTitle("Instant Messaging");
    		this.setLayout(new GridLayout(3, 1));
    		this.setSize(300, 300);
    		this.setLocationRelativeTo(null);
    		
    		myMessageTextField = new JTextField();
    		myMessageTextField.setEditable(false);
    		contentPane.add(myMessageTextField);
    		
    		mySendMessageTextField = new JTextField();
    		contentPane.add(mySendMessageTextField);
    		
    		myEnterButton = new JButton("Send");
    		myEnterButton.addActionListener(this);
    		contentPane.add(myEnterButton);
    		
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setVisible(true);
    	}
    
    	public String getText() 
    	{
    		return mySendMessageTextField.getText();
    	}
    
    	public void setMessage(String fromServer) 
    	{
    		myMessage += "\n" + fromServer;
    		myMessageTextField.setText(myMessage);
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) 
    	{
    		if(e.getSource().equals(myEnterButton));
    		{
    			if(isClient)
    			{
    				myClient.setText(
    						this.mySendMessageTextField.getSelectedText());
    			}
    			else
    			{
    				myServer.setText(
    						this.mySendMessageTextField.getSelectedText());
    			}
    		}
    	}
    	
    	
    }
    Client class:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    
    public class Client implements Runnable
    {
    
    	Socket myClientSocket;
        PrintWriter out;
        BufferedReader in;
        String fromServer;
        String fromUser;
        View myView;
        Thread myThread;
        
        public Client(View aView)
        {
        	try 
        	{
                myClientSocket = new Socket("JordanS", 4444);
                out = new PrintWriter(myClientSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(myClientSocket.getInputStream()));
            } catch (UnknownHostException e) {
                System.err.println("Don't know about host: taranis.");
                System.exit(1);
            } catch (IOException e) {
                System.err.println("Couldn't get I/O for the connection to: taranis.");
                System.exit(1);
            }
            
            myView = aView;
            this.start();
        }
        
        public void start() 
    	{
    		myThread = new Thread(this);
    		myThread.start();
    	}
    	
    	public void stop()
    	{
    		myThread = null;
    	}
    
    	public static void main(String[] args) 
    	{
    		View aView = new View(true);
    
    	}
    
    	@Override
    	public void run() 
    	{
    		while(true)
    		{
    			try 
    			{
    				if((fromServer = in.readLine()) != null) 
    				{
    				    myView.setMessage(fromServer);
    				}
    				    
    				String sendText = this.getText();
    				if (sendText != ""); 
    				{
    					out.println(fromUser);
    					fromUser = "";
    				}
    			} 
    			catch (IOException e) 
    			{
    				this.closeConnection();
    			}
    		}	
    	}
    	
    	private String getText() 
    	{
    		return fromUser;
    	}
    	
    	public String setText(String aMessage) 
    	{
    		return fromUser;
    	}
    
    	private void closeConnection()
    	{
    		try 
    		{
    			out.close();
    			in.close();
    			myClientSocket.close();
    			System.exit(0);
    		}
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Server Class:

    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    
    public class Server implements Runnable
    {
    
    	ServerSocket myServerSocket;
        PrintWriter out;
        BufferedReader in;
        String fromServer;
        String fromUser;
        View myView;
        Socket clientSocket;
        
        Thread myThread;
        
        public Server(View aView)
        {
        	ServerSocket serverSocket = null;
            try {
                serverSocket = new ServerSocket(4444);
            } catch (IOException e) {
                System.err.println("Could not listen on port: 4444.");
                System.exit(1);
            }
    
            clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.exit(1);
            }
            
            try {
    			out = new PrintWriter(clientSocket.getOutputStream(), true);
    			in = new BufferedReader(new InputStreamReader(
    					clientSocket.getInputStream()));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            myView = aView;
            this.start();
        }
    
    	public void start() 
    	{
    		myThread = new Thread(this);
    		myThread.start();
    	}
    	
    	public void stop()
    	{
    		myThread = null;
    	}
    
    	public static void main(String[] args) 
    	{
    		View aView = new View(false);
    
    	}
    
    	@Override
    	public void run() 
    	{
    		while(true)
    		{
    			try 
    			{
    				if((fromServer = in.readLine()) != null) 
    				{
    				    myView.setMessage(fromServer);
    				}
    				    
    				String sendText = this.getText();
    				if (sendText != ""); 
    				{
    					out.println(fromUser);
    					fromUser = "";
    				}
    			} 
    			catch (IOException e) 
    			{
    				System.exit(0);
    			}
    			try
    			{
    				Thread.sleep(0);
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}	
    	}
    	
    	private String getText() 
    	{
    		return fromUser;
    	}
    	
    	public String setText(String aMessage) 
    	{
    		return fromUser;
    	}
    
    	private void closeConnection()
    	{
    		try 
    		{
    			out.close();
    	        in.close();
    	        clientSocket.close();
    	        myServerSocket.close();
    		}
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    }
    Last edited by Zosden; 04-28-2008 at 07:25 AM.
    My IP address is 127.0.0.1

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    No I don't get any errors I just can't seem to get the program to read in the string from the other user. I'm new to networking so it could just be something dumb. In the end I would like to have this program set up then implement it into my tic tac toe project for my website.
    My IP address is 127.0.0.1

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    ah, ok pal. Why I ask this is, have to test a lot. Just looking to give a hint without compiling and run. Hang-on, i'll try this.

  5. #5
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    are you going to post a suggestion or not?
    My IP address is 127.0.0.1

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    How did you handle the client request in the server, wise versa? Did you use any http response codes there? Seems to me it's not. Why I say is, if you use them easily you can send messages through the response data part.

    By the way this is not a good choice pal, found it at the Client.

    Java Code:
    if (sendText != "");
    In your application it's fine. But it is not legal. Legal not says that it is incorrect. ;) And also you have empty body in the if loop. You have added ; at the end.

    Do it as follows.

    Java Code:
    if (!sendText.isEmpty())

  7. #7
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I think the problem is that it just leaves the while loop for some reason I did a S.O.P within it and it just printed it out once.
    My IP address is 127.0.0.1

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  9. #9
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    the while true loop in the run method. I work with threads a lot and i have no idea whats the matter. I think it has to do with the sockets
    My IP address is 127.0.0.1

  10. #10
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    What do you mean killtest
    My IP address is 127.0.0.1

  11. #11
    JavaHead08 is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Zosden View Post
    What do you mean killtest
    LOL, he's spamming the forum. Ignore him.

  12. #12
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    I still don't know whats wrong with my program. If someone has any ideas or knows how to do it better let me know
    My IP address is 127.0.0.1

  13. #13
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Just a few suggestions about the design.

    1. You don't need the server to know about the view.
    2. On the server keep a list of sessions. When a client connects for the first time it sends a session containing session ID 0. Then the server sends back a new allocated session ID.
    3. A session is a class containing a few facts like the client name, session ID and last time of access (you want to kill an inactive session after a while)
    4. Every message from the client to the servers always includes the session ID. For this you might implement a Message class.
    5. The message can contain: requests for session (maybe with authentication), commands (list users for example) and messages for other users.
    5. Messages from a client to other clients are distributed back by the server.
    6. Messages are handled by threads created for that purpose or reused from a pool.
    7. The GUI (View) uses the client but the client doesn't need the GUI to run.

    Identified actors: Server, Client, Session, Message
    Inside the server you have an "Incoming Message Dispatcher" and an "Outgoing Message Dispatcher". they get the incoming messages, distribute them to working threads and then distribute back to Clients the answers.

    To keep it simple you can use UDP in the beginning.

    Have fun :) You might attack Yahoo!'s market share with this :)
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  14. #14
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    Lol thanks daniel I was wondering if you could either explain it in either a lot more detail or preferably send me to a guide. Thanks again.
    My IP address is 127.0.0.1

Similar Threads

  1. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  2. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  3. broadcast messaging
    By bhanu in forum Networking
    Replies: 1
    Last Post: 10-30-2007, 12:41 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •