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
publicclass client {
publicstaticvoid main(String argv[]) throws Exception {
int port = 6789;
String hostIP = "127.0.0.1";
DenseMatrix m1 = new DenseMatrix(newdouble[][] { {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
publicclass server {
publicstaticvoid 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());
}
}
}
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?
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());
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
publicclass server {
publicstaticvoid 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
publicclass client {
publicstaticvoid main(String argv[]) { //throws Exception {
try {
int port = 6789;
String hostIP = "127.0.0.1";
DenseMatrix m1 = new DenseMatrix(newdouble[][] { {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... :(
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