The Artima Developer Community
Sponsored Link

Java Answers Forum
Object Serialisation

8 replies on 1 page. Most recent reply: Jun 3, 2005 1:13 PM by MS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
MS

Posts: 16
Nickname: ms2000
Registered: May, 2005

Object Serialisation Posted: May 31, 2005 1:16 PM
Reply to this message Reply
Advertisement
I'm having trouble sending an object (that implements the Serializable interface) from a client to a server. I have no problem with sending Strings or primitive data types, but I can't send an Object. I tried the format of the codes I've found on Java tutorial sites but it's of no use. Whenever I run the client (with the server running), I get:

java.io.FileNotFoundException: Matrix (The system cannot find the file specified)

at java.io.FileInputStream.open(Native Method)

at java.io.FileInputStream.<init>(FileInputStream.java:106)

at java.io.FileInputStream.<init>(FileInputStream.java:66)

at test1.server.main(server.java:29)

Exception in thread "main"


"server.java:29" is the line that corresponds to:
 FileInputStream inStream = new FileInputStream("Matrix");


Below are the client and server classes. It assumes that a DenseMatrix class exists, but I don't think it's necessary for me to post it here. What these simple classes should do is:

- Client send the server an object of type DenseMatrix (a class I wrote).

- Server sends its determinant back to the client.

Help is very appreciated!

Thanks!

---
package test1;
 
import java.io.*; // contains classes for input and output streams
import java.net.*; // provides classes for network support
 
public class client {
 
    public static void main(String argv[]) throws Exception {
 
        int port = 6789;
        String hostIP = "127.0.0.1";
        DenseMatrix m1 = new DenseMatrix(new double[][] { {2, -1, 6}, {5, 0, 1},
                                         {3, 2, 4}
        });
 
        /**
         * Create the object clientSocket of type Socket. It also initiates the TCP
         * connection between client and server.
         */
 
        Socket clientSocket = new Socket(hostIP, port);
 
        // Provide the process output to the socket.
 
        FileOutputStream out = new FileOutputStream("Matrix");
        ObjectOutputStream MatrixOutToServer = new ObjectOutputStream(out);
        MatrixOutToServer.writeObject(m1);
 
        // close down the streams
        MatrixOutToServer.flush();
        MatrixOutToServer.close();
        out.flush();
        out.close();
 
        DataInputStream inFromServer = new DataInputStream(clientSocket.
                getInputStream());
 
        System.out.println(inFromServer.readDouble());
 
        clientSocket.close();
    }
}


---

package test1;
 
import java.io.*; // contains classes for input and output streams
import java.net.*; // provides classes for network support
 
public class server {
 
    public static void main(String argv[]) throws Exception {
 
        int port = 6789;
 
        ServerSocket welcomeSocket = new ServerSocket(port);
 
        while (true) {
 
            /**
             * Create a new socket, called connectionSocket, when some client knocks
             * on welcomeSocket. This socket has the same port number. TCP then
             * establishes a direct virtual pipe between the client socket and
             * connectionSocket at the server so the client and server can send bytes
             * to each other over it.
             */
 
            Socket connectionSocket = welcomeSocket.accept();
 
            // Provide the process input from the socket.
            
            FileInputStream inStream = new FileInputStream("Matrix");
            ObjectInputStream objStream = new ObjectInputStream(inStream);
 
            // read the object
            DenseMatrix m1 = (DenseMatrix) objStream.readObject();
 
            // close down the streams
            objStream.close();
            inStream.close();
 
            // Provide the process output to the socket.
 
            DataOutputStream outToClient = new DataOutputStream(
                    connectionSocket.
                    getOutputStream());
 
            outToClient.writeDouble(m1.getDeterminant());
 
        }
 
    }
}


Jez Nicholson

Posts: 2
Nickname: jnicho02
Registered: Jun, 2005

Re: Object Serialisation Posted: Jun 1, 2005 3:03 AM
Reply to this message Reply
Javadocs say that, "A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform....."

Maybe you just have to create the file 'Matrix' first?

MS

Posts: 16
Nickname: ms2000
Registered: May, 2005

Re: Object Serialisation Posted: Jun 1, 2005 8:33 AM
Reply to this message Reply
Thanks for your response.

Unfortunatey, creating a file does not work either:

File theFile = new File ("Matrix");
FileInputStream inStream = new FileInputStream(theFile);
 


