1

How do I make this program exit when I run it in terminal using the Java JDK in Ubuntu?

I want to type in "end" when this program is running, but I can't get it to work. It listens for clients and calls a thread thingy to create a socket for the new client. I can't figure out how to make this program end. I tried everything.

I started learning java yesterday. Please help me....

import java.net.*;          //jave lib
import java.io.*;           //io lib

public class MultiServerConnections {       //initiate class
public static void main(String[] args) throws IOException {

    int portNum = 5342;                 //set server port number
    boolean listen = true;

    System.out.println("Listening for Connections");    //print message

    ServerSocket server_Socket = null;          //set server_Socket to null
    try {



        server_Socket = new ServerSocket(portNum);      //set server port
    } catch (IOException e) {
        System.err.println("Port " + portNum + " is unavailable"); //port is taken error
        System.exit(1);
    }

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); //set reader
String input;

    //while (TCPglobals.checkRequests){
    while (listen == true){
    new MultiServer(server_Socket.accept()).start();

    input = stdIn.readLine();
    //if(TCPglobals.checkRequests == false) //|| (input = stdIn.readLine()) == "end")
    if(input == "end") {
        System.out.println("end connection?");
        System.exit(1);
    }

}//while

    server_Socket.close(); //close server socket

}

}

  • `if(input == "end")` ==> `if(input.equals("end"))` – assylias Mar 08 '13 at 23:12
  • See also: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – assylias Mar 08 '13 at 23:14
  • I see that readLine is causing me problems. I want the server to continue running if nothing is typed in. The only way anything continues is if i write something into the server terminal, and then the server code continues. HOW DO I MAKE readLine skip if I don't write anything? – user2150278 Mar 09 '13 at 00:21
  • I suggest you asked a new question with that new issue. – assylias Mar 09 '13 at 10:21
  • Try polling BufferedReader.ready() to check if input is available. This is not the real answer, but apparently java has no easy way of non-blocking terminal IO. – Eric Urban Mar 09 '13 at 19:04

0 Answers0