├── Client.java ├── ClientTest.java ├── README.md ├── Server.java └── ServerTest.java /Client.java: -------------------------------------------------------------------------------- 1 | package server_client; 2 | 3 | import java.io.*; 4 | import java.lang.*; 5 | import java.net.*; 6 | import java.awt.BorderLayout; 7 | import java.awt.event.*; 8 | import javax.swing.*; 9 | 10 | public class Client extends JFrame 11 | { 12 | private JTextField usertext; 13 | private JTextArea chatwindow; 14 | private ObjectOutputStream output; 15 | private ObjectInputStream input; 16 | private Socket connection; 17 | private String serverIP; 18 | 19 | public Client(String host) 20 | { 21 | super("CLIENT"); 22 | serverIP = host; 23 | usertext = new JTextField(); 24 | usertext.setEditable(false); 25 | usertext.addActionListener( 26 | new ActionListener() 27 | { 28 | public void actionPerformed(ActionEvent event) 29 | { 30 | sendmessage(event.getActionCommand()); 31 | usertext.setText(""); 32 | } 33 | }); 34 | add(usertext,BorderLayout.SOUTH); 35 | chatwindow = new JTextArea(); 36 | chatwindow.setEditable(false); 37 | add(new JScrollPane(chatwindow),BorderLayout.CENTER); 38 | setSize(1280,720); 39 | setVisible(true); 40 | } 41 | public void startRunning() throws IOException 42 | { 43 | try 44 | { 45 | connection(); 46 | setup(); 47 | chatting(); 48 | } 49 | catch(EOFException e) 50 | { 51 | showmessage("\n Client ended the connection!"); 52 | } 53 | catch(IOException e) 54 | { 55 | e.printStackTrace(); 56 | } 57 | finally 58 | { 59 | closeall(); 60 | } 61 | } 62 | private void connection() throws IOException 63 | { 64 | showmessage("COnnecting..\n"); 65 | connection = new Socket(InetAddress.getByName(serverIP),3000); 66 | showmessage("Connected to "+connection.getInetAddress().getHostName()+ "\n"); 67 | } 68 | private void setup() throws IOException 69 | { 70 | output = new ObjectOutputStream(connection.getOutputStream()); 71 | output.flush(); 72 | input = new ObjectInputStream(connection.getInputStream()); 73 | } 74 | private void chatting() throws IOException 75 | { 76 | String message = "Connected!"; 77 | sendmessage(message); 78 | canType(true); 79 | do 80 | { 81 | try 82 | { 83 | message = (String) input.readObject(); 84 | showmessage(message+"\n"); 85 | } 86 | catch(ClassNotFoundException e) 87 | { 88 | showmessage("commmand not recognized\n"); 89 | } 90 | } 91 | while(!message.equals("Client - End")); 92 | } 93 | private void closeall() throws IOException 94 | { 95 | showmessage("Closing connection...\n"); 96 | canType(false); 97 | try 98 | { 99 | output.close(); 100 | input.close(); 101 | connection.close(); 102 | } 103 | catch(IOException e) 104 | { 105 | e.printStackTrace(); 106 | } 107 | } 108 | private void sendmessage(String message) 109 | { 110 | try 111 | { 112 | output.writeObject("CLIENT "+message); 113 | output.flush(); 114 | showmessage("CLINET "+message+"\n"); 115 | } 116 | catch(IOException e) 117 | { 118 | chatwindow.append("Error - message cannot be sent"); 119 | } 120 | } 121 | private void showmessage(final String text) 122 | { 123 | SwingUtilities.invokeLater( 124 | new Runnable() 125 | { 126 | public void run() 127 | { 128 | chatwindow.append(text); 129 | } 130 | }); 131 | } 132 | private void canType(final boolean tof) 133 | { 134 | SwingUtilities.invokeLater( 135 | new Runnable() 136 | { 137 | public void run() 138 | { 139 | usertext.setEditable(tof); 140 | } 141 | }); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /ClientTest.java: -------------------------------------------------------------------------------- 1 | package server_client; 2 | import java.io.IOException; 3 | 4 | import javax.swing.JFrame; 5 | public class ClientTest 6 | { 7 | 8 | public static void main(String[] args) throws IOException 9 | { 10 | Client homeAddress = new Client("127.0.0.1"); 11 | homeAddress.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 | homeAddress.startRunning(); 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SERVER CLIENT CHATBOT 2 | Basic Java based server client chatbot application. 3 | 4 | ![Screenshot (78)](https://user-images.githubusercontent.com/44341534/80281662-60c32780-872a-11ea-8ea3-595ac43064b2.png) 5 | 6 | -------------------------------------------------------------------------------- /Server.java: -------------------------------------------------------------------------------- 1 | package server_client; 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 Server extends JFrame 9 | { 10 | private JTextField usertext; 11 | private JTextArea chatwindow; 12 | private ObjectOutputStream output; 13 | private ObjectInputStream input; 14 | private ServerSocket server; 15 | private Socket connection; 16 | 17 | public Server() 18 | { 19 | super("SERVER"); 20 | usertext = new JTextField(); 21 | usertext.setEditable(false); 22 | usertext.addActionListener( 23 | new ActionListener() 24 | { 25 | public void actionPerformed(ActionEvent event) 26 | { 27 | sendMessage(event.getActionCommand()); 28 | usertext.setText(""); 29 | } 30 | }); 31 | add(usertext,BorderLayout.SOUTH); 32 | chatwindow = new JTextArea(); 33 | chatwindow.setEditable(false); 34 | add(new JScrollPane(chatwindow)); 35 | setSize(1280,720); 36 | setVisible(true); 37 | } 38 | public void startRunning() 39 | { 40 | try 41 | { 42 | server = new ServerSocket(3000,20); 43 | while(true) 44 | { 45 | try 46 | { 47 | connection(); 48 | setup(); 49 | chatting(); 50 | } 51 | catch(IOException e) 52 | { 53 | showMessage("\n Server ended the connection! "); 54 | } 55 | finally 56 | { 57 | closeAll(); 58 | } 59 | } 60 | } 61 | catch(IOException e) 62 | { 63 | e.printStackTrace(); 64 | } 65 | } 66 | private void connection() throws IOException 67 | { 68 | showMessage("waiting for the connection..\n"); 69 | connection = server.accept(); 70 | showMessage(connection.getInetAddress().getHostName()+" connection"); 71 | } 72 | private void setup() throws IOException 73 | { 74 | output = new ObjectOutputStream(connection.getOutputStream()); 75 | output.flush(); 76 | input = new ObjectInputStream(connection.getInputStream()); 77 | showMessage("Everything is setup!\n"); 78 | } 79 | private void chatting() throws IOException 80 | { 81 | String message = "Connected!"; 82 | sendMessage(message); 83 | canType(true); //a lock on text box so that only one client can talk to server at same time not mulitplle 84 | do 85 | { 86 | try 87 | { 88 | message = (String) input.readObject(); 89 | showMessage(message + "\n"); 90 | } 91 | catch(ClassNotFoundException e) 92 | { 93 | showMessage("commmand not recognized\n"); 94 | } 95 | } 96 | while(!message.equals("Client - End")); 97 | } 98 | private void closeAll() throws IOException 99 | { 100 | showMessage("Closing connection...\n"); 101 | canType(false); 102 | try 103 | { 104 | output.close(); 105 | input.close(); 106 | connection.close(); 107 | } 108 | catch(IOException e) 109 | { 110 | e.printStackTrace(); 111 | } 112 | } 113 | private void sendMessage(String message) 114 | { 115 | try 116 | { 117 | output.writeObject("SERVER "+message); 118 | output.flush(); 119 | showMessage("SERVER "+message+"\n"); 120 | } 121 | catch(IOException e) 122 | { 123 | chatwindow.append("Error - message cannot be sent"); 124 | } 125 | } 126 | private void showMessage(final String text) 127 | { 128 | SwingUtilities.invokeLater( 129 | new Runnable() 130 | { 131 | public void run() 132 | { 133 | chatwindow.append(text); 134 | } 135 | }); 136 | } 137 | private void canType(final boolean tof) 138 | { 139 | SwingUtilities.invokeLater( 140 | new Runnable() 141 | { 142 | public void run() 143 | { 144 | usertext.setEditable(tof); 145 | } 146 | }); 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /ServerTest.java: -------------------------------------------------------------------------------- 1 | package server_client; 2 | import javax.swing.JFrame; 3 | 4 | public class ServerTest 5 | { 6 | 7 | public static void main(String[] args) 8 | { 9 | Server Host = new Server(); 10 | Host.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11 | Host.startRunning(); 12 | } 13 | 14 | } 15 | --------------------------------------------------------------------------------