I am working in a project in uni and i need to create a server client application with sockets , right now i just need to send a object throw but i am having some problems.It would be very helpful for me if someone pointed me out how to solve this error , i tried a lot of things and looked online a lot but coudent find anything helpfull , sorry for bad english and thanks in advance this is the exception i am getting.
Server output
Server started--1
Server started
new connection , all conections are :1
Starting thread for client 1 at Thu Mar 29 14:32:48 CEST 2018
Client 1's host name is 127.0.0.1
Client 1's IP Address is 127.0.0.1
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:247)
at Server$HandleClient.run(Server.java:61)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Server Code
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.concurrent.*;
public class Server {
private ExecutorService executor = Executors.newCachedThreadPool();
private int connectedDevices=0;
private int portNr =8090;
private boolean running=true;
private int clientNo=0;
public Server(){
new Thread(()->{
try{
System.out.println("Server started--1");
ServerSocket serverSocket = new ServerSocket(portNr);
System.out.println("Server started");
while(running){
Socket socket = serverSocket.accept();
clientNo++;
connectedDevices++;
System.out.println("new connection , all conections are :"+connectedDevices);
System.out.println("Starting thread for client " + clientNo +" at " + new Date() );
InetAddress inetAddress = socket.getInetAddress();
System.out.println("Client " + clientNo + "'s host name is "+ inetAddress.getHostName() );
System.out.println("Client " + clientNo + "'s IP Address is "+ inetAddress.getHostAddress() );
executor.execute(new HandleClient(socket));
}
}catch(Exception e){
e.printStackTrace();
}
}).start();
}
public synchronized void decrement(){
connectedDevices--;
System.out.println("lost connection , all conections are :"+connectedDevices);
}
public static void main(String [] args){
Server s=new Server();
}
class HandleClient implements Runnable{
private Socket socket;
public HandleClient(Socket socket){
this.socket=socket;
}
public void run() {
try{
ObjectOutputStream dos = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream din = new ObjectInputStream(socket.getInputStream());
String msg="";
System.out.println(msg);
Date date = (Date) din.readObject();
System.out.println("(Client) Current time is: " + new Date() );
System.out.println("(Client) Object read from server : " + date );
decrement();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
And finally the client code
import java.io.*;
import java.net.*;
import java.util.Date;
public class Client {
public static void main(String[] args) {
try {
System.out.println("before socket");
Socket s = new Socket("localhost",8090);
System.out.println("after socket");
ObjectOutputStream dos = new ObjectOutputStream(s.getOutputStream());
System.out.println("before sending");
dos.writeObject(new Date());
System.out.println("after sending");
}catch(IOException e) {
e.printStackTrace();
}
}
}