Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 10th, 2007, 6:43 PM   #1
fresher
Newbie
 
Join Date: Dec 2006
Posts: 13
Rep Power: 0 fresher is on a distinguished road
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
fresher is offline   Reply With Quote
Old Jan 10th, 2007, 8:48 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Jan 10th, 2007, 9:27 PM   #3
fresher
Newbie
 
Join Date: Dec 2006
Posts: 13
Rep Power: 0 fresher is on a distinguished road
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
    
    
    } 
}
fresher is offline   Reply With Quote
Old Jan 10th, 2007, 11:28 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3 Jimbo is on a distinguished road
How is it not working? Does the BufferedReader line not work? Are you getting an exception? Is ip.ipmethd() not returning the correct value?
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Jan 11th, 2007, 7:08 AM   #5
fresher
Newbie
 
Join Date: Dec 2006
Posts: 13
Rep Power: 0 fresher is on a distinguished road
problem

Quote:
Originally Posted by Jimbo View Post
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 is offline   Reply With Quote
Old Jan 11th, 2007, 7:31 AM   #6
fresher
Newbie
 
Join Date: Dec 2006
Posts: 13
Rep Power: 0 fresher is on a distinguished road
many thanks indeed it now works.
fresher is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 6:41 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 6:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 11:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:37 AM.

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