java 对等方重置连接:套接字写入错误尝试发送对象时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37703167/
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
Connection reset by peer: socket write error While tring to send object
提问by Dreik
So I'm getting Connection reset by peer: socket write error while tring to send object and I don't know why. This is small part of my university project, I have spent a lot of time tring to make it work but I'm really out of ideas. I would be really greatful for your help. I tried to send a simple string message to client and it works fine.
所以我在尝试发送对象时通过对等方重置连接:套接字写入错误,我不知道为什么。这是我大学项目的一小部分,我花了很多时间试图让它发挥作用,但我真的没有想法。我会非常感谢您的帮助。我试图向客户端发送一个简单的字符串消息,它工作正常。
Server code:
服务器代码:
public class Server {
static ServerSocket serverSocket;
public static void main(String args[]) {
try {
serverSocket = new ServerSocket(7777);
Map map = new Map();
while(true)
{
Socket incoming = serverSocket.accept();
System.out.println(incoming.toString());
Runnable r = new ServerConnection(incoming, map);
Thread t = new Thread(r);
t.start();
}
}
}
ServerConnection code:
服务器连接代码:
public class ServerConnection implements Runnable {
private Socket s;
private ObjectOutputStream out;
private Map map;
public ServerConnection(Socket out, Map map){
this.s = out;
this.map = map;
System.out.println(map);
}
public void sendMap(){
//map.setRotation(rotated);
try {
out = new ObjectOutputStream(s.getOutputStream());
System.out.println(out);
System.out.println(this.map);
out.writeObject(this.map);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void closeSocket(){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
//this.connect();
this.sendMap();
}
}
The print stacktrace gives this information :
打印堆栈跟踪提供了以下信息:
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)`
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at Server.ServerConnection.sendMap(ServerConnection.java:39)
at Server.ServerConnection.run(ServerConnection.java:59)
at java.lang.Thread.run(Unknown Source)
Yes, I checked if Map is not null and if output exists etc. I don't know what triggers the problem.
是的,我检查了 Map 是否不为 null 以及是否存在输出等。我不知道是什么触发了问题。
Edit: - Object Map is serializable.
编辑: - 对象映射是可序列化的。
Client Console output:
客户端控制台输出:
Exception in thread "main" java.lang.NullPointerException
at Client.ClientConnection.getMap(ClientConnection.java:37)
at Client.Client.main(Client.java:28)
Client code:
客户端代码:
public static void main(String[] args) {
ClientConnection c = new ClientConnection();
c.connect();
Map map = c.getMap();
System.out.println(map);
c.closeSocket();
}
ClientConnection: - serverString is local adres "127.0.0.1"
ClientConnection: - serverString 是本地地址“127.0.0.1”
public void connect(){
try
{
Socket s = new Socket(serverString,7777);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Map getMap(){
Map map;
try {
input = new ObjectInputStream(s.getInputStream());
map = (Map)(input.readObject());
return map;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void closeSocket(){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
回答by DiogoSantana
Connection reset by peer means that the other side closed the connection. Since you are showing the server side program, then the client side must be the one closing it. Check the client side code and console output.
对端连接重置意味着对方关闭了连接。由于您正在显示服务器端程序,因此客户端必须是关闭它的那个。检查客户端代码和控制台输出。
EDIT: you are not assigning s to the class field, so it is always null when getMap is called.
编辑:您没有将 s 分配给类字段,因此在调用 getMap 时它始终为 null。
Change to this:
改成这样:
Socket s = new Socket(serverString,7777);
this.s = s;