Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 9:26 AM.

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