问题
I am building a simple client/server application using java sockets and experimenting with the ObjectOutputStream etc.
I have been following the tutorial at this url http://java.sun.com/developer/technicalArticles/ALT/sockets starting half way down when it talks about transporting objects over sockets.
See my code for the client http://pastebin.com/m37e4c577 However this doesn't seem to work and i cannot figure out what's not working. The code commented out at the bottom is directly copied out of the tutorial - and this works when i just use that instead of creating the client object.
Can anyone see anything i am doing wrong?
回答1:
The problem is the order you are creating the streams:
In the server from the article (which I assume is what you are using), when a new connection is opened, the server opens first an input stream, and then an output stream:
public Connect(Socket clientSocket) {
client = clientSocket;
try {
ois = new ObjectInputStream(client.getInputStream());
oos = new ObjectOutputStream(client.getOutputStream());
} catch(Exception e1) {
// ...
}
this.start();
}
The commented example code uses the reverse order, first establishing the output stream, then the input stream:
// open a socket connection
socket = new Socket("localhost", 2000);
// open I/O streams for objects
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
But your code does it the other way around:
server = new Socket(host, port);
in = new ObjectInputStream(server.getInputStream());
out = new ObjectOutputStream(server.getOutputStream());
Establishing an output stream/input stream pair will stall until they have exchanged their handshaking information, so you must match the order of creation. You can do this just by swapping lines 34 and 35 in your example code.
回答2:
You are not writing the object anywhere.
See that link again, somewhere you have to write:
oos.writeObject( new Date() );
In your code you only have
ois.readObject();
That's why
回答3:
Just a reminder.
When you use ObjectOutputStream keep in mind that it keeps a reference cache. If you write an object, change the object contents, and then send the same object again, you will get duplicate data. For example:
List list = new ArrayList();
list.add("value1");
out.writeObject(list);
list.clear();
list.add("value2");
out.writeObject(list);
Will produce in the client side two lists with the string "value1".
To avoid this, the reset method must be invoked to reset the stream cache when writing the same object reference multiple times:
List list = new ArrayList();
list.add("value1");
out.writeObject(list);
out.reset();
list.clear();
list.add("value2");
out.writeObject(list);
回答4:
Probably you'll like to learn the most basic first.
Here's a sample I have just coded.
It start a server, that attends only ONE client, and it sends an object and die.
When the user ( you ) press enter, a new client is created, it connects to the previously created server and read the object that server will send.
No exception is handled here. Just to make things simpler, but this is NOT the way exception should be handled.
When you understand all the concepts here, it will be easier to understand those in the tutorial.
import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleServer implements Runnable {
// Creates the server, send a "date" and die.
public void run() {
try {
ServerSocket server = new ServerSocket( 8090 );
Date creationDate = new Date();
System.out.println("(Server) Server is ready "
+ "and running at " + creationDate );
Socket uniqueClient = server.accept();
// We will write using this "chained" object.
ObjectOutputStream out = new ObjectOutputStream(
uniqueClient.getOutputStream());
out.writeObject( creationDate );
// close all, at this point forget about the exceptions.
// this is lesson #1
out.close();
uniqueClient.close();
server.close();
System.out.println("(Server) The server is down");
}catch( IOException ioe ) {}
}
public static void main ( String [] args ) throws IOException ,
ClassNotFoundException {
Thread serverThread = new Thread( new SimpleServer() );
serverThread.start(); // start the server thread ... doh..
System.out.println("(Client) Press enter when you want "+
" to connect to the server...");
Scanner scanner = new Scanner( System.in );
scanner.nextLine();
Socket client = new Socket("localhost", 8090 );
// Read from this object.
ObjectInputStream in = new ObjectInputStream( client.getInputStream() );
Date date = ( Date ) in.readObject();
System.out.println("(Client) Current time is: " + new Date() );
System.out.println("(Client) Object read from server : " + date );
in.close();
client.close();
}
}
I hope this helps.
回答5:
If you tried a debugger it would have told you where the problem was.(Perhaps not why)
The issue you have is that ObjectOutputStream writes a header and ObjectInputStream reads that header. You have created the ObjectInputStream first which means it is trying to read a header which will never be written.
Solution: Always create the ObjectOutputStream first and flush() it before creating the ObjectInputStream.
回答6:
It's better to open an outputStream
because an output stream doesn't block. Then, you have the input stream that waits for a Stream
. After all the streams, you write to the stream and flush it - outputStream.flush()
to send the bytes of data. You'll also need a method on the other end to read the input, whether it's simply inputStream.read()
which reads every byte as an integer for a char
, or by using a BufferedReader
or Scanner
. I've used almost all methods possible, but the most effective method for sending is outputStream.write(String)
which writes a sequence of char
s as byte
s into the stream and reading inputStream.read()
reads a single char
. I hope this helps.
来源:https://stackoverflow.com/questions/471342/java-socket-programming