Reputation: 215
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)
Upvotes: 0
Views: 873
Reputation: 215
After testing different solutions, the main error was not the code, but the JVM installed on the computer where the client was running, so the solution was to install Java from 0, using the same version that was previously installed.
Upvotes: -1
Reputation: 2208
I made few modifications to your client code and it is working. I am not using Computer object here. Instead, I have used String cast.
private void startClient() {
DataInputStream in = null;
DataOutputStream out = null;
Socket socket = null;
try {
InetAddress host = InetAddress.getLocalHost();
socket = new Socket(host.getHostName(), 9876);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//MANDAMOS EL NUMERO EN RANGO HACIA LOS SERVIDORES
out.writeInt(1000);
out.flush();
//LEEMOS EL TIEMPO ENVIADO POR EL SERVIDOR
long tiempo = in.readLong();
System.out.println(tiempo);
String str;
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
if ((str = (String) ois.readObject()) != null) {
System.out.println(str);
}
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} finally {
try {
out.close();
in.close();
socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Upvotes: 2