View Single Post
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