0

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();
    }
}
}

1 Answer 1

1

Why do you think there is anything wrong with your program. It is working as expected.

However, worth noticing is your code, I believe you want the client and server to run indefinitely.

But where is the infinite loop? You don't have one. So, the client will write a Date object, and the Server will read it. Just ONCE.

For continuity, I took your code, added two infinite loops and it works.

import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    private ExecutorService executor = Executors.newCachedThreadPool();
    private int connectedDevices = 0;
    private int portNr = 8090;
    private boolean running = true;
    private int clientNo = 0;
    private ServerSocket serverSocket;

    public Server() {
        new Thread(() -> {
            try {
                System.out.println("Server started--1");
                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) {
        new Server();
    }

    class HandleClient implements Runnable {
        private Socket socket;
        private ObjectInputStream din;

        public HandleClient(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            try {

                din = new ObjectInputStream(socket.getInputStream());
                String msg = "";
                while (true) {
                    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();
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}

Client:

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;

public class Client {
    private static Socket s;

    public static void main(String[] args) throws InterruptedException {
        try {

            System.out.println("before socket");
            s = new Socket("localhost", 8090);
            System.out.println("after socket");
            ObjectOutputStream dos = new ObjectOutputStream(s.getOutputStream());
            while (true) {
                System.out.println("before sending");
                dos.writeObject(new Date());
                System.out.println("after sending");
                Thread.sleep(1000);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.