I'd appreciate any additional help. I couldn't move on with my project for days because of this.. .:(

Thanks.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Object Serialisation Posted: Jun 1, 2005 11:19 PM
Reply to this message Reply
Try and stack trace your error lets see if it gives us more info.
Do not throw your exceptions catch them and print them out
sequentially by placing your code in a try-catch as follows:



package test1;

import java.io.*; // contains classes for input and output streams
import java.net.*; // provides classes for network support

public class client {

public static void main(String argv[]) {

try{
int port = 6789;
String hostIP = "127.0.0.1";
DenseMatrix m1 = new DenseMatrix(new double[][] { {2, -1, 6}, {5, 0, 1},
{3, 2, 4}
});

/**
* Create the object clientSocket of type Socket. It also initiates the TCP
* connection between client and server.
*/

Socket clientSocket = new Socket(hostIP, port);

// Provide the process output to the socket.

FileOutputStream out = new FileOutputStream("Matrix");
ObjectOutputStream MatrixOutToServer = new ObjectOutputStream(out);
MatrixOutToServer.writeObject(m1);

// close down the streams
MatrixOutToServer.flush();
MatrixOutToServer.close();
out.flush();
out.close();

DataInputStream inFromServer = new DataInputStream(clientSocket.
getInputStream());

System.out.println(inFromServer.readDouble());

clientSocket.close();
}catch(Exception e){
System.out.println("***Reporting Stack Trace***");
e.printStackTrace();
}
}
}



Lets see if we can debug it from there. For debugging purposes, throwing Exceptions is
not usually a good thing.

Lets try and crack it...

One,
Spike

MS

Posts: 16
Nickname: ms2000
Registered: May, 2005

Re: Object Serialisation Posted: Jun 2, 2005 9:33 AM
Reply to this message Reply
Yo Spike,

Thanks a bunch for the reply. I re-wrote the program as you asked:

package test1;
 
import java.io.*; // contains classes for input and output streams
import java.net.*; // provides classes for network support
 
public class server {
 
    public static void main(String argv[]) { //throws Exception {
        try {
            int port = 6789;
            //String file = "C:/";
            ServerSocket welcomeSocket = new ServerSocket(port);
 
            while (true) {
 
                /**
                 * Create a new socket, called connectionSocket, when some client knocks
                 * on welcomeSocket. This socket has the same port number. TCP then
                 * establishes a direct virtual pipe between the client socket and
                 * connectionSocket at the server so the client and server can send bytes
                 * to each other over it.
                 */
 
                Socket connectionSocket = welcomeSocket.accept();
 
                // Provide the process input from the socket.
 
                //File theFile = new File (file);
                FileInputStream inStream = new FileInputStream("Matrix");
                ObjectInputStream objStream = new ObjectInputStream(inStream);
 
                // read the object
                DenseMatrix m1 = (DenseMatrix) objStream.readObject();
 
                // close down the streams
                objStream.close();
                inStream.close();
 
                // Provide the process output to the socket.
 
                DataOutputStream outToClient = new DataOutputStream(
                        connectionSocket.
                        getOutputStream());
 
                outToClient.writeDouble(m1.getDeterminant());
 
            }
        } catch (Exception e) {
            System.out.println("***Reporting Stack Trace***");
            e.printStackTrace();
        }
    }
}


----

package test1;
 
import java.io.*; // contains classes for input and output streams
import java.net.*; // provides classes for network support
 
public class client {
 
    public static void main(String argv[]) { //throws Exception {
        try {
            int port = 6789;
            String hostIP = "127.0.0.1";
            DenseMatrix m1 = new DenseMatrix(new double[][] { {2, -1, 6}, {5, 0,
                                             1}, {3, 2, 4}
            });
 
            /**
             * Create the object clientSocket of type Socket. It also initiates the TCP
             * connection between client and server.
             */
 
            Socket clientSocket = new Socket(hostIP, port);
 
            // Provide the process output to the socket.
 
            FileOutputStream out = new FileOutputStream("Matrix");
            ObjectOutputStream MatrixOutToServer = new ObjectOutputStream(out);
            MatrixOutToServer.writeObject(m1);
 
            // close down the streams
            MatrixOutToServer.flush();
            MatrixOutToServer.close();
            out.flush();
            out.close();
 
            DataInputStream inFromServer = new DataInputStream(clientSocket.
                    getInputStream());
 
            System.out.println(inFromServer.readDouble());
 
            clientSocket.close();
 
        } catch (Exception e) {
            System.out.println("***Reporting Stack Trace***");
            e.printStackTrace();
        }
 
    }
}


-----

But I don't see what should come out. I'm not seeing any difference in the server output:



java.io.FileNotFoundException: Matrix (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at test1.server.main(server.java:29)
***Reporting Stack Trace***





--


Sorry for the bother guys... I don't know what to do.. I've been reading some textbooks and they're doing exactly what I'm doing... very odd... :(

Thanks for any help.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Object Serialisation Posted: Jun 2, 2005 10:38 PM
Reply to this message Reply
***Reporting Exception***

should be the first line you see. It still seems to
throw it, or its not catching the right one...

Try:

(catch IOException) instead of just (Exception e)

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Object Serialisation Posted: Jun 2, 2005 10:43 PM
Reply to this message Reply
From the line count it seems that the beef is with
these two lines:

FileInputStream inStream = new FileInputStream("Matrix");
ObjectInputStream objStream = new ObjectInputStream(inStream);

Place a try catch around these lines too as follows:


try{
FileInputStream inStream = new FileInputStream("Matrix");
ObjectInputStream objStream = new ObjectInputStream(inStream);
}catch(IOException io){
System.out.println("The stupid inputstream is
not properly initialized");
io.printStackTrace();
}

Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Object Serialisation Posted: Jun 3, 2005 4:07 AM
Reply to this message Reply
Hi,

You are saying that you want to sent object from client to server, but your client program does not send the object to server. The client only saves the m1 object in "Matrix" file on client side, then how your server will find the "Matrix" file on its side.
The client code should do something like this:
OutputStream ops = clientSocket.getOutputStream();
ObjectOutputStream MatrixOutToServer = new ObjectOutputStream(ops);
// write object into stream

and your server side code should be
InputStream is = connectionSocket.getInputStream(); 
ObjectInputStream objStream = new ObjectInputStream(is);
//read object from stream


Get back if you have any queries.

Regards,
Amol Brid.

MS

Posts: 16
Nickname: ms2000
Registered: May, 2005

Re: Object Serialisation Posted: Jun 3, 2005 1:13 PM
Reply to this message Reply
I followed the suggestion above and it (finally!) works.
while (true) {
 
System.out.println("Thanks to all of you! :-)");

} // end while

Flat View: This topic has 8 replies on 1 page
Topic: java beans Previous Topic   Next Topic Topic: Missing component for Lomboz plug-in for Eclipse

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use