Results 1 to 13 of 13
Thread: multiclient/server program
- 06-17-2011, 10:49 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
multiclient/server program
Hi, I have 3 errors in my program. one is that the client program printwriter in the chat class gives out null at the "sendToServer" method in the "chat" class. 2nd, the server sends messages to the client but the client program doesnt add it to the gui via the "refresh" method while the server can add text to the gui. 3rd is the client object in the server program doesn't work, it just gives out nulls for the accessors (get method).
thanks in advance, I really do appreciate your help, I don't know if I should take out the gui.gif)
PHP Code:import java.io.*; import java.net.*; import java.text.SimpleDateFormat; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; public class serverInterface extends javax.swing.JFrame { PrintStream sendToClient[]=new PrintStream [50]; BufferedReader inFrmClient[]= new BufferedReader[50]; public static final int prt = 9999; verify ver = new verify(); boolean started = false; Socket[] clientSock = new Socket[100]; boolean off=true; client clients[] = new client[100]; int num = 0; ServerSocket svrSock = null; public static final String time = "EEE HH:mm:ss a"; //time format of: day, hour, minute, second, pm/am // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); status = new javax.swing.JTextArea(); shutDown = new javax.swing.JButton(); serverMessage = new javax.swing.JTextField(); send = new javax.swing.JButton(); startButton = new javax.swing.JButton(); helpbutton = new javax.swing.JButton(); addClient = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); status.setColumns(20); status.setEditable(false); status.setRows(5); status.setText("Hello"); status.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); status.setVerifyInputWhenFocusTarget(false); jScrollPane1.setViewportView(status); shutDown.setText("Shutdown Server"); shutDown.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { shutDownActionPerformed(evt); } }); send.setText("Send"); send.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendActionPerformed(evt); } }); startButton.setText("Start Server"); startButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { startButtonActionPerformed(evt); } }); helpbutton.setText("help"); helpbutton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { helpbuttonActionPerformed(evt); } }); addClient.setText("Add client"); addClient.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { addClientActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 415, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(startButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(helpbutton) .addGap(18, 18, 18) .addComponent(addClient) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 189, Short.MAX_VALUE) .addComponent(shutDown) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addComponent(serverMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(send) .addGap(44, 44, 44))))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(startButton) .addComponent(shutDown) .addComponent(helpbutton) .addComponent(addClient)) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(serverMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(send)) .addContainerGap()) ); pack(); }// </editor-fold> private void shutDownActionPerformed(java.awt.event.ActionEvent evt) { shutDown(); } private void sendActionPerformed(java.awt.event.ActionEvent evt) { send(); } private void startButtonActionPerformed(java.awt.event.ActionEvent evt) { start(); } private void helpbuttonActionPerformed(java.awt.event.ActionEvent evt) { new help().setVisible(true); } private void addClientActionPerformed(java.awt.event.ActionEvent evt) { new addc().setVisible(true); } public void start() { try { svrSock = new ServerSocket(prt);//starts opening conenction } catch (IOException ex) { JOptionPane.showMessageDialog(null, "error with connection"); } startButton.setVisible(false);//so the start button justn't get pressed twice send.setVisible(true); serverMessage.setVisible(true); Thread rc = new Thread(new runChat()); //starts the server rc.start(); } public serverInterface() { initComponents(); send.setVisible(false); serverMessage.setVisible(false); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new serverInterface().setVisible(true); } }); } public class runChat implements Runnable { public void run() { do { try { clientSock[num] = svrSock.accept(); //client accepts, connection has been made } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } Thread chats = new Thread(new chatSer(num)); chats.start(); //runs thread num++;//new client number (next) } while(off!=false); } } public class chatSer implements Runnable { private int count; private String stat; chatSer(int co) { count = co; } public void run() { runF("Waiting for client to connect"); System.out.println("open"); started=true; //states the connection has been made System.out.println("connected"); runF("client " + clientSock[count].getInetAddress().getHostName()+" is connected, receiving credentials now..."); //gets info on client try { sendToClient[count] = new PrintStream(clientSock[count].getOutputStream()); //starts the info printer to client } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } try { inFrmClient[count] = new BufferedReader(new InputStreamReader(clientSock[count].getInputStream())); //starts the input reader from client String clientInput = inFrmClient[count].readLine(); //gets from client runF("verifying credentials"); String vy = ver.verify(clientInput,count); //runs verifying process with clients credintials if (!vy.equals("true")) { sendToClient[count].println(vy); //sends outcome to client as failed log in } else //sends outcome to client as logged in { sendToClient[count].println(vy); System.out.println(vy); //until logged out is sent to server Thread receive = new Thread(new receive(count)); receive.start(); } } catch (IOException e) { JOptionPane.showMessageDialog(null, "Connection error"); } } } public class receive implements Runnable { private int cou; receive(int cou) { this.cou=cou; } @SuppressWarnings("empty-statement") public void run() { System.out.println(cou); String user = clients[cou].getUsername(); try { while(!inFrmClient[cou].readLine().equals("logout")); { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(time); String mes = inFrmClient[cou].readLine(); if(clients[cou].getStats() == 2) { sendToClient[cou].println(sdf.format(cal.getTime()) + "- " + user + ": " + mes); //timestamp + username + message runF(sdf.format(cal.getTime()) + "- " + user + ": " + mes); } } } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } try { clientSock[cou].close(); //closes client connection inFrmClient[cou].close(); sendToClient[cou].close(); } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } runF("User " + clients[cou].getUsername() + " logged out"); clients[cou].setStats(3); } } public void send() { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(time); runF(sdf.format(cal.getTime())+"- Admin: "+serverMessage.getText()); //adds it to gui //for(int r = 0; r<num-1; r++) { //if(clients[r].getStats() == 2) //if client is logged it //{ sendToClient[0].println(sdf.format(cal.getTime())+"- Admin: "+serverMessage.getText()); //sends the message in the input box //} } } public void shutDown() { if(started == true) //this closes it straight if no connections have been made { for(int t =0; t<num-1; t++) { if(clients[t].getStats() != 3)//if connection is open, so if it has logged on or logging on it will be { sendToClient[t].println("logout"); try { inFrmClient[t].close(); } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } sendToClient[t].close(); } off = true; } } System.exit(0); //exit } public void runF(String reff) //starts thread with parameters { Thread r = new Thread(new refresh(reff)); r.start(); } public class refresh implements Runnable //this update the messages in the box with no freezing as it is a different thread { private String ref; refresh(String re) //this gets the parameter to run with thread { ref = re; } public void run() //this is what is run in the thread { status.setText(status.getText()+"\n"+ref); serverMessage.setText(""); } } // Variables declaration - do not modify private javax.swing.JButton addClient; private javax.swing.JButton helpbutton; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton send; public javax.swing.JTextField serverMessage; private javax.swing.JButton shutDown; private javax.swing.JButton startButton; public javax.swing.JTextArea status; // End of variables declaration }PHP Code:public class client { private int stats; private String username; public client(int st, String user) { stats =st; // 2 for loggedin, 3 for logged out username = user; } public int getStats() { return stats; } public void setStats(int stats) { this.stats = stats; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }PHP Code:import java.io.*; import java.util.*; import javax.swing.JOptionPane; public class verify { private String username =""; private String password =""; private String creds; private int coun; public String verify(String cred,int c) { coun = c; serverInterface svri = new serverInterface(); creds = cred; String pass[] = new String[50]; String user[] = new String[50]; String temp=""; int t=0; while(creds.charAt(t)!='#') { if(creds.charAt(t)!='#') { username = username + creds.charAt(t); //extracts username t++; } } t++; //int length = creds.length(); while(creds.charAt(t)!='#') { if(creds.charAt(t)!='#') { password = password + creds.charAt(t); //extracts password t++; } } int count = 0; try { Scanner sc = new Scanner(new FileReader("users.txt")).useDelimiter("#"); //loads database of usernames and passwords while(sc.hasNext()) { user[count]=sc.next(); pass[count]=sc.next(); count++; if(sc.hasNextLine()) { sc.nextLine(); } } sc.close(); } catch (FileNotFoundException ex) { //catch for missing file JOptionPane.showMessageDialog(null,"users file not found. It may be missing or corrupted"); } boolean usertrue = false; for(int y=0;y<count+1;y++) { if(username.equals(user[y])) //if username is found { usertrue=true; if(password.equals(pass[y])) //if password matches with username { temp = "true"; svri.clients[coun] = new client(2,username); //new client object } else //if password doesn't match with username { temp = "Incorrect password"; } } } if(usertrue == false) { temp = "Username not found"; } return temp; //returns outcome } }Client program:PHP Code:import java.io.*; import java.net.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; public class Chat extends javax.swing.JFrame { String loginStatus; String received=""; Send sd = new Send(); boolean end = false; public static final int prt = 9999; Socket clientsock; BufferedReader Infromserver; PrintStream tosvr; public void change() throws IOException { this.dispose(); //closes old window java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Chat().setVisible(true); } }); } public Chat() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { logoutButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); chatMessages = new javax.swing.JTextArea(); sendButton = new javax.swing.JButton(); message = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); logoutButton.setText("Logout"); logoutButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { logoutButtonActionPerformed(evt); } }); chatMessages.setColumns(20); chatMessages.setEditable(false); chatMessages.setRows(5); chatMessages.setText("Hello"); chatMessages.setVerifyInputWhenFocusTarget(false); jScrollPane1.setViewportView(chatMessages); sendButton.setText("Send"); sendButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(457, Short.MAX_VALUE) .addComponent(logoutButton) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 294, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(41, 41, 41) .addComponent(sendButton)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(99, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(12, 12, 12) .addComponent(logoutButton) .addGap(20, 20, 20) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(sendButton) .addComponent(message, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)) .addGap(39, 39, 39)) ); pack(); }// </editor-fold> private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) { try { end = true; tosvr.println("logout"); tosvr.close(); Infromserver.close(); clientsock.close(); JOptionPane.showMessageDialog(null, "Program is logged out, it will now exit"); System.exit(0); } catch (IOException ex) { Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex); } } private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) { sendToServer(); //sends message to server } public void start(String un, String pass, String ipad) { Thread sb = new Thread(new sendButton(un,pass,ipad)); sb.start(); } public class sendButton implements Runnable { String un,pass,ipad; sendButton(String un, String pass, String ipad) { this.un = un; this.pass = pass; this.ipad = ipad; //ip address } public void run() { try { clientsock = new Socket(ipad, prt); tosvr = new PrintStream(clientsock.getOutputStream()); //to send to server Infromserver = new BufferedReader(new InputStreamReader(clientsock.getInputStream())); tosvr.println(un+"#"+pass+"#"); //sends password and username as one word String reply = Infromserver.readLine(); //receives confomation System.out.println(reply); if(reply.equals("true")) { change();//switch to chat gui Thread chat = new Thread(new running()); chat.start(); //starts new thread running } else { JOptionPane.showMessageDialog(null, "Error: "+reply); //if your credentials don't match System.exit(0); } } catch (UnknownHostException ex) { JOptionPane.showMessageDialog(null,"No server Found with that IP address"); } catch (IOException ex) { JOptionPane.showMessageDialog(null,"Error with internet connection"); } } } public class running implements Runnable { @SuppressWarnings("empty-statement") public void run() { String message = ""; while(end!=true) { if(Infromserver !=null ||clientsock != null) { try { message = Infromserver.readLine(); //different class getting message from server } catch (IOException ex) { Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex); } } runF(message); } } } public void runF(String reff) { Thread r = new Thread(new refresh(reff)); r.start(); } public class refresh implements Runnable { private String ref; refresh(String re) { ref = re; } public void run() { chatMessages.setText(chatMessages.getText()+"\n"+ref); message.setText(""); } } public void sendToServer() { tosvr.println(message.getText()); } // Variables declaration - do not modify public javax.swing.JTextArea chatMessages; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton logoutButton; public javax.swing.JTextField message; private javax.swing.JButton sendButton; // End of variables declaration }PHP Code:public class Send extends javax.swing.JFrame { public Send() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jFrame1 = new javax.swing.JFrame(); jFrame2 = new javax.swing.JFrame(); jFrame3 = new javax.swing.JFrame(); jFrame4 = new javax.swing.JFrame(); exitButton = new javax.swing.JButton(); ip = new javax.swing.JTextField(); sendButton = new javax.swing.JButton(); password = new javax.swing.JPasswordField(); username = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); status = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane()); jFrame1.getContentPane().setLayout(jFrame1Layout); jFrame1Layout.setHorizontalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); jFrame1Layout.setVerticalGroup( jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 0, Short.MAX_VALUE) ); javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane()); jFrame2.getContentPane().setLayout(jFrame2Layout); jFrame2Layout.setHorizontalGroup( jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); jFrame2Layout.setVerticalGroup( jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); javax.swing.GroupLayout jFrame3Layout = new javax.swing.GroupLayout(jFrame3.getContentPane()); jFrame3.getContentPane().setLayout(jFrame3Layout); jFrame3Layout.setHorizontalGroup( jFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); jFrame3Layout.setVerticalGroup( jFrame3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); javax.swing.GroupLayout jFrame4Layout = new javax.swing.GroupLayout(jFrame4.getContentPane()); jFrame4.getContentPane().setLayout(jFrame4Layout); jFrame4Layout.setHorizontalGroup( jFrame4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); jFrame4Layout.setVerticalGroup( jFrame4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); exitButton.setText("Quit"); exitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitButtonActionPerformed(evt); } }); ip.setText("10.0.0.101"); sendButton.setText("Send"); sendButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { sendButtonActionPerformed(evt); } }); password.setText("admin"); username.setText("admin"); username.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { usernameActionPerformed(evt); } }); jLabel1.setText("Password:"); jLabel2.setText("Username:"); jLabel3.setText("IP:"); jLabel4.setText("Status:"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(28, 28, 28) .addComponent(jLabel4) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(55, 55, 55) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel3) .addComponent(jLabel2))) .addGap(4, 4, 4) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(sendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(171, 171, 171)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(username, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE) .addComponent(password, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE) .addComponent(ip, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)) .addContainerGap()))) .addGroup(layout.createSequentialGroup() .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 136, Short.MAX_VALUE) .addComponent(exitButton) .addContainerGap()))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(status, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(exitButton) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ip, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE) .addComponent(sendButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4}); pack(); }// </editor-fold> private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) { String un,pw; Chat chatt = new Chat(); un=username.getText(); pw =password.getText(); chatt.start(un,pw,ip.getText()); this.dispose(); } private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); } private void usernameActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Send().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton exitButton; private javax.swing.JTextField ip; private javax.swing.JFrame jFrame1; private javax.swing.JFrame jFrame2; private javax.swing.JFrame jFrame3; private javax.swing.JFrame jFrame4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPasswordField password; private javax.swing.JButton sendButton; private javax.swing.JLabel status; private javax.swing.JTextField username; // End of variables declaration }
- 06-17-2011, 10:56 PM #2
That's a lot of code to go through.
Can you make a small simple program that compiles and executes to demonstrate one of the problems at a time?
- 06-17-2011, 11:12 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
sure man. I will try
- 06-18-2011, 12:03 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
In the second last one(client program, chat class): it gives a nullpointererror even though it has been successfully used before
PHP Code:public void sendToServer() { tosvr.println(message.getText()); }
this should update the gui with the message but doesn't. It prints out using system.println the correct message. the exact same code is used in the server program and that updates the gui fine.PHP Code:public class running implements Runnable { @SuppressWarnings("empty-statement") public void run() { String message = ""; while(end!=true) { if(Infromserver !=null ||clientsock != null) { try { message = Infromserver.readLine(); //different class getting message from server } catch (IOException ex) { Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex); } } runF(message); } } } public void runF(String reff) { Thread r = new Thread(new refresh(reff)); r.start(); } public class refresh implements Runnable { private String ref; refresh(String re) { ref = re; } public void run() { chatMessages.setText(chatMessages.getText()+"\n"+ref); message.setText(""); } }
thats in the verify class. When i try and access anything in the object using a get method it gives out a null error:PHP Code:if(password.equals(pass[y])) //if password matches with username { temp = "true"; svri.clients[coun] = new client(2,username); //new client object }
PHP Code:public class receive implements Runnable { private int cou; receive(int cou) { this.cou=cou; } @SuppressWarnings("empty-statement") public void run() { System.out.println(cou); String user = clients[cou].getUsername(); try { while(!inFrmClient[cou].readLine().equals("logout")); { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat(time); String mes = inFrmClient[cou].readLine(); if(clients[cou].getStats() == 2) { sendToClient[cou].println(sdf.format(cal.getTime()) + "- " + user + ": " + mes); //timestamp + username + message runF(sdf.format(cal.getTime()) + "- " + user + ": " + mes); } } } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } try { clientSock[cou].close(); //closes client connection inFrmClient[cou].close(); sendToClient[cou].close(); } catch (IOException ex) { Logger.getLogger(serverInterface.class.getName()).log(Level.SEVERE, null, ex); } runF("User " + clients[cou].getUsername() + " logged out"); clients[cou].setStats(3); } }
- 06-18-2011, 12:12 AM #5
Does what you posted compile and execute and demo the problem?
The code you posted looks like bits and pieces. Need a working program to be able to test it and see the problem.
- 06-18-2011, 10:27 AM #6
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
I can't seem to replicate the gui problem
when i take the program out of context it works even though the enivironment it is running though is the same...I fixed the nullpointer error for the client class...yay
- 06-18-2011, 12:42 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
for the gui problem in chat, when I go and printout the getText it shows that it is adding the text, but not showing it when it is run
-
- 06-29-2011, 06:09 PM #9
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
no, This is my first time using multithreads. sorry for long reply, internet went down, TIA. I'm using the same parameterized thread for the server program and that works fine
- 09-28-2011, 06:55 PM #10
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
Re: multiclient/server program
well, mananged to fix the program just in time to hand it in (literally the day before). The problems were that I needed to change most of the variables to static. And the other problem i just fixed by sticking the gui and the code in one class
-
Re: multiclient/server program
- 09-30-2011, 05:17 PM #12
Member
- Join Date
- Jun 2011
- Posts
- 8
- Rep Power
- 0
Re: multiclient/server program
well, the static fixed it as the variables kept on reverting back to null. and the gui...well...i don't know. I know its 'bad programming' to have the programming within the gui class but i put another class with the gui class which seems to fix it. the chat program works with the class being seperate to the gui class but i don't know why it doesnt work for the server program. this program didn't even get me to the second round of the competition XD
- 09-30-2011, 05:19 PM #13
Re: multiclient/server program
The reason for that could be that you were creating new versions of the class instead of using the existing version.the static fixed it as the variables kept on reverting back to null
Add a println to the constructor of the class to see how often a new version of the class instance is created.
Similar Threads
-
Simple Client (2) - Server program
By Reztem in forum New To JavaReplies: 2Last Post: 01-09-2012, 02:05 AM -
MultiClient with Single Server
By prince87 in forum Threads and SynchronizationReplies: 1Last Post: 05-01-2011, 01:03 AM -
How to write two server program in java
By priyamurugar in forum NetworkingReplies: 1Last Post: 04-27-2010, 11:20 AM -
multiclient copy
By thuyduong89dt in forum NetworkingReplies: 0Last Post: 12-04-2009, 01:46 PM -
a server program that needs some help :)
By suppradyn in forum NetworkingReplies: 1Last Post: 11-13-2009, 04:44 PM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks