Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Passing a string to a variable (http://www.programmingforums.org/showthread.php?t=12346)

fresher Jan 10th, 2007 6:43 PM

Passing a string to a variable
 
Hi all, I've got this piece of code below which gives the output as shown:

:

// TCP client which receives a message from  server

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


class IPgenerator2
{

  //public static void main( String[] args )
 // {
          //int n = 0;
        String ip = "192.168.0.";
        void ipmethd()
        {
       
       
        KeyboardInput in = new KeyboardInput();
       
                for(int i = 1; i < 10; i++) {
                System.out.print(i);
                System.out.print(". ");
                //System.out.print();
                System.out.println(ip + i);
                }
                System.out.println();
       
                System.out.print("SELECT A MACHINE TO VIEW FROM LIST: ");
                int choice = in.readInteger();
                //to view  input
                //System.out.println(choice);
       
                        switch (choice)
                          {
       
                        case 1:
                        System.out.println("You selected to view machine with IP: 192.168.0.1");
                        //pass this ip to the client program to establish connection
                        break;
       
                        case 2:
                        System.out.println("You selected to view machine with IP: 192.168.0.2");
                        break;
       
                        default:
                        System.out.println("machine doesnt exist");
                        }
         
                  }
       
   
}




//client program starts here

public class TCPclientText
{
       
        public static void main(String args[])
        {
                IPgenerator2 ip = new IPgenerator2();
       
                //call ipgenerator
                ip.ipmethd();
       
                //server IP address
                  //String remoteIPaddress="192.168.2.4";
                  // int remotePort=9000;
 

  //array to hold list of client IPs say registered
  //array enables me select the client to connect to
  String remoteIPaddress[]={"192.168.2.4","192.168.2.5","192.168.2.6"};
  int remotePort=9000;
 
  //for(int index=0; index<3;index++)
 
  try
      {
        // connect to server with remoteIPaddress on remotePort
        //Socket socket1 = new Socket( remoteIPaddress, remotePort );
        //here I have selected to connect to just the first client - index 0
        Socket socket1 = new Socket( remoteIPaddress[0], remotePort );
       
       
        //timeout after 3sec of inactivity
        //socket1.setSoTimeout(3000);
       
        // open reader to receive messages from server
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader( socket1.getInputStream()));
        System.out.println("Server contacted OK ");
       
       
        // loop reading messages from server
        while(true)               
            {
                   
            String line = inFromServer.readLine();
            System.out.println("From server " + line);
            } 
             
        }
       
        catch(UnknownHostException e){
            System.err.println("Don't know about host ....");   
            }     
       
        //predefined message is returned if client is not responding
        catch(ConnectException e){
            System.err.println("Unreachable node!, failure in connecting to server");
            }   
       
    catch (IOException e) {System.err.println("TCP client error " +  e); }
   
    //if server suddenly shutsdown for instance
   
   
}

}


OUTPUT:
1. 192.168.0.1
2. 192.168.0.2
3. 192.168.0.3
.
.
.
9. 192.168.0.9

SELECT FROM LIST OF IPS.........:

[b]QUESTION[\B]
Im trying to pass the choice of the user to the variable 'remoteIPaddress' so that a connection is established with the
IP address (host) selected from the list by the user. In order words if the user selects/ enters 2 then: 192.168.0.2 is passed / assigned to remoteIPadress. Im gonna delete the array remoteIPaddress[] if i can get this to work.
any responses will be appreciated. Thanks

Jimbo Jan 10th, 2007 8:48 PM

You can simply return the IP address selected from ipmethd() as a String, save that value in a variable, and use the variable when you create the socket.

fresher Jan 10th, 2007 9:27 PM

Hi thanks for your reply but i've tried something similar and it doesnt work. see below.

:


// TCP client which receives a message from  server

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


class IPgenerator2
{

  //public static void main( String[] args )
 // {
          //int n = 0;
          String view_host;
        String ip = "192.168.2.";
       
        String ipmethd()
        {
       
       
        KeyboardInput in = new KeyboardInput();
       
                for(int i = 1; i < 10; i++) {
                System.out.print(i);
                System.out.print(". ");
                //System.out.print();
                System.out.println(ip + i);
                }
                System.out.println();
       
                System.out.print("SELECT A MACHINE TO VIEW FROM LIST: ");
                int choice = in.readInteger();
               
               
               
                //to view  input
                //System.out.println(choice);
       
                        switch (choice)
                          {
       
                        case 1:
                        view_host = "192.168.2.1";
                        System.out.println("You selected to view machine with IP: " + view_host);
                        return view_host;
                        break;
       
                        case 2:
                        view_host = "192.168.2.2";
                        System.out.println("You selected to view machine with IP: " +  view_host);
                        return view_host;
                        break;
                       
                        case 3:
                        view_host = "192.168.2.3";
                        System.out.println("You selected to view machine with IP: " +  view_host);
                        return view_host;
                        break;
                       
                        case 4:
                        view_host = "192.168.2.4";
                        System.out.println("You selected to view machine with IP: " +  view_host);
                        return view_host;
                        break;
                       
       
                        default:
                        System.out.println("This machine doesnt exist, please choose a valid host");
                        return view_host;
                        break;
                        }
                  }
         
                 
       
   
}




//client program starts here

public class TCPclientText
{
       
        public static void main(String args[])
        {
                //initialisation
                String view_host;
                IPgenerator2 ip = new IPgenerator2();
       
                //call ipgenerator
                ip.ipmethd();
       
         
                //server IP address
                  //String remoteIPaddress="192.168.2.4";
                  // int remotePort=9000;
 
 
 
                  //array to hold list of client IPs say registered
                  //array enables me select the client to connect to
                // String remoteIPaddress[]={"192.168.2.4","192.168.2.5","192.168.2.6"};
                  String remoteIPaddress = ip.ipmethd();
                  int remotePort=9000;
         
                  //for(int index=0; index<3;index++)
 
                  try
              {
                // connect to server with remoteIPaddress on remotePort
                //Socket socket1 = new Socket( remoteIPaddress, remotePort );
                //here I have selected to connect to just the first client - index 0
                Socket socket1 = new Socket( remoteIPaddress, remotePort );
       
       
                //timeout after 3sec of inactivity
                //socket1.setSoTimeout(3000);
       
                // open reader to receive messages from server
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader( socket1.getInputStream()));
                System.out.println("Server contacted OK ");
       
       
                // loop reading messages from server
                while(true)               
            {
                   
            String line = inFromServer.readLine();
            System.out.println("From server " + line);
            } 
             
        }
       
                catch(UnknownHostException e){
            System.err.println("Don't know about host ....");   
            }     
       
        //predefined message is returned if client is not responding
        catch(ConnectException e){
            System.err.println("Unreachable node!, failure in connecting to server");
            }   
       
    catch (IOException e) {System.err.println("TCP client error " +  e); }
   
    //if server suddenly shutsdown for instance
   
   
    }
}


Jimbo Jan 10th, 2007 11:28 PM

How is it not working? Does the BufferedReader line not work? Are you getting an exception? Is ip.ipmethd() not returning the correct value?

fresher Jan 11th, 2007 7:08 AM

problem
 
Quote:

Originally Posted by Jimbo (Post 122470)
How is it not working? Does the BufferedReader line not work? Are you getting an exception? Is ip.ipmethd() not returning the correct value?



Hi jimbo, thanks for your reply..this is what i've got:

:


