Search This Blog

Free Two Person Text Chatting Application Software for Local Networks using Java With Source Code

two person text chat program java program java code java source code program code free chatting application free local network chatting free LAN Chatting LAN messenger
Java text Chatting program
Free two person chat program for local networks. This java text chatting application can be used for two person chatting within private or local networks. This java application uses TCP connection.

How to use

Both users should open this java application in their computer. One of them should click Make a Connection  while the other should enter the first person's ip address in the textfield provided and click Connect to this IP.


Limitations

The application is designed to work within same local network. it will also work across internet only if both the persons have direct connection to internet (not through a router). One of the persons should know the IP address of the other.

You can download the java chatting program (jar) from link below.

https://drive.google.com/file/d/0B5WYaNmq5rbuZ3BwQmh0LV91VzA/view?usp=sharing

/* 1: */ package pkg2chat; /* 2: */ /* 3: */ import java.awt.Container; /* 4: */ import java.awt.EventQueue; /* 5: */ import java.awt.Font; /* 6: */ import java.awt.event.ActionEvent; /* 7: */ import java.awt.event.ActionListener; /* 8: */ import java.io.BufferedReader; /* 9: */ import java.io.IOException; /* 10: */ import java.io.InputStream; /* 11: */ import java.io.InputStreamReader; /* 12: */ import java.io.PrintStream; /* 13: */ import java.net.InetAddress; /* 14: */ import java.net.ServerSocket; /* 15: */ import java.net.Socket; /* 16: */ import java.net.UnknownHostException; /* 17: */ import java.util.logging.Level; /* 18: */ import java.util.logging.Logger; /* 19: */ import javax.swing.JButton; /* 20: */ import javax.swing.JFrame; /* 21: */ import javax.swing.JLabel; /* 22: */ import javax.swing.JScrollPane; /* 23: */ import javax.swing.JSeparator; /* 24: */ import javax.swing.JTextArea; /* 25: */ import javax.swing.JTextField; /* 26: */ import javax.swing.UIDefaults; /* 27: */ import javax.swing.UIManager; /* 28: */ import javax.swing.UIManager.LookAndFeelInfo; /* 29: */ import javax.swing.UnsupportedLookAndFeelException; /* 30: */ /* 31: */ public class Home /* 32: */ extends JFrame /* 33: */ { /* 34: */ InetAddress ip; /* 35: */ boolean erred; /* 36: */ BufferedReader bin; /* 37: */ int line_no; /* 38: */ ServerSocket servr; /* 39: */ Socket client; /* 40: */ boolean ready_to_send; /* 41: */ PrintStream pout; /* 42: */ InputStream in; /* 43: */ boolean end_connection; /* 44: */ private JButton btn_connect_to_an_ip; /* 45: */ private JButton btn_disconnect; /* 46: */ private JButton btn_getip; /* 47: */ private JButton btn_host; /* 48: */ private JButton btn_send; /* 49: */ private JLabel jLabel1; /* 50: */ private JScrollPane jScrollPane1; /* 51: */ private JScrollPane jScrollPane2; /* 52: */ private JSeparator jSeparator1; /* 53: */ private JTextArea txt_chat; /* 54: */ private JTextField txt_dest_ip; /* 55: */ private JTextField txt_ip; /* 56: */ private JTextField txt_msg; /* 57: */ private JTextArea txt_status; /* 58: */ /* 59: */ public Home() /* 60: */ { /* 61: */ try /* 62: */ { /* 63: 36 */ for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { /* 64: 37 */ if ("Windows".equals(info.getName())) /* 65: */ { /* 66: 38 */ UIManager.setLookAndFeel(info.getClassName()); /* 67: 39 */ break; /* 68: */ } /* 69: */ } /* 70: */ } /* 71: */ catch (ClassNotFoundException ex) /* 72: */ { /* 73: 44 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 74: */ } /* 75: */ catch (InstantiationException ex) /* 76: */ { /* 77: 46 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 78: */ } /* 79: */ catch (IllegalAccessException ex) /* 80: */ { /* 81: 48 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 82: */ } /* 83: */ catch (UnsupportedLookAndFeelException ex) /* 84: */ { /* 85: 50 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 86: */ } /* 87: 52 */ initComponents(); /* 88: 53 */ setSize(412, 550); /* 89: */ try /* 90: */ { /* 91: 56 */ this.ip = InetAddress.getLocalHost(); /* 92: 57 */ this.txt_status.setText("Current IP address : " + this.ip.getHostAddress()); /* 93: */ } /* 94: */ catch (UnknownHostException ex) /* 95: */ { /* 96: 59 */ this.txt_status.setText("\nError getting ip"); /* 97: */ } /* 98: 61 */ this.txt_ip.setText(this.ip.getHostAddress()); /* 99: 62 */ this.txt_msg.setEnabled(false); /* 100: 63 */ this.btn_send.setEnabled(false); /* 101: 64 */ this.erred = false; /* 102: 65 */ this.line_no = 0; /* 103: 66 */ this.ready_to_send = false; /* 104: 67 */ this.end_connection = false; /* 105: */ } /* 106: */ /* 107: */ private void initComponents() /* 108: */ { /* 109: 76 */ this.txt_ip = new JTextField(); /* 110: 77 */ this.btn_getip = new JButton(); /* 111: 78 */ this.jLabel1 = new JLabel(); /* 112: 79 */ this.btn_host = new JButton(); /* 113: 80 */ this.btn_connect_to_an_ip = new JButton(); /* 114: 81 */ this.btn_send = new JButton(); /* 115: 82 */ this.txt_dest_ip = new JTextField(); /* 116: 83 */ this.txt_msg = new JTextField(); /* 117: 84 */ this.jScrollPane1 = new JScrollPane(); /* 118: 85 */ this.txt_status = new JTextArea(); /* 119: 86 */ this.jScrollPane2 = new JScrollPane(); /* 120: 87 */ this.txt_chat = new JTextArea(); /* 121: 88 */ this.btn_disconnect = new JButton(); /* 122: 89 */ this.jSeparator1 = new JSeparator(); /* 123: */ /* 124: 91 */ setDefaultCloseOperation(3); /* 125: 92 */ getContentPane().setLayout(null); /* 126: */ /* 127: 94 */ this.txt_ip.setEditable(false); /* 128: 95 */ this.txt_ip.setFont(new Font("Tahoma", 1, 14)); /* 129: 96 */ this.txt_ip.setHorizontalAlignment(0); /* 130: 97 */ getContentPane().add(this.txt_ip); /* 131: 98 */ this.txt_ip.setBounds(10, 30, 220, 34); /* 132: */ /* 133:100 */ this.btn_getip.setText("Refresh IP"); /* 134:101 */ this.btn_getip.addActionListener(new ActionListener() /* 135: */ { /* 136: */ public void actionPerformed(ActionEvent evt) /* 137: */ { /* 138:103 */ Home.this.btn_getipActionPerformed(evt); /* 139: */ } /* 140:105 */ }); /* 141:106 */ getContentPane().add(this.btn_getip); /* 142:107 */ this.btn_getip.setBounds(240, 30, 150, 34); /* 143: */ /* 144:109 */ this.jLabel1.setText("Your IP"); /* 145:110 */ getContentPane().add(this.jLabel1); /* 146:111 */ this.jLabel1.setBounds(20, 10, 50, 14); /* 147: */ /* 148:113 */ this.btn_host.setText("Make a Connection"); /* 149:114 */ this.btn_host.addActionListener(new ActionListener() /* 150: */ { /* 151: */ public void actionPerformed(ActionEvent evt) /* 152: */ { /* 153:116 */ Home.this.btn_hostActionPerformed(evt); /* 154: */ } /* 155:118 */ }); /* 156:119 */ getContentPane().add(this.btn_host); /* 157:120 */ this.btn_host.setBounds(10, 70, 220, 30); /* 158: */ /* 159:122 */ this.btn_connect_to_an_ip.setText("Connect to this IP"); /* 160:123 */ this.btn_connect_to_an_ip.addActionListener(new ActionListener() /* 161: */ { /* 162: */ public void actionPerformed(ActionEvent evt) /* 163: */ { /* 164:125 */ Home.this.btn_connect_to_an_ipActionPerformed(evt); /* 165: */ } /* 166:127 */ }); /* 167:128 */ getContentPane().add(this.btn_connect_to_an_ip); /* 168:129 */ this.btn_connect_to_an_ip.setBounds(240, 110, 150, 30); /* 169: */ /* 170:131 */ this.btn_send.setText("send"); /* 171:132 */ this.btn_send.addActionListener(new ActionListener() /* 172: */ { /* 173: */ public void actionPerformed(ActionEvent evt) /* 174: */ { /* 175:134 */ Home.this.btn_sendActionPerformed(evt); /* 176: */ } /* 177:136 */ }); /* 178:137 */ getContentPane().add(this.btn_send); /* 179:138 */ this.btn_send.setBounds(318, 160, 73, 30); /* 180:139 */ getContentPane().add(this.txt_dest_ip); /* 181:140 */ this.txt_dest_ip.setBounds(10, 110, 220, 30); /* 182:141 */ getContentPane().add(this.txt_msg); /* 183:142 */ this.txt_msg.setBounds(10, 160, 300, 30); /* 184: */ /* 185:144 */ this.txt_status.setEditable(false); /* 186:145 */ this.txt_status.setBackground(UIManager.getDefaults().getColor("Menu.background")); /* 187:146 */ this.txt_status.setColumns(20); /* 188:147 */ this.txt_status.setRows(1); /* 189:148 */ this.jScrollPane1.setViewportView(this.txt_status); /* 190: */ /* 191:150 */ getContentPane().add(this.jScrollPane1); /* 192:151 */ this.jScrollPane1.setBounds(10, 450, 380, 50); /* 193: */ /* 194:153 */ this.txt_chat.setEditable(false); /* 195:154 */ this.txt_chat.setColumns(20); /* 196:155 */ this.txt_chat.setRows(8); /* 197:156 */ this.jScrollPane2.setViewportView(this.txt_chat); /* 198: */ /* 199:158 */ getContentPane().add(this.jScrollPane2); /* 200:159 */ this.jScrollPane2.setBounds(10, 200, 380, 240); /* 201: */ /* 202:161 */ this.btn_disconnect.setText("Disconnect"); /* 203:162 */ this.btn_disconnect.setEnabled(false); /* 204:163 */ this.btn_disconnect.addActionListener(new ActionListener() /* 205: */ { /* 206: */ public void actionPerformed(ActionEvent evt) /* 207: */ { /* 208:165 */ Home.this.btn_disconnectActionPerformed(evt); /* 209: */ } /* 210:167 */ }); /* 211:168 */ getContentPane().add(this.btn_disconnect); /* 212:169 */ this.btn_disconnect.setBounds(240, 70, 150, 30); /* 213:170 */ getContentPane().add(this.jSeparator1); /* 214:171 */ this.jSeparator1.setBounds(10, 150, 380, 10); /* 215: */ /* 216:173 */ pack(); /* 217: */ } /* 218: */ /* 219: */ private void btn_getipActionPerformed(ActionEvent evt) /* 220: */ { /* 221: */ try /* 222: */ { /* 223:178 */ this.ip = InetAddress.getLocalHost(); /* 224: */ } /* 225: */ catch (UnknownHostException ex) /* 226: */ { /* 227:180 */ this.txt_status.setText("\nError getting ip"); /* 228: */ } /* 229:182 */ this.txt_ip.setText(this.ip.getHostAddress()); /* 230: */ } /* 231: */ /* 232: */ private void btn_hostActionPerformed(ActionEvent evt) /* 233: */ { /* 234:186 */ this.erred = false; /* 235:187 */ this.btn_host.setEnabled(false); /* 236:188 */ this.txt_dest_ip.setEnabled(false); /* 237:189 */ this.txt_dest_ip.setEditable(false); /* 238:190 */ this.btn_connect_to_an_ip.setEnabled(false); /* 239:191 */ this.btn_getip.setEnabled(false); /* 240: */ try /* 241: */ { /* 242:193 */ this.servr = new ServerSocket(6013); /* 243:194 */ this.client = this.servr.accept(); /* 244:195 */ this.client.setKeepAlive(true); /* 245:196 */ this.pout = new PrintStream(this.client.getOutputStream()); /* 246:197 */ this.in = this.client.getInputStream(); /* 247:198 */ this.bin = new BufferedReader(new InputStreamReader(this.in)); /* 248: */ } /* 249: */ catch (IOException ex) /* 250: */ { /* 251:200 */ this.txt_status.setText("Error in connection."); /* 252:201 */ this.erred = true; /* 253: */ } /* 254:204 */ if (!this.client.isConnected()) /* 255: */ { /* 256:206 */ this.txt_status.setText("Connection cannot be established."); /* 257:207 */ this.erred = true; /* 258: */ } /* 259: */ else /* 260: */ { /* 261:211 */ this.txt_status.setText("Conected"); /* 262: */ } /* 263:214 */ if (this.erred) /* 264: */ { /* 265:216 */ this.end_connection = false; /* 266:217 */ this.btn_disconnect.setEnabled(true); /* 267:218 */ this.btn_send.setEnabled(true); /* 268:219 */ this.txt_msg.setEditable(true); /* 269:220 */ this.txt_msg.setEnabled(true); /* 270:221 */ this.txt_dest_ip.setEnabled(true); /* 271:222 */ this.btn_connect_to_an_ip.setEnabled(true); /* 272:223 */ this.btn_host.setEnabled(true); /* 273: */ } /* 274: */ else /* 275: */ { /* 276:226 */ this.btn_disconnect.setEnabled(true); /* 277:227 */ this.end_connection = false; /* 278:228 */ this.send_or_read.start(); /* 279:229 */ this.ready_to_send = true; /* 280:230 */ this.btn_send.setEnabled(true); /* 281:231 */ this.txt_msg.setEditable(true); /* 282:232 */ this.txt_msg.setEnabled(true); /* 283:233 */ this.txt_msg.setText("Started"); /* 284:234 */ this.btn_send.doClick(); /* 285: */ } /* 286: */ } /* 287: */ /* 288: */ private void btn_connect_to_an_ipActionPerformed(ActionEvent evt) /* 289: */ { /* 290:240 */ this.erred = false; /* 291:241 */ this.txt_dest_ip.setEnabled(false); /* 292:242 */ this.btn_connect_to_an_ip.setEnabled(false); /* 293:243 */ this.btn_host.setEnabled(false); /* 294: */ try /* 295: */ { /* 296:246 */ this.client = new Socket(this.txt_dest_ip.getText(), 6013); /* 297:247 */ this.client.setKeepAlive(true); /* 298:248 */ this.pout = new PrintStream(this.client.getOutputStream()); /* 299:249 */ this.in = this.client.getInputStream(); /* 300:250 */ this.bin = new BufferedReader(new InputStreamReader(this.in)); /* 301: */ } /* 302: */ catch (IOException ex) /* 303: */ { /* 304:252 */ this.txt_status.setText("Error in connection."); /* 305:253 */ this.erred = true; /* 306: */ } /* 307:255 */ if (!this.client.isConnected()) /* 308: */ { /* 309:257 */ this.txt_status.setText("Connection cannot be established."); /* 310:258 */ this.erred = true; /* 311: */ } /* 312: */ else /* 313: */ { /* 314:262 */ this.txt_status.setText("Connected"); /* 315: */ } /* 316:264 */ if (this.erred) /* 317: */ { /* 318:266 */ this.end_connection = false; /* 319:267 */ this.btn_disconnect.setEnabled(true); /* 320:268 */ this.btn_send.setEnabled(true); /* 321:269 */ this.txt_msg.setEditable(true); /* 322:270 */ this.txt_msg.setEnabled(true); /* 323:271 */ this.txt_dest_ip.setEnabled(true); /* 324:272 */ this.btn_connect_to_an_ip.setEnabled(true); /* 325:273 */ this.btn_host.setEnabled(true); /* 326: */ } /* 327: */ else /* 328: */ { /* 329:277 */ this.end_connection = false; /* 330:278 */ this.btn_disconnect.setEnabled(true); /* 331:279 */ this.btn_send.setEnabled(true); /* 332:280 */ this.txt_msg.setEditable(true); /* 333:281 */ this.txt_msg.setEnabled(true); /* 334:282 */ this.send_or_read.start(); /* 335: */ } /* 336: */ } /* 337: */ /* 338: */ private void btn_sendActionPerformed(ActionEvent evt) /* 339: */ { /* 340:288 */ this.ready_to_send = true; /* 341: */ } /* 342: */ /* 343: */ private void btn_disconnectActionPerformed(ActionEvent evt) /* 344: */ { /* 345:292 */ this.end_connection = true; /* 346: */ } /* 347: */ /* 348: */ public static void main(String[] args) /* 349: */ { /* 350: */ try /* 351: */ { /* 352:305 */ for (UIManager.LookAndFeelInfo info : ) { /* 353:306 */ if ("Nimbus".equals(info.getName())) /* 354: */ { /* 355:307 */ UIManager.setLookAndFeel(info.getClassName()); /* 356:308 */ break; /* 357: */ } /* 358: */ } /* 359: */ } /* 360: */ catch (ClassNotFoundException ex) /* 361: */ { /* 362:312 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 363: */ } /* 364: */ catch (InstantiationException ex) /* 365: */ { /* 366:314 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 367: */ } /* 368: */ catch (IllegalAccessException ex) /* 369: */ { /* 370:316 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 371: */ } /* 372: */ catch (UnsupportedLookAndFeelException ex) /* 373: */ { /* 374:318 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 375: */ } /* 376:323 */ EventQueue.invokeLater(new Runnable() /* 377: */ { /* 378: */ public void run() /* 379: */ { /* 380:325 */ new Home().setVisible(true); /* 381: */ } /* 382: */ }); /* 383: */ } /* 384: */ /* 385:348 */ Thread send_or_read = new Thread() /* 386: */ { /* 387: */ public void run() /* 388: */ { /* 389: */ for (;;) /* 390: */ { /* 391:360 */ if (Home.this.ready_to_send == true) /* 392: */ { /* 393:362 */ if (Home.this.txt_msg.getText() != null) /* 394: */ { /* 395:364 */ Home.this.pout.println(Home.this.txt_msg.getText()); /* 396:365 */ Home.this.txt_chat.setText(Home.this.txt_chat.getText() + "\nMe: " + Home.this.txt_msg.getText()); /* 397:366 */ Home.this.txt_msg.setText(null); /* 398:367 */ Home.this.line_no += 1; /* 399:368 */ if (Home.this.line_no > 12) /* 400: */ { /* 401:370 */ Home.this.txt_chat.setText(Home.this.txt_chat.getText().substring(Home.this.txt_chat.getText().indexOf("\n") + 1)); /* 402:371 */ Home.this.line_no -= 1; /* 403: */ } /* 404: */ } /* 405:374 */ Home.this.ready_to_send = false; /* 406: */ } /* 407: */ try /* 408: */ { /* 409:380 */ if (Home.this.bin.ready()) /* 410: */ { /* 411:383 */ String line = Home.this.bin.readLine(); /* 412:384 */ if (line != null) /* 413: */ { /* 414:386 */ Home.this.txt_chat.setText(Home.this.txt_chat.getText() + "\nHe: " + line); /* 415:387 */ Home.this.line_no += 1; /* 416:388 */ if (Home.this.line_no > 12) /* 417: */ { /* 418:390 */ Home.this.txt_chat.setText(Home.this.txt_chat.getText().substring(Home.this.txt_chat.getText().indexOf("\n") + 1)); /* 419:391 */ Home.this.line_no -= 1; /* 420: */ } /* 421: */ } /* 422: */ } /* 423: */ } /* 424: */ catch (IOException ex) /* 425: */ { /* 426:399 */ Home.this.txt_status.setText(Home.this.txt_status.getText() + "\nCheck network!"); /* 427: */ } /* 428:401 */ if (Home.this.client.isConnected()) { /* 429:401 */ if (Home.this.end_connection == true) { /* 430: */ break; /* 431: */ } /* 432: */ } /* 433: */ } /* 434:404 */ Home.this.txt_ip.setEnabled(true); /* 435:405 */ Home.this.btn_getip.setEnabled(true); /* 436:406 */ Home.this.txt_dest_ip.setEditable(true); /* 437:407 */ Home.this.btn_connect_to_an_ip.setEnabled(true); /* 438:408 */ Home.this.btn_host.setEnabled(true); /* 439:409 */ Home.this.txt_chat.setText(""); /* 440:410 */ Home.this.txt_msg.setEnabled(false); /* 441:411 */ Home.this.btn_send.setEnabled(false); /* 442:412 */ Home.this.txt_status.setText(Home.this.txt_status.getText() + "\n Disconnected"); /* 443:413 */ Home.this.btn_disconnect.setEnabled(false); /* 444: */ try /* 445: */ { /* 446:415 */ Home.this.client.close(); /* 447: */ } /* 448: */ catch (IOException ex) /* 449: */ { /* 450:417 */ Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex); /* 451: */ } /* 452: */ } /* 453: */ }; /* 454: */ }

No comments:

Post a Comment