문제

Ok so when I connect the first time everything works. But then when I connect again (without stopping the server program) it says "Connection refused: connect". I thought it was because I was only accepting the connection once so I used a swing Timer to trigger an action event every 10 milliseconds and every time the action event triggered it set the clientSocket to serverSocket.accept() (clientSocket = serverSocket.accept();) so here is the code:

package org.code;

import java.net.*;
import java.io.*;

public class MainClass {
    public static void main(String[] args) {
            new MainClass();
    }


    Socket server = null;
    PrintWriter out = null;

    public MainClass() {
            try {
                    server = new Socket("192.168.0.104", 4444);
                    out = new PrintWriter(server.getOutputStream(), true);

                    out.println("start");

                    out.close();
                    server.close();
            } catch(Exception ex) {ex.printStackTrace();}
    }
}

And:

package org.code;

import java.io.*;
import java.net.*;
import javax.swing.*;

public class MainClass {
    public static void main(String[] args) {
            new MainClass();
    }

    ServerSocket serverSocket = null;

    Socket clientSocket = null;

    BufferedReader in;

    public MainClass() {
            JFrame frame = new JFrame("Minecraft Server Manager v0.1 Server");
            frame.setSize(500,500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);

            try {
                    serverSocket = new ServerSocket(4444);

                    clientSocket = serverSocket.accept();

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

                    String inputLine;

                    while((inputLine = in.readLine()) != null) {
                            System.out.println(inputLine);
                            if(inputLine.equals("start")) {
                                    System.out.println("Good");
                            }
                            if(inputLine.equals("stop")) {
                                    System.out.println("Bad");
                            }
                    }

                    clientSocket.close();
                    serverSocket.close();
            } catch(Exception ex) {System.err.println("Error: " + ex.getMessage());}
    }
}
도움이 되었습니까?

해결책

Your code closes the ServerSocket (and also stops) after every request.

This is a bit more likely to work. (I didn't tested it, but with this you get the scenario)

// in your main 
serverSocket = new ServerSocket(4444);

try {
    while (true) { 
        clientSocket = serverSocket.accept();

        Thread t = new ClientSocketThread(clientSocket);
        t.start();
    }
} finally {
    serverSocket.close();
}


class ClientSocketThread extends Thread {
    final Socket clientSocket;

    ClientSocketThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    public void run() {
        InputStream in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        try
            String inputLine;

            while((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
                if(inputLine.equals("start")) {
                     System.out.println("Good");
                }
                if(inputLine.equals("stop")) {
                     System.out.println("Bad");
                }
            }
         } finally {
            in.close()
            clientSocket.close();
         }
    }
}

다른 팁

Yes, the problem with current implementation is that it waits only for first client and then server stops. Check Writing the Server Side of a Socket especially last entry "Supporting multiple clients". Usually pattern for supporting multiple clients is following:

while (true) {
    accept a connection;
    create a thread to deal with the client;
}

Your server program (which, BTW, has the exacte same class name as the client program, which is not very wise), only accepts a single connection.

Once it has finished its conversation with the first client, it ends up. You think it's still listening because the frame is still visible, but the main method has ended.

You should have a loop if you want to accept multiple successive connections. And you should spawn threads if you want multiple parallel connections.

clientSocket.close();
serverSocket.close();

The problem is here. There is no point in closing the server socket at this moment just because you've finished with one client.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top