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