0

I am making an online java chat and I was testing it and when I used it on my other computer it wouldn't work at the same time as this PC or on the other one show the JTextBox-es on the other PC, here's the code i use to communicate the server to the client

    Socket kkSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {

        kkSocket = new Socket("dargon.ddns.net", 7598);
        out = new PrintWriter(kkSocket.getOutputStream(), true);

        in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));



    } catch (UnknownHostException e) {

        System.err.println("not work :/");

      System.exit(1);

    } catch (IOException e) {

        System.err.println("Couldn't get I/O for the connection");

       System.exit(1);

    }

And here's the servers code

public class Online {

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


    ServerSocket serverSocket = null;

    try {

        serverSocket = new ServerSocket(7598);
        System.out.print("Online!");

    } catch (IOException e) {

      System.err.println("Could not listen on port: 7598.");

        System.exit(1);

    }


    Socket clientSocket = null;

    try {

        clientSocket = serverSocket.accept();
        System.out.print("Online!");

    } catch (IOException e) {

       System.err.println("Accept failed.");
        System.exit(1);

   }


    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

    BufferedReader in = new BufferedReader(

                            new InputStreamReader(

                           clientSocket.getInputStream()));

    out.close();

    in.close();

    clientSocket.close();

   serverSocket.close();

}

}

  • Possible duplicate of [socket programming multiple client to one server](http://stackoverflow.com/questions/10131377/socket-programming-multiple-client-to-one-server) – Adam Kučera Feb 17 '17 at 10:14
  • Your code doesn't really do anything. How do you know it's not working? Your server also only supports one connection, since you call `accept()` only once. – Kayaman Feb 17 '17 at 10:17
  • Thats not the full code, thats the snippits of the socket bit, if you want the full code i can give – Oscar Morton Feb 17 '17 at 10:18
  • where else do i call accept()? – Oscar Morton Feb 17 '17 at 10:18
  • @Kayaman I added an extra Client socket listener now it doesn't work at all. Do i add an extra server listener? – Oscar Morton Feb 17 '17 at 10:45

0 Answers0