Connection works but program does not continue in the code ?

Hello,
I have a client, a server and a data class implementing Serializable interface.
Both classes communicate via sockets.
Between the sockets I transfer objects from the type of the data class.
The client and the server are running each in a Thread.
First the server is started then pressing a certain button in the GUI the client is started.
1. Why is the code in the clients Thread never going beyond this code line: System.out.println("test"); ???
2. Why is the file satz.dat not written ?
This is the part code of all 3 classes which is making me trouble:
Client code:
public class ClientThread extends Thread
       ClientThread()
       public void run()
            try
                 InetAddress ip = InetAddress.getByName("localhost");                 
                 Socket socket = new Socket(ip , ServerThread.PORT); 
                 System.out.println("test");
                 ObjectInputStream incomingObject = new ObjectInputStream(socket.getInputStream());             
                 ObjectOutputStream outgoingObject = new ObjectOutputStream(socket.getOutputStream());                    
                  outgoingObject.writeObject(serializeObjekt(meineBuchdaten)); // serialize the object "meineBuchdaten"
                  incomingObject.close();
                  outgoingObject.close();
                  socket.close();              
            catch (Exception e)
                  e.printStackTrace();                
Method to serialize the string data of the data class called Buchdaten class:
public Object serializeObjekt(Object objekt) throws IOException
          ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/satz.dat")));
         oos.writeObject(objekt);
         oos.flush();
         oos.close(); 
         return oos;
server class:
public class ServerThread extends Thread
     public static final int PORT = 8080;
     private ServerSocket myServerSocket = null;
     private Socket myClientSocket;     
     private Database myDatabase = new Database();     
     public ServerThread()
    public void run()
          try
               myServerSocket = new ServerSocket(PORT);               
          catch (IOException e)
               e.printStackTrace();
          System.out.println("Started: " + myServerSocket);          
          try
               while(true)
                    // Warten until the client connects...               
                    myClientSocket = myServerSocket.accept();     
                    System.out.println("Connection done - handshake " + myClientSocket);
                    ObjectInputStream incomingObject = new ObjectInputStream(myClientSocket.getInputStream());
                 ObjectOutputStream outgoingObject = new ObjectOutputStream(myClientSocket.getOutputStream());                    
                    Buchdaten bd = (Buchdaten) incomingObject.readObject();
                    System.out.println("This should be the deserialized data: " + bd);
                    myClientSocket.close();                    
          catch(Exception ex)
               System.out.println(ex.getMessage());
data class:
public class Buchdaten implements Serializable
     private static final long serialVersionUID = 1L;
     private String autor;
     private String titel;
     private String jahr;
     private String verlag;
     private int number;
     private int id;
     public Buchdaten()
     public void setDataToArray(String autor, String titel, String jahr, String verlag , int number)
       this.autor = autor;
       this.titel = titel;
       this.jahr  = jahr;
       this.verlag = verlag;
       this.number = number;     
     public void setDataToArray(int number , int id)
       this.number = number;     
       this.id = id;
     public void setDataToArray(String autor, String titel, String jahr, String verlag , int number , int id)
       this.autor = autor;
       this.titel = titel;
       this.jahr  = jahr;
       this.verlag = verlag;
       this.number = number;     
       this.id = id;
}

>
Oh, and by the way, is there a reason you aren't just using normal java RMI?
Edited by: jtahlborn on Feb 1, 2008 9:34 PMOh, and by the way, is there a reason you aren't just using normal java RMI?
yes for now i have to do it this way. The app must only run on my home pc but later i have to do it with RMI, but first it must work with sockets and streams etc stupd i know... ;-)
sabre150: quote:"As with all two way communication, one thread should be used for writing and another for reading. This ways the blocking nature of the streams works for and not against."
0: Does that mean i have to open 4 threads ? 2 threads for the client class and 2 threads for the server class? each one has an ObjectInput/Output - stream right?
For now i have only opened the outputstream on client side and the inputstream on server side to see wether it works at all. Furthermore my object is now serialized to the satz.dat file and it works.
1. Is there a way to serialize my data "meineBuchdaten" on-the-fly without writing it into a file on harddisk?
2. I want to print out the deserialized data but it doesnt work i get no output using the system.out.println method?
3. After this output: Connection done - handshake Socket[addr=/127.0.0.1,port=3139,localport=10001] I get this output: null
why null? from where does this null come?
Edit: ok my debugger work now again i reinstalled eclipse... debugging the cast into "meineBuchdaten" is the problem because right after this the debugger jumps into an exception this one
catch(Exception ex)
               System.out.println(ex.getMessage());
          }Edit: I have changed again the code a bit only this:
// Output of the deserialized data for test purposes
System.out.println("This should be the deserialized data: Autor: " + bd.getAutor());
its accessing the autor attribute variable of the bd object in a proper way but still the bd object is null i guess the problem is my serialized data on the client side is not transported what do i wrong can someone pls help me please ?
changed code:
client class:
public class ClientThread extends Thread
       ClientThread()
       public void run()
              try
                      InetAddress ip = InetAddress.getByName("localhost");                 
                      Socket socket = new Socket(ip , ServerThread.PORT); 
                      System.out.println("test");                
                       // ObjectOutputStream for the object to be sent over socket to the server
                      ObjectOutputStream outgoingObject = new ObjectOutputStream(socket.getOutputStream());
                      // writing the class object "meineBuchdaten" into a file on the hard disk
                      try
                           ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/satz.dat")));
                      oos.writeObject(meineBuchdaten);
                      oos.flush();
                      oos.close();
                      catch (NotSerializableException nse)
                           System.out.println("No Serialization of the class " + nse.getLocalizedMessage() + " is possible");                           
                      catch(IOException ioe)
                           System.out.println(ioe.getMessage());
                      // closing the ObjectOutputStream and the client connection to free resources
                       outgoingObject.close();
                       socket.close();              
                 catch (Exception e)
                       e.printStackTrace();                
server class:
public class ServerThread extends Thread
     public static final int PORT = 10001;
     private ServerSocket myServerSocket = null;
     private Socket myClientSocket;     
     private Database myDatabase = new Database();     
     public ServerThread()
    public void run()
          try
               myServerSocket = new ServerSocket(PORT);               
          catch (IOException e)
               e.printStackTrace();
          System.out.println("Started: " + myServerSocket);          
          try
               while(true)
                    // wait until the client connects...               
                    myClientSocket = myServerSocket.accept();     
                    System.out.println("Connection done - handshake " + myClientSocket);
                    ObjectInputStream incomingObject = new ObjectInputStream(myClientSocket.getInputStream());                
                    // Reading the serialized data and cast it to the proper type
                    Buchdaten bd = (Buchdaten) incomingObject.readObject();
                    // Output of the deserialized data for test purposes
                    System.out.println("This should be the deserialized data: " + bd);
                    // closing the ObjectInputStream and the client connection to free resources
                    incomingObject.close();
                    myClientSocket.close();                    
          catch(Exception ex)
               System.out.println(ex.getMessage());
}Edited by: pel on Feb 2, 2008 2:04 AM

Similar Messages

Maybe you are looking for