简体   繁体   中英

ServerSocket stops recieving commands, java/android

I have made a connection between my phone and my computer using the Socket-framework. It work fine as long as i only need to send one command. Then i have to restart my server to recieve a new command. Can you help me figure out why? I'd like to be able to send multiple commands.

I'm calling the class-methods from a GUI-main-class.

My server code:

public class Server {

    private static PrintWriter out;
    private static BufferedReader in;
    private static ServerSocket serverSocket = null;
    private static Socket clientSocket = null;
    private static String inputLine, outputLine;

    public static void main() throws IOException {


        try {
            serverSocket = new ServerSocket(4444);
            System.out.println(serverSocket.getInetAddress().toString());
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }


        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        Protocol kkp = new Protocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             System.out.println(inputLine);
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
        }

    }

    public static void shutDown() throws IOException
    {
        System.exit(1);
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }

}

And my client code:

class ClientThread implements Runnable {

        public void run() {
            Socket kkSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;


            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                kkSocket = new Socket(serverAddr, 4444);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            } catch (UnknownHostException e) {
                showToast("Kan ikke finde host fra: "+settings.getString("ip", "86.52.16.102"));
                System.err.println("Don't know about host: taranis.");
                System.exit(1);
            } catch (IOException e) {
                showToast("Kan ikke udveksle oplysninger med: "+settings.getString("ip", "86.52.16.102"));
                System.err.println("Couldn't get I/O for the connection to: taranis.");
                System.exit(1);
            }

            String fromServer;
            String fromUser;

            try {
                while ((fromServer = in.readLine()) != null) {
                    System.out.println("Server: " + fromServer);
                    if (fromServer.equals("Shutting down") || fromServer.equals("Wrong pin!"))
                    {
                        showToast(fromServer);

                        break;
                    }


                    //fromUser = stdIn.readLine();
                    fromUser = pinkode.getText().toString()+","+time;
                    if (fromUser != null) {
                        System.out.println("Client: " + fromUser);
                        out.println(fromUser);
                        fromUser = null;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            out.close();
            try {
                in.close();
                kkSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

Try to put this part of the code:

    try {
        clientSocket = serverSocket.accept();
        } catch (IOException e) {
             System.err.println("Accept failed.");
            System.exit(1);
         }

    out = new PrintWriter(clientSocket.getOutputStream(), true);
    in = new BufferedReader(
            new InputStreamReader(
            clientSocket.getInputStream()));
    Protocol kkp = new Protocol();

    outputLine = kkp.processInput(null);
    out.println(outputLine);

    while ((inputLine = in.readLine()) != null) {
         System.out.println(inputLine);
         outputLine = kkp.processInput(inputLine);
         out.println(outputLine);
    }

Inside a while(true){ ... } .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM