Я пытаюсь передать объект через класс ObjectOutputStream через клиент и получить его через ObjectInputStream, проблема в том, что возникает java.net.SocketException: появляется ошибка сброса соединения, устраняющая прошлое объектов между клиентом и сервером,проблема решается, но я не знаю, в чем будет ошибка.
Раньше я работал правильно, когда у меня был клиентский код на сервере, и наоборот, но теперь, когда я изменяю эти части кодане хочет работать.В этих строках находится эта ошибка, на модульном сервере:
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Computer c = (Computer) ois.readObject();
CLIENT
private void startClient() {
DataInputStream in = null;
DataOutputStream out = null;
Socket socket = null;
try {
socket = new Socket(ip, port);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//MANDAMOS EL NUMERO EN RANGO HACIA LOS SERVIDORES
out.writeInt(n);
out.flush();
//LEEMOS EL TIEMPO ENVIADO POR EL SERVIDOR
tiempo = in.readLong();
System.out.println(tiempo);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
Computer c = (Computer) ois.readObject();
synchronized (main) {
main.add(c);
}
ois.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
out.close();
in.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
SERVER
private void startServer(){
DataOutputStream out = null;
DataInputStream in = null;
ServerSocket ss = null;
try {
Socket socket = null;
ss = new ServerSocket(port);
System.out.println("Esperando conexion");
socket = ss.accept();
in = new DataInputStream(socket.getInputStream());
int n = in.readInt();
long time = encontrarPrimos(n);
out = new DataOutputStream(socket.getOutputStream());
out.writeLong(time);
out.flush();
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Computer c = new Computer(id, Computer.getLocalIp(), time, Computer.getUserDomainSO());
oos.writeObject(c);
oos.close();
in.close();
out.close();
socket.close();
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}
КОМПЬЮТЕРНЫЙ КЛАСС
public class Computer implements Serializable{
private int id;
private String ip;
private long time;
private String userDomain;
public Computer(int id, String ip, long time, String userDomain) {
this.id = id;
this.ip = ip;
this.time = time;
this.userDomain = userDomain;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getUserDomain() {
return userDomain;
}
public void setUserDomain(String userDomain) {
this.userDomain = userDomain;
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + this.id;
hash = 59 * hash + Objects.hashCode(this.ip);
hash = 59 * hash + (int) (this.time ^ (this.time >>> 32));
hash = 59 * hash + Objects.hashCode(this.userDomain);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Computer other = (Computer) obj;
if (this.id != other.id) {
return false;
}
if (this.time != other.time) {
return false;
}
if (!Objects.equals(this.ip, other.ip)) {
return false;
}
if (!Objects.equals(this.userDomain, other.userDomain)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Computer{" + "id=" + id + ", ip=" + ip + ", time=" + time + ", userDomain=" + userDomain + '}';
}
public static String getLocalIp(){
try {
InetAddress localhost = InetAddress.getLocalHost();
return localhost.getHostAddress().trim();
} catch (UnknownHostException ex) {
Logger.getLogger(Computer.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public static String getUserDomainSO() {
String operatingSystem = System.getProperty("os.name");
if ("Linux".equals(operatingSystem) || "Mac OS X".equals(operatingSystem)) {
return System.getProperty("user.name");
} else if ("Windows".equals(operatingSystem)) {
return System.getenv("USERDOMAIN");
} else {
throw new RuntimeException("Unsupported operating system.");
}
}
}
Ошибка следующая:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2663)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2679)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
at Client.startClient(Client.java:71)
at Client.run(Client.java:44)
at java.lang.Thread.run(Thread.java:748)