GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java-Chat-Application/MyServer.java at main · zack047/Java-Chat-Application Expand file tree/ Copy pathMyServer.javaMore file actions 328 lines (288 loc) · 7.3 KB / Copy pathMyServer.javaFile metadata and controls328 lines (288 loc) · 7.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class MyServer { ArrayList al=new ArrayList(); ArrayList users=new ArrayList(); ServerSocket ss; Socket s; public final static int PORT=10; public final static String UPDATE_USERS="updateuserslist:"; public final static String LOGOUT_MESSAGE="@@logoutme@@:"; public MyServer() { try{ ss=new ServerSocket(PORT); System.out.println("Server Started "+ss); while(true) { s=ss.accept(); Runnable r=new MyThread(s,al,users); Thread t=new Thread(r); t.start(); } } catch(Exception e){System.err.println("Server constructor"+e);} } public static void main(String [] args) { new MyServer(); } } class MyThread implements Runnable { Socket s; ArrayList al; ArrayList users; String username; MyThread (Socket s, ArrayList al,ArrayList users) { this.s=s; this.al=al; this.users=users; try{ DataInputStream dis=new DataInputStream(s.getInputStream()); username=dis.readUTF(); al.add(s); users.add(username); tellEveryOne("****** "+ username+" Logged in at "+(new Date())+" ******"); sendNewUserList(); } catch(Exception e){System.err.println("MyThread constructor "+e);} } public void run() { String s1; try{ DataInputStream dis=new DataInputStream(s.getInputStream()); do { s1=dis.readUTF(); if(s1.toLowerCase().equals(MyServer.LOGOUT_MESSAGE)) break; // System.out.println("received from "+s.getPort()); tellEveryOne(username+" said: "+" : "+s1); } while(true); DataOutputStream tdos=new DataOutputStream(s.getOutputStream()); tdos.writeUTF(MyServer.LOGOUT_MESSAGE); tdos.flush(); users.remove(username); tellEveryOne("****** "+username+" Logged out at "+(new Date())+" ******"); sendNewUserList(); al.remove(s); s.close(); } catch(Exception e){System.out.println("MyThread Run"+e);} } public void sendNewUserList() { tellEveryOne(MyServer.UPDATE_USERS+users.toString()); } public void tellEveryOne(String s1) { Iterator i=al.iterator(); while(i.hasNext()) { try{ Socket temp=(Socket)i.next(); DataOutputStream dos=new DataOutputStream(temp.getOutputStream()); dos.writeUTF(s1); dos.flush(); //System.out.println("sent to : "+temp.getPort()+" : "+ s1); } catch(Exception e){System.err.println("TellEveryOne "+e);} } } } class MyClient implements ActionListener { Socket s; DataInputStream dis; DataOutputStream dos; JButton sendButton, logoutButton,loginButton, exitButton; JFrame chatWindow; JTextArea txtBroadcast; JTextArea txtMessage; JList usersList; public void displayGUI() { chatWindow=new JFrame(); txtBroadcast=new JTextArea(5,30); txtBroadcast.setEditable(false); txtMessage=new JTextArea(2,20); usersList=new JList(); sendButton=new JButton("Send"); logoutButton=new JButton("Exit"); loginButton=new JButton("Enter"); exitButton=new JButton("Leave"); JPanel center1=new JPanel(); center1.setLayout(new BorderLayout()); center1.add(new JLabel("Broad Cast messages from all online users",JLabel.CENTER),"North"); center1.add(new JScrollPane(txtBroadcast),"Center"); JPanel south1=new JPanel(); south1.setLayout(new FlowLayout()); south1.add(new JScrollPane(txtMessage)); south1.add(sendButton); JPanel south2=new JPanel(); south2.setLayout(new FlowLayout()); south2.add(loginButton); south2.add(logoutButton); south2.add(exitButton); JPanel south=new JPanel(); south.setLayout(new GridLayout(2,1)); south.add(south1); south.add(south2); JPanel east=new JPanel(); east.setLayout(new BorderLayout()); east.add(new JLabel("Online Users",JLabel.CENTER),"East"); east.add(new JScrollPane(usersList),"South"); chatWindow.add(east,"East"); chatWindow.add(center1,"Center"); chatWindow.add(south,"South"); chatWindow.pack(); chatWindow.setTitle("Login for Chat"); chatWindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); chatWindow.setVisible(true); sendButton.addActionListener(this); logoutButton.addActionListener(this); loginButton.addActionListener(this); exitButton.addActionListener(this); logoutButton.setEnabled(false); loginButton.setEnabled(true); txtMessage.addFocusListener(new FocusAdapter() {public void focusGained(FocusEvent fe){txtMessage.selectAll();}}); chatWindow.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { if(s!=null) { JOptionPane.showMessageDialog(chatWindow,"u r logged out right now. ","Exit",JOptionPane.INFORMATION_MESSAGE); logoutSession(); } System.exit(0); } }); } public void actionPerformed(ActionEvent ev) { JButton temp=(JButton)ev.getSource(); if(temp==sendButton) { if(s==null) {JOptionPane.showMessageDialog(chatWindow,"You should Enter by telling your name"); return;} try{ dos.writeUTF(txtMessage.getText()); txtMessage.setText(""); } catch(Exception excp){txtBroadcast.append("\nsend button click :"+excp);} } if(temp==loginButton) { String uname=JOptionPane.showInputDialog(chatWindow,"Enter Your lovely nick name: "); if(uname!=null) clientChat(uname); } if(temp==logoutButton) { if(s!=null) logoutSession(); } if(temp==exitButton) { if(s!=null) { JOptionPane.showMessageDialog(chatWindow,"You have Exited right now. ","Exit",JOptionPane.INFORMATION_MESSAGE); logoutSession(); } System.exit(0); } } public void logoutSession() { if(s==null) return; try{ dos.writeUTF(MyServer.LOGOUT_MESSAGE); Thread.sleep(500); s=null; } catch(Exception e){txtBroadcast.append("\n inside logoutSession Method"+e);} logoutButton.setEnabled(false); loginButton.setEnabled(true); chatWindow.setTitle("Enter for Chat"); } ////////////////////////// public void clientChat(String uname) { try{ s=new Socket(InetAddress.getLocalHost(),MyServer.PORT); dis=new DataInputStream(s.getInputStream()); dos=new DataOutputStream(s.getOutputStream()); ClientThread ct=new ClientThread(dis,this); Thread t1=new Thread(ct); t1.start(); dos.writeUTF(uname); chatWindow.setTitle(uname+" Chat Window"); } catch(Exception e){txtBroadcast.append("\nClient Constructor " +e);} logoutButton.setEnabled(true); loginButton.setEnabled(false); } public MyClient() { displayGUI(); // clientChat(); } public static void main(String []args) { new MyClient(); } } class ClientThread implements Runnable { DataInputStream dis; MyClient client; ClientThread(DataInputStream dis,MyClient client) { this.dis=dis; this.client=client; } public void run() { String s2=""; do { try{ s2=dis.readUTF(); if(s2.startsWith(MyServer.UPDATE_USERS)) updateUsersList(s2); else if(s2.equals(MyServer.LOGOUT_MESSAGE)) break; else client.txtBroadcast.append("\n"+s2); int lineOffset=client.txtBroadcast.getLineStartOffset(client.txtBroadcast.getLineCount()-1); client.txtBroadcast.setCaretPosition(lineOffset); } catch(Exception e){client.txtBroadcast.append("\nClientThread run : "+e);} } while(true); } public void updateUsersList(String ul) { Vector ulist=new Vector(); ul=ul.replace("[",""); ul=ul.replace("]",""); ul=ul.replace(MyServer.UPDATE_USERS,""); StringTokenizer st=new StringTokenizer(ul,","); while(st.hasMoreTokens()) { String temp=st.nextToken(); ulist.add(temp); } client.usersList.setListData(ulist); } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |