├── README.md ├── src └── Serverdemo │ ├── Clienttest.java │ ├── servertest.java │ ├── Client.java │ └── Server.java └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # Instant-Messenger 2 | An command chat application using Java 3 | 4 | 5 | 6 | I recommend to use an eclipse Indigo or higher version for better results 7 | 8 | 9 | 10 | help me get more stars .. 11 | -------------------------------------------------------------------------------- /src/Serverdemo/Clienttest.java: -------------------------------------------------------------------------------- 1 | package Serverdemo; 2 | import javax.swing.*; 3 | 4 | public class Clienttest { 5 | public static void main(String[] args) { 6 | Client friday= new Client("127.0.0.1"); 7 | friday.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 8 | friday.startRunning(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/Serverdemo/servertest.java: -------------------------------------------------------------------------------- 1 | package Serverdemo; 2 | 3 | import javax.swing.JFrame; 4 | 5 | public class servertest { 6 | public static void main(String[] args) { 7 | Server Jarvis=new Server(); 8 | Jarvis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 9 | Jarvis.startRunning(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build and Release Folders 2 | bin-debug/ 3 | bin-release/ 4 | [Oo]bj/ 5 | [Bb]in/ 6 | 7 | # Other files and folders 8 | .settings/ 9 | 10 | # Executables 11 | *.swf 12 | *.air 13 | *.ipa 14 | *.apk 15 | 16 | # Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` 17 | # should NOT be excluded as they contain compiler settings and other important 18 | # information for Eclipse / Flash Builder. 19 | -------------------------------------------------------------------------------- /src/Serverdemo/Client.java: -------------------------------------------------------------------------------- 1 | package Serverdemo; 2 | import java.io.*; 3 | import java.net.*; 4 | import java.awt.*; 5 | import java.awt.event.*; 6 | import javax.swing.*; 7 | 8 | public class Client extends JFrame{ 9 | private JTextArea chatwindow; 10 | private JTextField usertext; 11 | private ObjectOutputStream output; 12 | private ObjectInputStream input; 13 | private String message=""; 14 | private String serverIP; 15 | private Socket connection; 16 | 17 | //cotr 18 | public Client(String host) 19 | { 20 | super("Client"); 21 | serverIP=host; 22 | usertext = new JTextField(); 23 | usertext.setEditable(false); 24 | usertext.addActionListener( 25 | new ActionListener() { 26 | 27 | @Override 28 | public void actionPerformed(ActionEvent e) { 29 | sendMessage(e.getActionCommand()); 30 | usertext.setText(""); 31 | 32 | } 33 | } 34 | 35 | 36 | ); 37 | add(usertext,BorderLayout.NORTH); 38 | chatwindow = new JTextArea(); 39 | add(new JScrollPane(chatwindow),BorderLayout.CENTER); 40 | setSize(500,300); 41 | setLocation(600,300); 42 | setVisible(true); 43 | } 44 | 45 | //connect to server 46 | public void startRunning() { 47 | try { 48 | connectToServer(); 49 | setupStreams(); 50 | whileChatting(); 51 | 52 | }catch(EOFException eofexception) { 53 | showMessage("\nClient terminated connection"); 54 | } 55 | catch(IOException ioe) {ioe.printStackTrace();} 56 | finally { 57 | closeCrap(); 58 | } 59 | } 60 | 61 | //close the streams and sockets 62 | 63 | private void closeCrap() { 64 | showMessage("\nClosing connections ... "); 65 | abletoType(false); 66 | try{ 67 | output.close(); 68 | input.close(); 69 | connection.close(); 70 | } 71 | catch(IOException e) {e.printStackTrace();} 72 | } 73 | 74 | //connect to server 75 | private void connectToServer() throws IOException{ 76 | showMessage("\n\nATTEmmpting connections....."); 77 | connection=new Socket(InetAddress.getByName(serverIP),6789); 78 | showMessage("COnnected to : "+connection.getInetAddress().getHostName()); 79 | } 80 | //setup the streams send nad receive message 81 | private void setupStreams() throws IOException { 82 | output= new ObjectOutputStream(connection.getOutputStream()); 83 | output.flush(); 84 | input= new ObjectInputStream(connection.getInputStream()); 85 | showMessage("\n\ndude your streams are good to go..."); 86 | } 87 | //while chhating with server 88 | private void whileChatting() throws IOException { 89 | abletoType(true); 90 | do { 91 | try { 92 | message=(String) input.readObject(); 93 | showMessage("\n"+message); 94 | } 95 | catch(ClassNotFoundException c) {showMessage("\nidk wtf that user send!");} 96 | //have a conversation 97 | }while(!message.equals("SERVER-END")); 98 | 99 | } 100 | private void sendMessage(String message) 101 | { 102 | try { 103 | output.writeObject("\nCLIENT-"+message); 104 | output.flush(); 105 | showMessage("\nCLIENT-"+message); 106 | }catch(IOException ioexception) {chatwindow.append("\n error: dude i cant send that message");} 107 | 108 | } 109 | //updatess chatwindow 110 | private void showMessage(final String s) { 111 | SwingUtilities.invokeLater( 112 | new Runnable() { 113 | 114 | @Override 115 | public void run() { 116 | chatwindow.append(s); 117 | } 118 | } 119 | ); 120 | 121 | } 122 | 123 | // let the user type into chat 124 | private void abletoType(final boolean tof) { 125 | SwingUtilities.invokeLater( 126 | new Runnable() { 127 | 128 | @Override 129 | public void run() { 130 | usertext.setEditable(tof); 131 | } 132 | } 133 | ); 134 | } 135 | 136 | } 137 | -------------------------------------------------------------------------------- /src/Serverdemo/Server.java: -------------------------------------------------------------------------------- 1 | package Serverdemo; 2 | import java.io.*; 3 | import java.net.*; 4 | import java.awt.*; 5 | import java.awt.event.*; 6 | import javax.swing.*; 7 | 8 | 9 | public class Server extends JFrame { 10 | 11 | private JTextField userText; 12 | private JTextArea chatWindow; 13 | private ObjectOutputStream output; 14 | private ObjectInputStream input; 15 | private ServerSocket server; 16 | private Socket connection; 17 | 18 | public Server() { 19 | super("ashlesh's iNstant messenger"); 20 | userText = new JTextField(); 21 | userText.setEditable(false); 22 | userText.addActionListener( 23 | new ActionListener() { 24 | 25 | @Override 26 | public void actionPerformed(ActionEvent event) { 27 | sendMessage(event.getActionCommand()); 28 | userText.setText(""); 29 | 30 | } 31 | } 32 | ); 33 | add(userText,BorderLayout.NORTH); 34 | chatWindow = new JTextArea(); 35 | add(new JScrollPane(chatWindow)); 36 | setSize(500,300); 37 | setLocation(600,300); 38 | setVisible(true); 39 | 40 | } 41 | //set up and run he server 42 | public void startRunning() 43 | { 44 | try { 45 | server =new ServerSocket(6789,100); 46 | while(true) 47 | { 48 | try { 49 | waitForConnection(); 50 | setupStreams(); 51 | whileChatting(); 52 | 53 | } 54 | catch(EOFException eofexception) { 55 | showMessage("\nserver ended the connection!"); 56 | } 57 | finally { 58 | closeCrap(); 59 | } 60 | } 61 | } 62 | catch(IOException e) {e.printStackTrace();} 63 | } 64 | 65 | 66 | 67 | //wait for connection then display connection info 68 | private void waitForConnection() throws IOException{ 69 | showMessage("\nwaiting for someone to connect..."); 70 | connection = server.accept(); 71 | showMessage("\nNow connected to : " +connection.getInetAddress().getHostName()); 72 | 73 | 74 | } 75 | //get stream to end and recieve data 76 | private void setupStreams() throws IOException { 77 | output=new ObjectOutputStream(connection.getOutputStream()); 78 | output.flush(); 79 | input =new ObjectInputStream(connection.getInputStream()); 80 | showMessage("\nStreams are now setup!!"); 81 | 82 | } 83 | 84 | //during the coversation 85 | private void whileChatting() throws IOException{ 86 | String message ="you are now connnected"; 87 | sendMessage(message); 88 | abletoType(true); 89 | do { 90 | try { 91 | message=(String) input.readObject(); 92 | showMessage("\n"+message); 93 | } 94 | catch(ClassNotFoundException c) {showMessage("\nidk wtf that user send!");} 95 | //have a conversation 96 | }while(!message.equals("CLIENT-END")); 97 | } 98 | 99 | //close streams and sockets after done chatting 100 | private void closeCrap() 101 | { 102 | showMessage("\nClosing connections ... "); 103 | abletoType(false); 104 | try{ 105 | output.close(); 106 | input.close(); 107 | connection.close(); 108 | } 109 | catch(IOException e) {e.printStackTrace();} 110 | } 111 | //send a message to client 112 | private void sendMessage(String message) 113 | { 114 | try { 115 | output.writeObject("\nSERVER-"+message); 116 | output.flush(); 117 | showMessage("\nSERVER-"+message); 118 | }catch(IOException ioexception) {chatWindow.append("\n error: dude i cant send that message");} 119 | 120 | } 121 | //updatess chatwindow 122 | private void showMessage(final String s) { 123 | SwingUtilities.invokeLater( 124 | new Runnable() { 125 | 126 | @Override 127 | public void run() { 128 | chatWindow.append(s); 129 | } 130 | } 131 | ); 132 | 133 | } 134 | 135 | // let the user type into chat 136 | private void abletoType(final boolean tof) { 137 | SwingUtilities.invokeLater( 138 | new Runnable() { 139 | 140 | @Override 141 | public void run() { 142 | userText.setEditable(tof); 143 | } 144 | } 145 | ); 146 | } 147 | } 148 | --------------------------------------------------------------------------------