Error in server-client

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 838 Views - Last Post: 03 April 2013 - 06:07 PM Rate Topic: -----

#1 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Error in server-client

Posted 02 April 2013 - 03:03 PM

Hellow i have one client one server/client and one server i want to make a Vandue , i make all programm, they connection is ok and when send the username and password for authentication the user ,the eclipse send me this erro

Quote

Error with I/O processes
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Client.actionPerformed(Client.java:171)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


the error is in this part of code
if (source==login){     
        	  try{ 
        	   out.writeObject("Authedication");
               //katharizoume mnimi
               out.flush();  
             // dimiourgoume antikimeno me ta stoixima pou edose o xristis pass kai username 
             LogFile l = new LogFile(User.getText(),Pass.getText());  
            

              //instream = new ObjectInputStream(sock.getInputStream());
              
              //stelnoume to antikimeno me ta stoixia pou egine parapano ston serverAuthedication
              out.writeObject(l);
              //katharizoume mnimi
              out.flush();

              String str=null;
              Items item=null;
               // diavazoume ti mas stelni o Serverauthdication
              str=(String)instream.readObject();

                 if(str.equals("OK")) {
                	 item=(Items)instream.readObject();
                     //Se periptosi pou mas stili ok diladi iparxi o xristis tote kaloume ton 2 constractor 
                     // gia na doume to parathiro kai to  antikimeno pou dimopratite
                       Client mm =new Client(item);
                      
                }   
           }



the is communicate with this server Authentications.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import javax.swing.JOptionPane;
public class ServerAuthedication{
	
	static LogFile admin1=new LogFile("vagelis","123");
    static LogFile admin2=new LogFile("Mairi","3");
    static LogFile admin3=new LogFile("Skevi","3");
    static ArrayList<LogFile> Logs = new ArrayList<LogFile>();
    
    static Items a=new Items("Sony w320","Kinito",132);
// metavliti gia na diloso oti einai klidomeno ena arxeio
    protected boolean locked = false;
    public static void main(String[] args){
    	Logs.add(admin1);
        Logs.add(admin2);
        Logs.add(admin3);
        try{
//dimiourgia server
            int clients = 0;
            ServerSocket server = new ServerSocket(5555);
           
            System.out.println("Server Started!");
            System.out.println("Local Address: "+server.getInetAddress()+"\nPort: "+server.getLocalPort());
            System.out.println("Waiting For Connections...");
            try{
                Socket sockSe = new Socket("localhost", 5750);
                
            do{ 
// sindesi server
                
                Socket socket = server.accept();
                clients++;
                System.out.println("Client "+clients+" Connected!");
// dimiourgi neo Thread me parametro to new_job pou ousiastika einai san ton client
// Etsi mporoun etsi bori na eksipireti polous sindemenous tautoxrona client/
                ServerRun new_job = new ServerRun(socket,sockSe);
                Thread t = new Thread(new_job);
                t.start();
            }while(true);          
        }catch(IOException e){
        e.printStackTrace();	
        }
        }catch(IOException ex){
            System.out.println("IOException!");
        }
     
    }
    
    public synchronized String Authedication(Socket sock) throws ClassNotFoundException{
    	LogFile strin;
    	String name = null;
    	int flug = 0;
    	try{
    		ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    	    strin=(LogFile) in.readObject();
         
         System.out.println("The client says: \n"+strin);
         
        //psaxnoume stin  Arraylist kai elenxoume me tin if an odos iparxei o xristis 
         for(int j=0; j<Logs.size(); j++){
            
             if((strin.get_username()).equals( Logs.get(j).get_username()) && (strin.get_password()).equals( Logs.get(j).get_password()))
             {
             name=Logs.get(j).get_username();
                 // an iparexeio o xristis stin arraylist tote stelni ok ston client
             out.writeObject("OK");
             out.writeObject(a); 
             out.flush();
             // an vrethi kanoume tin simea 1
             flug=1;
             }
                 }
        //an den vrethi i sima mas meni 0 ara den vrethike kai emfanizoume minima oti den vrethike o xristis
      if(flug==0){
          JOptionPane.showMessageDialog(null, "Δεν έχετε πρόσβαση!"+"\nΠροσπαθήστε ξάνα.", "Log Authdication", JOptionPane.INFORMATION_MESSAGE );
      }
    } catch(IOException ex){
        System.out.println("IOException!");
    }
    	return name;
    
    }
/// sinartisi gia tin prosfora tou xristi
    public synchronized void Prosfora(Socket sock) throws ClassNotFoundException{
    	LogFile strin;
    	float price;
    	try{
    		ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    	    strin=(LogFile) in.readObject();
         
         System.out.println("The client says: \n"+strin);

         price=(float)in.readFloat();
         if(price<=a.get_timi()){
         	JOptionPane.showMessageDialog(null, "Προσφορά σας είναι δεχτεί!"+"\nΕυχαρηστούμε"+price, "Bid", JOptionPane.INFORMATION_MESSAGE );
         }else{
         	JOptionPane.showMessageDialog(null, "Προσφορά δεν είναι δεχτεί!"+"\nΕυχαρηστούμε."+price, "Bid", JOptionPane.INFORMATION_MESSAGE );
         } 
        }
        catch(IOException ex){
            System.out.println("IOException!");
        }
    }
// Sinartisi enimerosi
    public synchronized void Enimerosi(Socket sock,Socket sock2,String name){
        
        String number;
        Account ac;	
        try{
        	ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    		ObjectOutputStream outServer2=new ObjectOutputStream(sock2.getOutputStream());
    		ObjectInputStream inServer2 = new ObjectInputStream(sock2.getInputStream());
            while(locked==true){ // xrisimopoioume gia na apofigoume an kapoios xrisitis sindethi tautoxrona me kapoion allon ston idio logariasmo kai o enanas kani alalipsi kai o allos enimerosi
                wait(); // apofevgoume autin tin sigrousi
            }
            locked=true;
        outServer2.writeObject("Enimerosi");	
        outServer2.writeObject(name);
        outServer2.flush();
        ac=(Account)inServer2.readObject();
        out.writeObject(ac);
        out.flush();  
        locked=false;

        }
        catch(InterruptedException ex){ 
            System.out.println("InterruptedException!");
        }
        catch(IOException ex){
            System.out.println("IOException!");
        } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public synchronized void Katathesi(Socket sock,Socket sock2,String name){
    	String number;
        Account ac;	
        try{
        	ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    		ObjectOutputStream outServer2=new ObjectOutputStream(sock2.getOutputStream());
    		ObjectInputStream inServer2 = new ObjectInputStream(sock2.getInputStream());
    		
    		float money=(float)in.readFloat();	
             
             outServer2.writeObject("Katathesi");	
             outServer2.writeObject(name);
             outServer2.writeObject(money);	
             outServer2.flush();
             money=inServer2.readFloat();
             out.writeObject(money);
             out.flush();    	
        }
        catch(IOException ex){
            System.out.println("IOException!");
        }
    	
    }
    
    
}



Thanks for help

Is This A Good Question/Topic? 0
  • +

Replies To: Error in server-client

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error in server-client

Posted 02 April 2013 - 07:53 PM

It is useless to post what you call "relevant" piece of code
We need all code with the even the import statement so we can match the line numbers with the stack trace line numbers

Quote

at Client.actionPerformed(Client.java:171)

what is at line 171 ?
Was This Post Helpful? 0
  • +
  • -

#3 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 04:40 AM

I sat all night and I corrected the code as I could and I made him work partly done loogin normally and the user removes the object and data, but when I press the button offer(Προσφορα) I get this error
Exception in thread "Thread-1" java.lang.ClassCastException: java.lang.String cannot be cast to LogFile
at ServerAuthedication.run(ServerAuthedication.java:64)
at java.lang.Thread.run(Unknown Source)
java.net.SocketException: Connection reset

Server code
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JOptionPane;


public class ServerAuthedication implements Runnable {
	
	static LogFile admin1=new LogFile("vagelis","123");
    static LogFile admin2=new LogFile("Mairi","3");
    static LogFile admin3=new LogFile("Skevi","3");
    static ArrayList<LogFile> Logs = new ArrayList<LogFile>();
    
    static Items a=new Items("Sony w320","Kinito",132);
// metavliti gia na diloso oti einai klidomeno ena arxeio
    
    Socket sock;
    ServerAuthedication(Socket sock) {
       this.sock = sock;
    }
    public static void main(String[] args){
    	Logs.add(admin1);
        Logs.add(admin2);
        Logs.add(admin3);
        
//dimiourgia server
            int clients = 0;
            ServerSocket server;
			try { 
            	server = new ServerSocket(5555);
         
                System.out.println("Server Started!");
                System.out.println("Local Address: "+server.getInetAddress()+"\nPort: "+server.getLocalPort());
                System.out.println("Waiting For Connections...");
                
                while (true) {
                   Socket sock = server.accept();
                   System.out.println("Connected");
                   new Thread(new ServerAuthedication(sock)).start();}
            
			} catch (IOException ex) {
				System.out.println(ex);
			}
     
			
			
			
			
			
    }
	@Override
	public void run() {
		    String incoming;
			LogFile strin;
	    	String name = null;
	    	int flug = 0;
	    	try{
	    		ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    	    strin=(LogFile) in.readObject();
	         
	         System.out.println("The client says: \n"+strin);
	         
	        //psaxnoume stin  Arraylist kai elenxoume me tin if an odos iparxei o xristis 
	         for(int j=0; j<Logs.size(); j++){
	            
	             if((strin.get_username()).equals( Logs.get(j).get_username()) && (strin.get_password()).equals( Logs.get(j).get_password()))
	             {
	             name=Logs.get(j).get_username();
	                 // an iparexeio o xristis stin arraylist tote stelni ok ston client
	             out.writeObject("OK");
	             out.writeObject(a); 
	             out.flush();
	             // an vrethi kanoume tin simea 1
	             flug=1;
	             }
	                 }
	        //an den vrethi i sima mas meni 0 ara den vrethike kai emfanizoume minima oti den vrethike o xristis
	      if(flug==0){
	          JOptionPane.showMessageDialog(null, "Δεν έχετε πρόσβαση!"+"\nΠροσπαθήστε ξάνα.", "Log Authdication", JOptionPane.INFORMATION_MESSAGE );
	      }
	      
	      
          do{
              incoming =(String) in.readObject();
              switch(incoming){
                  case "Prosfora":
                      Prosfora(sock);
                  break;
                  case "Enimerosi":
                //      Enimerosi(sock,name);
                      break;
                  case "Katathesi":
               //   	Katathesi(sock,name);
                 break;
              }
// otan to incoming einai Exit tote telos epanalipsis
          }while(!incoming.equals("EXIT"));
	      
	      
	      
	      out.close();
	      sock.close();
	      }
	      catch (IOException e) {
	         System.out.println(e);
	      } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
    
    
	
	  public synchronized void Prosfora(Socket sock) throws ClassNotFoundException{
	    	LogFile strin;
	    	float price;
	    	try{
	    		ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    	    strin=(LogFile) in.readObject();
	         
	         System.out.println("The client says: \n"+strin);

	         price=(float)in.readFloat();
	         if(price<=a.get_timi()){
	         	JOptionPane.showMessageDialog(null, "Προσφορά σας είναι δεχτεί!"+"\nΕυχαρηστούμε"+price, "Bid", JOptionPane.INFORMATION_MESSAGE );
	         }else{
	         	JOptionPane.showMessageDialog(null, "Προσφορά δεν είναι δεχτεί!"+"\nΕυχαρηστούμε."+price, "Bid", JOptionPane.INFORMATION_MESSAGE );
	         } 
	        }
	        catch(IOException ex){
	            System.out.println("IOException!");
	        }
	    }
	// Sinartisi enimerosi
	    public synchronized void Enimerosi(Socket sock,Socket sock2,String name){
	        boolean locked=false;
	        String number;
	        Account ac;	
	        try{
	        	ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    		ObjectOutputStream outServer2=new ObjectOutputStream(sock2.getOutputStream());
	    		ObjectInputStream inServer2 = new ObjectInputStream(sock2.getInputStream());
	            while(locked==true){ // xrisimopoioume gia na apofigoume an kapoios xrisitis sindethi tautoxrona me kapoion allon ston idio logariasmo kai o enanas kani alalipsi kai o allos enimerosi
	                wait(); // apofevgoume autin tin sigrousi
	            }
	            locked=true;
	        outServer2.writeObject("Enimerosi");	
	        outServer2.writeObject(name);
	        outServer2.flush();
	        ac=(Account)inServer2.readObject();
	        out.writeObject(ac);
	        out.flush();  
	        locked=false;

	        }
	        catch(InterruptedException ex){ 
	            System.out.println("InterruptedException!");
	        }
	        catch(IOException ex){
	            System.out.println("IOException!");
	        } catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
	    public synchronized void Katathesi(Socket sock,Socket sock2,String name){
	    	String number;
	        Account ac;	
	        try{
	        	ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    		ObjectOutputStream outServer2=new ObjectOutputStream(sock2.getOutputStream());
	    		ObjectInputStream inServer2 = new ObjectInputStream(sock2.getInputStream());
	    		
	    		float money=(float)in.readFloat();	
	             
	             outServer2.writeObject("Katathesi");	
	             outServer2.writeObject(name);
	             outServer2.writeObject(money);	
	             outServer2.flush();
	             money=inServer2.readFloat();
	             out.writeObject(money);
	             out.flush();    	
	        }
	        catch(IOException ex){
	            System.out.println("IOException!");
	        }  
    
}
}


client code
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Client extends JFrame implements ActionListener{
   //arxikopoioume ta antikimena mas
    // ta thetoume null
    Socket sock = null;
    //roi antikimenon pros ai apo ton serkverutAhedication
    ObjectOutputStream out=null;
    ObjectInputStream instream=null;
    //BufferedReader fromtype=null;
//dimiourgia tou panel-koubion kai lebel kai textfields
JPanel row1=new JPanel();
JLabel User_Label=new JLabel("Username :", JLabel.LEFT);
JTextField User = new JTextField(20);

JPanel row2=new JPanel();
JLabel Pass_Label=new JLabel("Password :", JLabel.LEFT);
JTextField Pass = new JTextField(20);


JPanel row7=new JPanel();
JButton login=new JButton("Log In");


JPanel row8=new JPanel();
JLabel Onoma_Label=new JLabel("Όνομα :", JLabel.LEFT);
JTextField Onoma = new JTextField(20);

JPanel row9=new JPanel();
JLabel Tipos_Label=new JLabel("Είδος :", JLabel.LEFT);
JTextField Tipos = new JTextField(20);

JPanel row10=new JPanel();
JLabel Timi_Label=new JLabel("Τιμή :", JLabel.LEFT);
JTextField Timi = new JTextField(5);

JPanel row11=new JPanel();
JLabel  Prosfora_Label=new JLabel("Προσφορα/Κατάθεση :", JLabel.LEFT);
JTextField Prosfora = new JTextField(15);


//dimiourgia ton koubion
JPanel row12=new JPanel();
JButton katathesi=new JButton("Κατάθεση");
JButton enimerosi=new JButton("Ενημέρωση");
JButton prosfora=new JButton("Προσφορά");

JPanel row13=new JPanel();
JButton akuro=new JButton("ΑΚΥΡΟ");
//orizoume 1 constractor gia to parathiro pou tha kanei tin tautopiisi tou xristi
public Client()  {

    //Titlos
    super("Log in");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Thetoume to parathiro na einai orato
    setVisible(true);
    
    Container pane = getContentPane();

    //kathorismos gramon kai stilon  Container.
    GridLayout layout=new GridLayout(3,2);
    pane.setLayout(layout);
    //eisagoume ta adikimena
    pane.add(User_Label);
    pane.add(User);
    pane.add(Pass_Label);
    pane.add(Pass);
    pane.add(login);
    setContentPane(pane);
    // prosarmozi ta adikimena sto parathiro
    pack();
    //dinoume energia sto koubi ha ilopoisoume stin sinartisi Actionlistener.t 
    login.addActionListener(this);

}//telos 1 constactor.
 // 2 konstractor pou tha emfanizete otan ginete i taautisi stixion gia na vlepi antikimeno pou dimopratite
public Client(Items item)  {
     //Titlos
    super("Vandue");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Thetoume to parathiro na einai orato
    setVisible(true);
    
    Container pane = getContentPane();

    //kathorismos gramon kai stilon  Container.
    GridLayout layout=new GridLayout(6,3);
    pane.setLayout(layout);
    //eisagoume ta adikimena
    pane.add(Onoma_Label);
    pane.add(Onoma);
    pane.add(Tipos_Label);
    pane.add(Tipos);
    pane.add(Timi_Label);
    pane.add(Timi);
    pane.add(Prosfora_Label);
    pane.add(Prosfora);
    pane.add(katathesi);
    pane.add(enimerosi);
    pane.add(akuro);
    pane.add(prosfora);

    setContentPane(pane);
    // prosarmozi ta adikimena sto parathiro
    pack();
    Onoma.setText(item.get_onoma());
    Tipos.setText(item.get_tipos());
    Timi.setText(Integer.toString(item.get_timi()));
    
  //dinoume energia sto koubi ha ilopoisoume stin sinartisi Actionlistener.t 
    enimerosi.addActionListener(this);
    katathesi.addActionListener(this);
    prosfora.addActionListener(this);
    akuro.addActionListener(this);
}// telos 1 constractor

 //ilopoiisi tis action listener , ti tha kanei to kathe koubi

   public void actionPerformed(ActionEvent evt) {
        //pernoume tin energia apo to koubi, poio koubi patisame
         Object source = evt.getSource();
          //anoume connect me ton ServerAuthedication gia na tou stoiloume ta stoixia kai na doume an iparxei stin lista o xristis
          
          try {
     //me to sock epikinonoume me ton serverAuthdication
            sock = new Socket("localhost", 5555);
            // roi pou stelni o client ston server1i
            out=new ObjectOutputStream(sock.getOutputStream());
            instream = new ObjectInputStream(sock.getInputStream());
            
            } catch (UnknownHostException ex) {
            ex.printStackTrace();

            } catch (IOException ex) {
            ex.printStackTrace();

        }

           //otan patisoume to koubi login

           if (source==login){     
        	  try{ 
        	//   out.writeObject("Authedication");
               //katharizoume mnimi
             //  out.flush();  
             // dimiourgoume antikimeno me ta stoixima pou edose o xristis pass kai username 
             LogFile l = new LogFile(User.getText(),Pass.getText());  
            

              //instream = new ObjectInputStream(sock.getInputStream());
              
              //stelnoume to antikimeno me ta stoixia pou egine parapano ston serverAuthedication
              out.writeObject(l);
              //katharizoume mnimi
              out.flush();

              String str=null;
              Items item=null;
               // diavazoume ti mas stelni o Serverauthdication
              str=(String)instream.readObject();

                 if(str.equals("OK")) {
                	 item=(Items)instream.readObject();
                     //Se periptosi pou mas stili ok diladi iparxi o xristis tote kaloume ton 2 constractor 
                     // gia na doume to parathiro kai to  antikimeno pou dimopratite
                       Client mm =new Client(item);
                      
                }   
           }
        catch (IOException ex) {
        System.out.println("Error with I/O processes") ;
        ex.printStackTrace();
                        } catch (ClassNotFoundException ex) {
                 Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
             }
        }//telos tou login

            if(source==enimerosi){
            	try{
            	out.writeObject("Enimerosi");
                //katharizoume mnimi
                out.flush();
            	Account ac=null;
            	
            		out.writeObject("Enimerosi");
            		out.flush();
            	  ac=(Account)instream.readObject();
            	  JOptionPane.showMessageDialog(null, "Ο λογαριασμός σας !"+"\n"+ac.toString(), "Account ", JOptionPane.INFORMATION_MESSAGE );	
            	}catch(NumberFormatException ex){
            		System.out.println("Error give wrong type of I/O!!!!") ;
                    ex.printStackTrace();
            	}catch (IOException ex) {
            		   System.out.println("Error with I/O processes") ;
            		   ex.printStackTrace();
            	} catch (ClassNotFoundException ex) {
            		Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
					ex.printStackTrace();
				}   
            	
          	  
               }
       
            
   if (source==katathesi){
	   try{ 
	   out.writeObject("Katathesi");
       //katharizoume mnimi
       out.flush();
     float money;
	  
			 money=Integer.parseInt(Prosfora.getText()); 
			 out.writeObject(money);
			 out.flush();	
			// instream = new ObjectInputStream(sock.getInputStream());
       	  money=instream.readFloat();
       	  JOptionPane.showMessageDialog(null, "Το νεο υπολύπόσας !"+"\n"+money, "Account ", JOptionPane.INFORMATION_MESSAGE );	
			}catch(NumberFormatException ex){
				System.out.println("Error give wrong type of I/O!!!!") ;
		        ex.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   JOptionPane.showMessageDialog(null, "Δεν έχετε πρόσβαση!"+"\nΠροσπαθήστε ξάνα.", "Log Authdication", JOptionPane.INFORMATION_MESSAGE );
     }
   
if(source==prosfora){
	try{
	float price=0;
		out.writeObject("Prosfora");
		out.flush();
	  try{
	 price=Integer.parseInt(Prosfora.getText()); 
	 out.writeObject(price);
	 out.flush();	
	}catch(NumberFormatException ex){
		System.out.println("Error give wrong type of I/O!!!!") ;
        ex.printStackTrace();
	}}catch (IOException ex) {
		   System.out.println("Error with I/O processes") ;
		   ex.printStackTrace();
		                   }   
}
         //kathorismos-ilopoiisi koubiou akuro.

if(source==akuro){
	try {
		out.writeObject("EXIT");
		out.flush();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 	
      //methodos exit(0) klini to GUI.
        System.exit(0);
            try {
                 //kleini tin sindesi me ton authedication
                sock.close();
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
  }

}//telos actionPerformed

    public static void main(String[] args) {
  
       Client c=new Client();
    }

}


Was This Post Helpful? 0
  • +
  • -

#4 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Error in server-client

Posted 03 April 2013 - 04:48 AM

strin=(LogFile) in.readObject(); is the source of your error.

Apparently, the string object read in cannot be cast to a LogFile.
Was This Post Helpful? 1
  • +
  • -

#5 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 04:57 AM

View PostFlukeshot, on 03 April 2013 - 04:48 AM, said:

strin=(LogFile) in.readObject(); is the source of your error.

Apparently, the string object read in cannot be cast to a LogFile.


I have make a Class logFile with username and passward , in code send an item logfile and read it and cast it in logfile,
i not send a string .
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Error in server-client

Posted 03 April 2013 - 05:06 AM

Correct, you aren't sending a string, but you try to cast a string to this Object type. This happens before you 'send' anything.

This isn't coming from me, it's coming from the compiler. I suggest you find an alternative to a cast, to achieve what you are trying to.

Edit: more info.

Perhaps redesign your LogFile class to accept a string as a parameter, then use some string gynmastics to get your end result.

This post has been edited by Flukeshot: 03 April 2013 - 05:09 AM

Was This Post Helpful? 0
  • +
  • -

#7 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 05:16 AM

heare is the class LogFile
what i can do ?
import java.io.Serializable;

/**
 *
 * @author Vagos
 */
public class LogFile implements Serializable{

    private String username;
    private String password;


    public LogFile(String username,String password){
   this.username=username;
   this.password=password;
   }

  public void set_username(String username){this.username=username;}
  public String get_username(){return username;}

  public void set_password(String password){this.password=password;}
  public String get_password(){return password;}

   public String toString(){

        return "Username: "+this.username+"\nPassword: "+this.password+"\n ";
    }
}

Was This Post Helpful? 0
  • +
  • -

#8 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Error in server-client

Posted 03 April 2013 - 05:26 AM

Then you are already setup to take 2 strings as parameters. You could read the data in via in.nextLine() and then split it into an array delimited by space and use index 0 and 1 as the first 2 "words".

This is not a robust solution as you would have to specify a size for the array which could lead to NullPointerExceptions if it's too large or ArrayIndexOutOfBounds exceptions if it's too small.

So, you could find a way for this to work with an dynamic collection, such as a list.

What would I do?
Practice with the first way - make it work if the conditions are right, then improve on it to account for user-stupidity.
Was This Post Helpful? 1
  • +
  • -

#9 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 07:08 AM

I have make it ai send as string the name and the password and make the loging code a methode and now give this error
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Client.actionPerformed(Client.java:140)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Client.actionPerformed(Client.java:165)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Error with I/O processes
server
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.swing.JOptionPane;


public class ServerAuthedication implements Runnable {
	static ArrayList<LogFile> Logs = new ArrayList<LogFile>();
        
    static Items a=new Items("Sony w320","Kinito",132);
    Socket sock;
    ServerAuthedication(Socket sock) {
       this.sock = sock;
    }
    public static void main(String[] args){
     
    	
       LogFile admin1=new LogFile("vagelis","123");
       LogFile admin2=new LogFile("Mairi","3");
       
    	Logs.add(admin1);
        Logs.add(admin2);
  
        
//dimiourgia server
            int clients = 0;
            ServerSocket server;
			try { 
            	server = new ServerSocket(5555);
         
                System.out.println("Server Started!");
                System.out.println("Local Address: "+server.getInetAddress()+"\nPort: "+server.getLocalPort());
                System.out.println("Waiting For Connections...");
                
                while (true) {
                   Socket sock = server.accept();
                   System.out.println("Connected");
                   new Thread(new ServerAuthedication(sock)).start();}
             
			} catch (IOException ex) {
				System.out.println(ex);
			}
			
			
			
			
			
    		
    }
	@Override
	public void run() {
		    String incoming;
	    	try{
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    		do{
              incoming =(String) in.readObject();
              switch(incoming){
              
              case " Prosfora":
                      Prosfora(sock);
                        break;
                  case "Enimerosi":
                //      Enimerosi(sock);
                      break;
                  case "Katathesi":
               //(sock,name);
                 break;
              }
// otan to incoming einai Exit tote telos epanalipsis
          }while(!incoming.equals("EXIT"));
	      
	      
	      
	      in.close();
	      sock.close();
	      }
	      catch (IOException e) {
	         System.out.println(e);
	      } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
    
	 public synchronized void Authedication(Socket sock) throws ClassNotFoundException{
		 String usn,pwd;
			
	    	int flug = 0;
	    	try{
	    		ObjectOutputStream out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    	    usn=(String) in.readObject();
	    	    pwd=(String) in.readObject();
	         System.out.println("The client says: \n"+usn+"\n"+pwd);
	         
	        //psaxnoume stin  Arraylist kai elenxoume me tin if an odos iparxei o xristis 
	         for(int j=0; j<Logs.size(); j++){
	            
	             if((usn.equals( Logs.get(j).get_username())) && (pwd.equals( Logs.get(j).get_password())))
	             {
	             name=Logs.get(j).get_username();
	                 // an iparexeio o xristis stin arraylist tote stelni ok ston client
	             out.writeObject("OK");
	             out.writeObject(a); 
	             out.flush();
	             // an vrethi kanoume tin simea 1
	             flug=1;
	             }
	                 }
	        //an den vrethi i sima mas meni 0 ara den vrethike kai emfanizoume minima oti den vrethike o xristis
	      if(flug==0){
	          JOptionPane.showMessageDialog(null, "Δεν έχετε πρόσβαση!"+"\nΠροσπαθήστε ξάνα.", "Log Authdication", JOptionPane.INFORMATION_MESSAGE );
	      }} catch(IOException ex){
	        System.out.println("IOException!");
	    }
	    
	    
	    }
    
	
	  public synchronized void Prosfora(Socket sock) throws ClassNotFoundException{
	    	LogFile strin;
	    	float price;
	    	try{
	    		ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    	    strin=(LogFile) in.readObject();
	         
	         System.out.println("The client says: \n"+strin);

	         price=(float)in.readFloat();
	         if(price<=a.get_timi()){
	         	JOptionPane.showMessageDialog(null, "Προσφορά σας είναι δεχτεί!"+"\nΕυχαρηστούμε"+price, "Bid", JOptionPane.INFORMATION_MESSAGE );
	         }else{
	         	JOptionPane.showMessageDialog(null, "Προσφορά δεν είναι δεχτεί!"+"\nΕυχαρηστούμε."+price, "Bid", JOptionPane.INFORMATION_MESSAGE );
	         } 
	        }
	        catch(IOException ex){
	            System.out.println("IOException!");
	        }
	    }
	// Sinartisi enimerosi
	    public synchronized void Enimerosi(Socket sock,Socket sock2,String name){
	        boolean locked=false;
	        String number;
	        Account ac;	
	        try{
	        	ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    		ObjectOutputStream outServer2=new ObjectOutputStream(sock2.getOutputStream());
	    		ObjectInputStream inServer2 = new ObjectInputStream(sock2.getInputStream());
	            while(locked==true){ // xrisimopoioume gia na apofigoume an kapoios xrisitis sindethi tautoxrona me kapoion allon ston idio logariasmo kai o enanas kani alalipsi kai o allos enimerosi
	                wait(); // apofevgoume autin tin sigrousi
	            }
	            locked=true;
	        outServer2.writeObject("Enimerosi");	
	        outServer2.writeObject(name);
	        outServer2.flush();
	        ac=(Account)inServer2.readObject();
	        out.writeObject(ac);
	        out.flush();  
	        locked=false;

	        }
	        catch(InterruptedException ex){ 
	            System.out.println("InterruptedException!");
	        }
	        catch(IOException ex){
	            System.out.println("IOException!");
	        } catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    
	    public synchronized void Katathesi(Socket sock,Socket sock2,String name){
	    	String number;
	        Account ac;	
	        try{
	        	ObjectOutputStream	 out=new ObjectOutputStream(sock.getOutputStream());
	    		ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
	    		ObjectOutputStream outServer2=new ObjectOutputStream(sock2.getOutputStream());
	    		ObjectInputStream inServer2 = new ObjectInputStream(sock2.getInputStream());
	    		
	    		float money=(float)in.readFloat();	
	             
	             outServer2.writeObject("Katathesi");	
	             outServer2.writeObject(name);
	             outServer2.writeObject(money);	
	             outServer2.flush();
	             money=inServer2.readFloat();
	             out.writeObject(money);
	             out.flush();    	
	        }
	        catch(IOException ex){
	            System.out.println("IOException!");
	        }  
    
}
}

client
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Client extends JFrame implements ActionListener{
   //arxikopoioume ta antikimena mas
    // ta thetoume null
    Socket sock = null;
    //roi antikimenon pros ai apo ton serkverutAhedication
    ObjectOutputStream out=null;
    ObjectInputStream instream=null;
    //BufferedReader fromtype=null;
//dimiourgia tou panel-koubion kai lebel kai textfields
JPanel row1=new JPanel();
JLabel User_Label=new JLabel("Username :", JLabel.LEFT);
JTextField User = new JTextField(20);

JPanel row2=new JPanel();
JLabel Pass_Label=new JLabel("Password :", JLabel.LEFT);
JTextField Pass = new JTextField(20);


JPanel row7=new JPanel();
JButton login=new JButton("Log In");


JPanel row8=new JPanel();
JLabel Onoma_Label=new JLabel("Όνομα :", JLabel.LEFT);
JTextField Onoma = new JTextField(20);

JPanel row9=new JPanel();
JLabel Tipos_Label=new JLabel("Είδος :", JLabel.LEFT);
JTextField Tipos = new JTextField(20);

JPanel row10=new JPanel();
JLabel Timi_Label=new JLabel("Τιμή :", JLabel.LEFT);
JTextField Timi = new JTextField(5);

JPanel row11=new JPanel();
JLabel  Prosfora_Label=new JLabel("Προσφορα/Κατάθεση :", JLabel.LEFT);
JTextField Prosfora = new JTextField(15);


//dimiourgia ton koubion
JPanel row12=new JPanel();
JButton katathesi=new JButton("Κατάθεση");
JButton enimerosi=new JButton("Ενημέρωση");
JButton prosfora=new JButton("Προσφορά");

JPanel row13=new JPanel();
JButton akuro=new JButton("ΑΚΥΡΟ");
//orizoume 1 constractor gia to parathiro pou tha kanei tin tautopiisi tou xristi
public Client()  {

    //Titlos
    super("Log in");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Thetoume to parathiro na einai orato
    setVisible(true);
    
    Container pane = getContentPane();

    //kathorismos gramon kai stilon  Container.
    GridLayout layout=new GridLayout(3,2);
    pane.setLayout(layout);
    //eisagoume ta adikimena
    pane.add(User_Label);
    pane.add(User);
    pane.add(Pass_Label);
    pane.add(Pass);
    pane.add(login);
    setContentPane(pane);
    // prosarmozi ta adikimena sto parathiro
    pack();
    //dinoume energia sto koubi ha ilopoisoume stin sinartisi Actionlistener.t 
    login.addActionListener(this);

}//telos 1 constactor.
 // 2 konstractor pou tha emfanizete otan ginete i taautisi stixion gia na vlepi antikimeno pou dimopratite
public Client(Items item)  {
     //Titlos
    super("Vandue");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Thetoume to parathiro na einai orato
    setVisible(true);
    
    Container pane = getContentPane();

    //kathorismos gramon kai stilon  Container.
    GridLayout layout=new GridLayout(6,3);
    pane.setLayout(layout);
    //eisagoume ta adikimena
    pane.add(Onoma_Label);
    pane.add(Onoma);
    pane.add(Tipos_Label);
    pane.add(Tipos);
    pane.add(Timi_Label);
    pane.add(Timi);
    pane.add(Prosfora_Label);
    pane.add(Prosfora);
    pane.add(katathesi);
    pane.add(enimerosi);
    pane.add(akuro);
    pane.add(prosfora);

    setContentPane(pane);
    // prosarmozi ta adikimena sto parathiro
    pack();
    Onoma.setText(item.get_onoma());
    Tipos.setText(item.get_tipos());
    Timi.setText(Integer.toString(item.get_timi()));
    
  //dinoume energia sto koubi ha ilopoisoume stin sinartisi Actionlistener.t 
    enimerosi.addActionListener(this);
    katathesi.addActionListener(this);
    prosfora.addActionListener(this);
    akuro.addActionListener(this);
}// telos 1 constractor

 //ilopoiisi tis action listener , ti tha kanei to kathe koubi

   public void actionPerformed(ActionEvent evt) {
        //pernoume tin energia apo to koubi, poio koubi patisame
         Object source = evt.getSource();
          //anoume connect me ton ServerAuthedication gia na tou stoiloume ta stoixia kai na doume an iparxei stin lista o xristis
          
          try {
     //me to sock epikinonoume me ton serverAuthdication
            sock = new Socket("localhost", 5555);
            // roi pou stelni o client ston server1i
            out=new ObjectOutputStream(sock.getOutputStream());
            instream = new ObjectInputStream(sock.getInputStream());
            
            } catch (UnknownHostException ex) {
            ex.printStackTrace();

            } catch (IOException ex) {
            ex.printStackTrace();

        }

           //otan patisoume to koubi login

           if (source==login){     
        	   String str=null;
              Items item=null;
        	   try{ 
        	
                    
        		   
             LogFile l = new LogFile(User.getText(),Pass.getText());  
            

              //instream = new ObjectInputStream(sock.getInputStream());
              
              //stelnoume to antikimeno me ta stoixia pou egine parapano ston serverAuthedication
              out.writeObject(User.getText());
              out.writeObject(Pass.getText());
              //katharizoume mnimi
              out.flush();

             
               // diavazoume ti mas stelni o Serverauthdication
              str=(String)instream.readObject();

                 if(str.equals("OK")) {
                	 item=(Items)instream.readObject();
                     //Se periptosi pou mas stili ok diladi iparxi o xristis tote kaloume ton 2 constractor 
                     // gia na doume to parathiro kai to  antikimeno pou dimopratite
                       Client mm =new Client(item);
                      
                }   
           }
        catch (IOException ex) {
        System.out.println("Error with I/O processes") ;
        ex.printStackTrace();
                        } catch (ClassNotFoundException ex) {
                 Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
             }
        }//telos tou login

            if(source==enimerosi){
            	try{
            	out.writeObject("Enimerosi");
                //katharizoume mnimi
                out.flush();
            	Account ac=null;
            	
            		out.writeObject("Enimerosi");
            		out.flush();
            	  ac=(Account)instream.readObject();
            	  JOptionPane.showMessageDialog(null, "Ο λογαριασμός σας !"+"\n"+ac.toString(), "Account ", JOptionPane.INFORMATION_MESSAGE );	
            	}catch(NumberFormatException ex){
            		System.out.println("Error give wrong type of I/O!!!!") ;
                    ex.printStackTrace();
            	}catch (IOException ex) {
            		   System.out.println("Error with I/O processes") ;
            		   ex.printStackTrace();
            	} catch (ClassNotFoundException ex) {
            		Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
					ex.printStackTrace();
				}   
            	
          	  
               }
       
            
   if (source==katathesi){
	   try{ 
	   out.writeObject("Katathesi");
       //katharizoume mnimi
       out.flush();
     float money;
	  
			 money=Integer.parseInt(Prosfora.getText()); 
			 out.writeObject(money);
			 out.flush();	
			// instream = new ObjectInputStream(sock.getInputStream());
       	  money=instream.readFloat();
       	  JOptionPane.showMessageDialog(null, "Το νεο υπολύπόσας !"+"\n"+money, "Account ", JOptionPane.INFORMATION_MESSAGE );	
			}catch(NumberFormatException ex){
				System.out.println("Error give wrong type of I/O!!!!") ;
		        ex.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   JOptionPane.showMessageDialog(null, "Δεν έχετε πρόσβαση!"+"\nΠροσπαθήστε ξάνα.", "Log Authdication", JOptionPane.INFORMATION_MESSAGE );
     }
   
if(source==prosfora){
	try{
	float price=0;
		out.writeObject("Prosfora");
		out.flush();
	  try{
	 price=Integer.parseInt(Prosfora.getText()); 
	 out.writeObject(price);
	 out.flush();	
	}catch(NumberFormatException ex){
		System.out.println("Error give wrong type of I/O!!!!") ;
        ex.printStackTrace();
	}}catch (IOException ex) {
		   System.out.println("Error with I/O processes") ;
		   ex.printStackTrace();
		                   }   
}
         //kathorismos-ilopoiisi koubiou akuro.

if(source==akuro){
	try {
		out.writeObject("EXIT");
		out.flush();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 	
      //methodos exit(0) klini to GUI.
        System.exit(0);
            try {
                 //kleini tin sindesi me ton authedication
                sock.close();
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
  }

}//telos actionPerformed

    public static void main(String[] args) {
  
       Client c=new Client();
    }

}


Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error in server-client

Posted 03 April 2013 - 10:37 AM

You should read a LogFile object from which you'll extrract the username and password instance variable.
Right now, at line 171 you try to cast that LogFile object into a String
Was This Post Helpful? 0
  • +
  • -

#11 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 11:01 AM

View Postpbl, on 03 April 2013 - 10:37 AM, said:

You should read a LogFile object from which you'll extrract the username and password instance variable.
Right now, at line 171 you try to cast that LogFile object into a String


yea beacase i send i String here is the line i client i send
out.writeObject("OK");
out.writeObject(a);


and the line 171 in server is castin a Stirng which read from client is OK this is read the server in 171
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error in server-client

Posted 03 April 2013 - 11:16 AM

This is my Client/Server application, like in my Client/Server Chatroom Tutorial, when I do that type of things
I always send the same type of Object over the wire like a Message

class Message implements Serializable{
    public static final int MSG_LOGIN = 1, MSG_OTHER = 2, .....;
    int type;
    String username;
    String password;
    String otherStuff;


    Message(String username, String password) {
       this.username = username;
       this.password = password;
       type = MSG_LOGIN;
   }
}



and on the other side you can do

    Message msg = (Message) inputStream.readObject();
    switch(msg.type) {
       case Message.MSG_LOGIN:
         ...
         break;

       case Message.MSG_OTHER:
         ...
         break;

       case Message.MSG_xxx:


This post has been edited by pbl: 03 April 2013 - 12:29 PM
Reason for edit:: gorgot implement Serializable

Was This Post Helpful? 0
  • +
  • -

#13 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 12:34 PM

View Postpbl, on 03 April 2013 - 11:16 AM, said:

This is my Client/Server application, like in my Client/Server Chatroom Tutorial, when I do that type of things
I always send the same type of Object over the wire like a Message

class Message implements Serializable{
    public static final int MSG_LOGIN = 1, MSG_OTHER = 2, .....;
    int type;
    String username;
    String password;
    String otherStuff;


    Message(String username, String password) {
       this.username = username;
       this.password = password;
       type = MSG_LOGIN;
   }
}



and on the other side you can do

    Message msg = (Message) inputStream.readObject();
    switch(msg.type) {
       case Message.MSG_LOGIN:
         ...
         break;

       case Message.MSG_OTHER:
         ...
         break;

       case Message.MSG_xxx:





i try it but i have the same stack after the loogin in the sent ok is stak ...

This post has been edited by Vagosm: 03 April 2013 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Error in server-client

Posted 03 April 2013 - 12:55 PM

You are sure that you only receive and send (both Server and Client) instances of Message (or whatever you call it) objects ?
Was This Post Helpful? 0
  • +
  • -

#15 Vagosm  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 91
  • Joined: 15-May 12

Re: Error in server-client

Posted 03 April 2013 - 01:41 PM

View Postpbl, on 03 April 2013 - 12:55 PM, said:

You are sure that you only receive and send (both Server and Client) instances of Message (or whatever you call it) objects ?


Yes i cant make it and i dont understand why is stack....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2