I am trying to pass an object through the ObjectOutputStream class through the client, and receive it through ObjectInputStream, the problem is that the java.net.SocketException: Connection reset error appears, eliminating this past of objects between client and server, the problem is It solves, but I do not know what the error would be.
Before I worked correctly when I had the client code on the server and vice versa, but now that I change these parts of the code does not want to work. In these lines is where this error jumps, on module server:
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 {
}
}
COMPUTER CLASS
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.");
}
}
}
The error is the next:
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)
NotSerializableExceptionfor example? What isComputer? Does it have a customwriteObject()method? There's not enough information here. NB Your title is wrong. You are passing an object from the server to the client. And you can still do all the I/O with object streams, as I told you in the previous version of this question.Computerclass, just as it evidently was the last time when I asked the same question about the sender throwing an exception and you hastily deleted the question. And you haven't postedComputer. Again. So your question is off-topic.