Thread: *FREE* Instant Messaging Program

Results 1 to 4 of 4
  1. #1 *FREE* Instant Messaging Program 
    I'm the best in the world.
    Vesgar's Avatar
    Join Date
    Nov 2014
    Age
    27
    Posts
    284
    Thanks given
    78
    Thanks received
    52
    Rep Power
    59
    I know this is the "Selling" section, but they don't have a "free services" section for what I'm about to give out (and if there is, I'd like a moderator to move this to the proper section and I apologize in advance).

    So, what is the instant messaging program all about? Simple. It works just the same as if you were messaging someone through Skype, Facebook, or even Twitter. Please note, not all of this is my work, and I left the credit at the bottom of this section.

    Code:
    //this is for java file: 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 Side");
    		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 recieve 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 don't know that object type.");
    			}
    		}while(!message.equals("SERVER - END"));
    	}
    	
    	//close the streams and sockets
    	private void closeCrap(){
    		showMessage("\n Closing 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 into the text box
    	private void ableToType(final boolean tof){
    			SwingUtilities.invokeLater(
    					new Runnable(){
    						public void run(){
    							userText.setEditable(tof);
    						}
    					}
    			);
    	}
    }
    Code:
    //this is for Java file: 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("Instant 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! ");
    	}
    	
    	//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 don't 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 CAN'T 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 into their box
    	private void ableToType(final boolean tof){
    		SwingUtilities.invokeLater(
    				new Runnable(){
    					public void run(){
    						userText.setEditable(tof);
    					}
    				}
    			);
    	}
    	
    }
    Code:
    //this is for java file: ServerTest.java
    import javax.swing.JFrame;
    
    public class ServerTest {
    	public static void main(String[] args) {
    		Server sally = new Server();
    		sally.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		sally.startRunning();
    	}
    
    }
    Code:
    //this is for java file: ClientTest.java
    import javax.swing.JFrame;
    
    public class ClientTest {
    	public static void main(String[] args){
    		Client charlie;
    		charlie = new Client("127.0.0.1");
    		charlie.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		charlie.startRunning();
    	}
    }


    Please note: Eclipse is recommended, and you need to create two project files - one for Client and one for Server. Please remember the CAPS is important if you're going to copy this step by step.

    *This works best if you have a VPS if you're going to use this for a RuneScape Private Server*

    Works with:
    • Desktop Communication
    • HTML/PHP Communication
    • RuneScape Private Servers


    If you know a lot about Java, then you should have no concerns about this whatsoever, but if you happen to then you can just let me know.

    If you have any questions, comments, or concerns about this, please let me know. If you need further explanation, please feel free to comment below.

    Credit
    • thenewboston
    • Ascaria
    Reply With Quote  
     

  2. #2  
    Celestial - Founder
    Classic's Avatar
    Join Date
    Nov 2014
    Posts
    233
    Thanks given
    119
    Thanks received
    47
    Rep Power
    39
    Images please.
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    I'm the best in the world.
    Vesgar's Avatar
    Join Date
    Nov 2014
    Age
    27
    Posts
    284
    Thanks given
    78
    Thanks received
    52
    Rep Power
    59
    Quote Originally Posted by Limbo_ View Post
    Images please.
    Added a photo of what it looks like.

    Forgot to add it before, my apologies.
    Reply With Quote  
     

  5. #4  
    Registered Member
    Join Date
    Aug 2014
    Posts
    349
    Thanks given
    58
    Thanks received
    62
    Rep Power
    30
    Which part of this is yours?
    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. FREE Graphic Design Programs for Beginners.
    By Quezts in forum Resources
    Replies: 5
    Last Post: 07-30-2014, 05:22 AM
  2. Replies: 6
    Last Post: 03-25-2011, 11:27 PM
  3. Instant Messaging Freezers/MassAdds.
    By Aj in forum Announcements
    Replies: 75
    Last Post: 10-14-2009, 08:03 PM
  4. Replies: 7
    Last Post: 05-27-2009, 12:54 PM
  5. Send a message w/ a VB.NET or VB Program?
    By Anthony-| in forum Help
    Replies: 0
    Last Post: 03-09-2009, 06:53 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
  •