// TCP client which receives a message from  server

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


        class IPgenerator2
        {
                  String view_host;
                String ip = "192.168.2.";
       
                String ipmethd()
                {
       
       
                KeyboardInput in = new KeyboardInput();
       
                for(int i = 1; i < 10; i++) {
                System.out.print(i);
                System.out.print(". ");
                //System.out.print();
                System.out.println(ip + i);
                }
                System.out.println();
       
                System.out.print("SELECT A MACHINE TO VIEW FROM LIST: ");
                int choice = in.readInteger();
               
               
       
                        switch (choice)
                          {
       
                        case 1:
                        view_host = "192.168.2.1";
                        //System.out.println("You selected to view machine with IP: " + view_host);
                        //return view_host = "192.168.2.1";
                        break;
       
                        case 2:
                        view_host = "192.168.2.2";
                        //System.out.println("You selected to view machine with IP: " +  view_host);
                        //return view_host = "192.168.2.2";
                        break;
                       
                        case 3:
                        view_host = "192.168.2.3";
                        //System.out.println("You selected to view machine with IP: " +  view_host);
                        //return view_host = "192.168.2.3";
                        break;
                       
                        case 4:
                        view_host = "192.168.2.4";
                        //System.out.println("You selected to view machine with IP: " +  view_host);
                        //return view_host = "192.168.2.4";
                        break;
                       
                       
                       
       
                        default:
                        System.out.println("This machine doesnt exist, please choose a valid host");
                       
                        return view_host;
                        }
                // return view_host;

            }
          }




        //client program starts here

        public class TCPclientText
        {
       
                        public static void main(String args[])
                        {
                        //initialisation
                        String view_host;
                        IPgenerator2 ip = new IPgenerator2();
       
                        //call ipgenerator
                        ip.ipmethd();
       
         
                        //server IP address
                          //String remoteIPaddress="192.168.2.4";
                          // int remotePort=9000;
 
 
 
                          //array to hold list of client IPs say registered
                          //array enables me select the client to connect to
                        // String remoteIPaddress[]={"192.168.2.4","192.168.2.5","192.168.2.6"};
                          String remoteIPaddress = view_host;
                          int remotePort=9000;
         
                          //for(int index=0; index<3;index++)
 
                          try
                              {
                        // connect to server with remoteIPaddress on remotePort
                        //Socket socket1 = new Socket( remoteIPaddress, remotePort );
                        //here I have selected to connect to just the first client - index 0
                        Socket socket1 = new Socket( remoteIPaddress, remotePort );
       
       
                        //timeout after 3sec of inactivity
                        //socket1.setSoTimeout(3000);
       
                        // open reader to receive messages from server
                        BufferedReader inFromServer = new BufferedReader(new InputStreamReader( socket1.getInputStream()));
                        System.out.println("Server contacted OK ");
       
       
                        // loop reading messages from server
                        while(true)               
                    {
                   
                    String line = inFromServer.readLine();
                    System.out.println("From server " + line);
                    } 
             
                }
       
                        catch(UnknownHostException e){
                    System.err.println("Don't know about host ....");   
                    }     
       
                //predefined message is returned if client is not responding
                catch(ConnectException e){
            System.err.println("Unreachable node!, failure in connecting to server");
            }   
       
            catch (IOException e) {System.err.println("TCP client error " +  e); }
   
            //if server suddenly shutsdown for instance
   
        }
    }
           
         
the program for some reason calls 'ipmethd()' twice before actually connecting ie. as shown below

1.192.168.2.1
2.192.168.2.2
3.192.168.2.3
.
.
9. 192.168.2.9

Select the machine to view....:

//now when an input is entered say 4, the  ipmethd() is called again:

1.192.168.2.1
2.192.168.2.2
3.192.168.2.3
.
.
9. 192.168.2.9

Select the machine to view....:
//after entering an input here then it goes to the next step - connection

I cant figure out why it calls ipmethd() twice b4 connecting..
pls help me out here im confused. thanks n advance.


fresher Jan 11th, 2007 7:31 AM

many thanks indeed it now works.


All times are GMT -5. The time now is 1:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC