0

I wrote a program which needs to connect to multiple server sockets. Each time it connects to a server, I try to save the server's InetAddress and localPort as an ArrayList in an ArrayList (connectedServers). So connectedServers is an ArrayList of ArrayLists. Before a new connection to a server is made, I try to check whether the same server is already connected with this client by checking through connectedServers.

While debugging in eclipse, the debugger stops at the line marked "ERROR" in the below code. In eclipse a new tab opens with the heading NumberFormatException(Throwable).<init>(String) line: 197 which shows the message Source not found.

If I take the marked line of code outside the if block, the connection gets made successfully. But I need it to work inside the if block. What can be the problem? The code is as follows.

public static ArrayList<ArrayList<Object>> connectedServers = new ArrayList<ArrayList<Object>>();

public static void main (String args[]) throws IOException {
    listeningPort = 1111;
    String host = takeInput("Host");
    int port = takeInputInt("Port");

    Socket a = connectToServer(host, port);

    if (a != null) { 
        //....
            }
    //....
}

public static String takeInput(String inputName) throws IOException {
    System.out.print(inputName+": ");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String input = br.readLine();
    return input;
}

public static int takeInputInt(String inputName) throws IOException {
    System.out.print(inputName+": ");
    Scanner inputInt = new Scanner(System.in);
    int input = inputInt.nextInt();
    return input;
}

public static Socket connectToServer(String host, int port) throws IOException {
    ArrayList<Object> element = new ArrayList<>();
    element.add(host);
    element.add(port);

    //println(connectedServers);
    //println(element);
    //println(connectedServers);


    if (connectedServers.contains(element) != true) {
        //println(host + "   " + port);
        Socket fellowServer = new Socket(host, port);//<-------ERROR!!
        connectedServers.add(element);
        element.remove(host);
        element.remove(0);
        return fellowServer;
    }
    else{
        return null;
    }
}
1xQ
  • 85
  • 2
  • 11

1 Answers1

0

Something could be wrong in here with input

Scanner inputInt = new Scanner(System.in);
int input = inputInt.nextInt();
jmj
  • 237,923
  • 42
  • 401
  • 438
  • if that were the case then taking the marked line of code outside the `if` block would not have resulted in the connection being successfully made. Also, I tried taking input with `BufferedInput` and then using `Integer.parseInt()` for the port number. Got the same error... – 1xQ Mar 05 '14 at 03:17
  • 1
    try putting try catch around it to debug it, I am positive that it is coming from that statement – jmj Mar 05 '14 at 03:18
  • you are right. It gives out a `FileNotFoundException`. What can be done to rectify that? – 1xQ Mar 05 '14 at 03:26
  • I don't see any File operation unless you are hiding some code – jmj Mar 05 '14 at 03:29
  • The debugger stops at the line `Scanner inputInt = new Scanner(System.in);` and throws this exception. There is alot of other code...but this is the beginning of the program and nothing else gets executed before this. The other code should not affect this initial code at all. – 1xQ Mar 05 '14 at 03:31
  • any 4 digit number above 1024. I tested with 1111. – 1xQ Mar 05 '14 at 03:55
  • Are you sure yii arent adding space in input – jmj Mar 05 '14 at 04:17
  • Make it reproducible and paste ideone link here – jmj Mar 05 '14 at 04:39
  • [Here](https://ideone.com/Upx6QY) is the full code. There are some sub-classes whose code is not included(`Connection`&`ServerConnection`) ...There are quite a few bugs that need to be fixed in the code...but I think you will not need those parts of the code. FYI, I run the same instance of this code in separate cmd windows when checking if the program runs. – 1xQ Mar 05 '14 at 05:02
  • well could you please post a minimal code to reproduce it ? your code has bugs, `==` doesn't work for String – jmj Mar 05 '14 at 05:04
  • On your previous comment...`==` does work for String. It returns `true` or `false` depending on whether the String variable on the right side is the same as the String on the left. – 1xQ Mar 05 '14 at 05:21
  • [Here is a minimal code](https://ideone.com/kpXV5d) that reproduces that error when debugging with eclipse. – 1xQ Mar 05 '14 at 05:37
  • The last link i posted is actually very compact and it reproduces the error I originally asked about. Please have a look at it. I have read the link you posted. – 1xQ Mar 05 '14 at 05:45
  • it doesn't even compile – jmj Mar 05 '14 at 05:51
  • you changed the code, but now it doesn't produce exception that you are talking about please read the basic, I am going to delete answer and this thread since you aren;t actually helping me help you – jmj Mar 05 '14 at 06:08
  • very new to programming and cs as a whole. I tried to do what was asked though...dont know what i am doing wrong... – 1xQ Mar 05 '14 at 06:09
  • I know but you are not *helping me to help you*, if you could post the code which compiles fine, which is minimal, and which reproduces the error you are talking about I can help, not otherwise – jmj Mar 05 '14 at 06:11