1

I try to disconnect my client:

public class SiecZapis {

    public static Socket gniazdo2;

    public static void main(String[] args) {
        new SiecZapis().doIt2();
    }

    public void doIt2() {   
        try {
            ServerSocket gniazdo = new ServerSocket(4242);

            while(true) {
                Socket gniazdo2 = gniazdo.accept();
                PrintWriter pisarz = new PrintWriter(gniazdo2.getOutputStream());
                String porada = "test2";
                pisarz.println(porada);
                pisarz.close();
                System.out.println(porada);
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    public void exit() {
        gniazdo2.disconnect();
    }
}

I tried to use something like socket.disconnect() from this topic but there is not method disconnect in socket. Could you please advise how to handle this topic?

senerh
  • 1,315
  • 10
  • 20
Woks
  • 13
  • 3
  • Running a Socket in the main thread is a bad idea, make sure that you also handle Nullpointer Exception. – Enzokie Feb 05 '18 at 12:38
  • Java `Socket`s and `socket.io` are two completely different things (in different languages nonetheless). You can't just pick some functions from one of them and try to use them on the other – nbokmans Feb 05 '18 at 12:40

1 Answers1

1

Insted of disconnect try using the close method.

public void exit(){
    gniazdo2.close();
}

Further reading: https://docs.oracle.com/javase/8/docs/api/java/net/Socket.html#close--

mboskamp
  • 50
  • 1
  • 5