Results 1 to 1 of 1
- 08-24-2010, 04:39 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
Create Personal Messenger that works on Network or Internet
hello friends i wrote this coding for personal Messenger
Now we have to create two different class
Class 1 :
Class Name : msgFrame.java
Coding like below
Now we have to create second classJava Code:/* // Created By : 13Guru (Vishal D Paraliya) // Date : 3-Aug-2010 //------------------------------------------------------------------------------- // This is a personal messenger we can use this on // network or internet and its easy to use i have // wrote coding just for messenge if you want to // also share files and something else then tell me // I will like to help everone.... //------------------------------------------------------------------------------- // Syntax : java msgFrame <your name> // Example : java msgFrame 13Guru // then u get a jframe there was to ways to connect with another // client first is to send request to another client // and second is to accept the request one client must // request and second must be accept this is for security // so then send messeges to each other // Enjoy.... Happy Chating..... */ import java.net.*; import java.rmi.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class msgFrame extends JFrame implements ActionListener { //Component Declaration private javax.swing.JTextField AccPort; private javax.swing.JLabel IPADD; private javax.swing.JButton btnAccept; private javax.swing.JButton btnSend; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextField reIP; public javax.swing.JTextArea msgArea; private javax.swing.JTextField rePort; public javax.swing.JTextArea sendMsg; // Variable Declaration String ip; protected ServerSocket ss; protected Socket sk; protected static msgFrame mf; protected static msgThread mt; Thread t; public static String myName = null; public msgFrame() { java.awt.GridBagConstraints gridBagConstraints; setSize(300,400); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); IPADD = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); msgArea = new javax.swing.JTextArea(); jPanel3 = new javax.swing.JPanel(); btnSend = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); sendMsg = new javax.swing.JTextArea(); jPanel4 = new javax.swing.JPanel(); jLabel4 = new javax.swing.JLabel(); rePort = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); AccPort = new javax.swing.JTextField(); btnAccept = new javax.swing.JButton(); reIP = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.GridBagLayout()); jPanel1.setLayout(new java.awt.GridBagLayout()); jLabel1.setText("My Messenger"); jPanel1.add(jLabel1, new java.awt.GridBagConstraints()); jLabel2.setText("IP :"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel1.add(jLabel2, gridBagConstraints); IPADD.setText("...."); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; jPanel1.add(IPADD, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; getContentPane().add(jPanel1, gridBagConstraints); jPanel2.setLayout(new java.awt.GridBagLayout()); msgArea.setColumns(20); msgArea.setRows(5); msgArea.setDisabledTextColor(new java.awt.Color(0, 0, 0)); msgArea.setEnabled(false); jScrollPane1.setViewportView(msgArea); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.weightx = 0.5; jPanel2.add(jScrollPane1, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); getContentPane().add(jPanel2, gridBagConstraints); jPanel3.setLayout(new java.awt.GridBagLayout()); btnSend.setText("Send"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0); jPanel3.add(btnSend, gridBagConstraints); sendMsg.setColumns(20); sendMsg.setRows(5); jScrollPane2.setViewportView(sendMsg); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; jPanel3.add(jScrollPane2, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0); getContentPane().add(jPanel3, gridBagConstraints); jPanel4.setLayout(new java.awt.GridBagLayout()); jLabel4.setText("Port"); jPanel4.add(jLabel4, new java.awt.GridBagConstraints()); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 0.5; jPanel4.add(rePort, gridBagConstraints); jButton1.setText("Send Request"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel4.add(jButton1, gridBagConstraints); jLabel3.setText("Port"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; jPanel4.add(jLabel3, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.weightx = 0.5; jPanel4.add(AccPort, gridBagConstraints); btnAccept.setText("Accept Request"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 2; jPanel4.add(btnAccept, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; jPanel4.add(reIP, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(1, 0, 0, 0); getContentPane().add(jPanel4, gridBagConstraints); jButton1.addActionListener(this); btnSend.addActionListener(this); btnAccept.addActionListener(this); try{ ip = (InetAddress.getLocalHost()).toString(); IPADD.setText(ip); } catch(Exception e) {} } public static void main(String arg[]) { myName = arg[0]; mf = new msgFrame(); mt = new msgThread(mf); mf.setVisible(true); } public void showMsg(String msg) { System.out.println("show Message"); msg = msgArea.getText() + "\n" + msg; msgArea.setText(msg); } public void actionPerformed(ActionEvent ae) { try{ if(ae.getSource()==jButton1) { int port = (new Integer(rePort.getText()).intValue()); String add = reIP.getText(); sk = new Socket(add,port); ObjectOutputStream oos = new ObjectOutputStream(sk.getOutputStream()); oos.writeObject("Request Accepted"); mt.setSocket(sk); } else if(ae.getSource()==btnAccept) { int port = (new Integer(AccPort.getText()).intValue()); ss = new ServerSocket(port); sk = ss.accept(); mt.setSocket(sk); // ObjectInputStream ois = new ObjectInputStream(sk.getInputStream()); // String msg = (String) ois.readObject(); // msgArea.setText("ok"); } else if(ae.getSource()==btnSend) { String msg = (String)sendMsg.getText(); msg = myName + " : " + msg; msgArea.setText(msgArea.getText() + "\n" + msg); System.out.println(msg); ObjectOutputStream oos = new ObjectOutputStream(sk.getOutputStream()); oos.writeObject(msg); sendMsg.setText(""); } }catch(Exception e) {} } }
Class 2 :
Class Name : msgThread
Java Code:import java.net.*; import java.rmi.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; public class msgThread extends msgFrame implements Runnable { public Socket thsock; int l; msgFrame mf; Thread t; // Constructor to Set Class public msgThread(msgFrame setmf) { mf=setmf; } public void setSocket(Socket sock) { System.out.println("Socket Setting...."); thsock = sock; //this.run(); t = new Thread(this,"guru"); t.start(); System.out.println("Thread Started"); } public void run() { try{ l=1; System.out.println("Start"); for(;;) { System.out.println("Loop Start"); ObjectInputStream ois = new ObjectInputStream(thsock.getInputStream()); String msg = (String) ois.readObject(); System.out.println("Thread : " + msg); mf.showMsg(msg); l++; //msg = super.msgArea.getText() + "\n" + msg; //super.msgArea.setText("Message"+msg); //System.out.println("Loop"); //Thread.sleep(1000); } }catch(Exception e) {} } }
Now Its time to complie them and now run like below
Syntax : java msgFrame <your name>
Example : java msgFrame 13Guru
then u get a jframe there was to ways to connect with another
client first is to send request to another client
and second is to accept the request one client must
request and second must be accept this is for security
so then send messeges to each other
Enjoy.... Happy Chating.....
Similar Threads
-
Server is in a network using an IP, but router in network carries the external IP(www
By lse123 in forum NetworkingReplies: 7Last Post: 05-02-2010, 10:35 PM -
Java chat program, works on single PC, but not over internet?
By rolls in forum NetworkingReplies: 7Last Post: 03-31-2009, 01:29 PM -
how to write a messenger with using java?
By kira4 in forum New To JavaReplies: 2Last Post: 07-30-2008, 08:15 PM -
123 Web Messenger Server 1.2
By JavaBean in forum Java SoftwareReplies: 0Last Post: 08-10-2007, 04:32 PM -
messenger
By Peter in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 07:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks