java Java套接字编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/471342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Socket Programming
提问by Malachi
I am building a simple client/server application using java sockets and experimenting with the ObjectOutputStream etc.
我正在使用 java 套接字构建一个简单的客户端/服务器应用程序并尝试使用 ObjectOutputStream 等。
I have been following the tutorial at this url http://java.sun.com/developer/technicalArticles/ALT/socketsstarting half way down when it talks about transporting objects over sockets.
我一直在关注这个 url http://java.sun.com/developer/technicalArticles/ALT/sockets 上的教程,当它谈到通过套接字传输对象时。
See my code for the client http://pastebin.com/m37e4c577However 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.
请参阅我的客户端代码http://pastebin.com/m37e4c577但是这似乎不起作用,我无法弄清楚什么不起作用。在底部注释掉的代码是直接从教程中复制出来的 - 这在我只使用它而不是创建客户端对象时有效。
Can anyone see anything i am doing wrong?
谁能看到我做错了什么?
回答by Mike Houston
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.
建立输出流/输入流对将停止,直到它们交换了握手信息,因此您必须匹配创建顺序。您可以通过交换示例代码中的第 34 行和第 35 行来完成此操作。
回答by OscarRyz
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
这就是为什么
回答by jassuncao
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:
当您使用 ObjectOutputStream 时,请记住它会保留一个引用缓存。如果你写一个对象,改变对象的内容,然后再次发送同一个对象,你会得到重复的数据。例如:
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".
将在客户端生成两个带有字符串“value1”的列表。
To avoid this, the reset method must be invoked to reset the stream cache when writing the same object reference multiple times:
为了避免这种情况,当多次写入同一个对象引用时,必须调用 reset 方法来重置流缓存:
List list = new ArrayList();
list.add("value1");
out.writeObject(list);
out.reset();
list.clear();
list.add("value2");
out.writeObject(list);
回答by OscarRyz
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 clientis 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.
我希望这有帮助。
回答by Peter Lawrey
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.
您遇到的问题是 ObjectOutputStream 写入一个标头,ObjectInputStream 读取该标头。您首先创建了 ObjectInputStream,这意味着它正在尝试读取永远不会写入的标头。
Solution: Always create the ObjectOutputStream first and flush() it before creating the ObjectInputStream.
解决方案:始终先创建 ObjectOutputStream 并在创建 ObjectInputStream 之前刷新它。
回答by kyle england
It's better to open an outputStreambecause 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 BufferedReaderor Scanner. I've used almost all methods possible, but the most effective method for sending is outputStream.write(String)which writes a sequence of chars as bytes into the stream and reading inputStream.read()reads a single char. I hope this helps.
最好打开一个,outputStream因为输出流不会阻塞。然后,您拥有等待Stream. 在所有流之后,您写入流并刷新它 -outputStream.flush()以发送数据字节。您还需要在另一端使用一个方法来读取输入,无论是简单地inputStream.read()将每个字节读取为 a 的整数char,还是使用 aBufferedReader或Scanner。我已经使用了几乎所有可能的方法,但最有效的发送方法是outputStream.write(String)将chars 作为bytes的序列写入流中,然后读取inputStream.read()读取单个char. 我希望这有帮助。

