Results 1 to 6 of 6
  1. #1
    JonnySnip3r is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default Looping Help Please

    Hey guys im new to java but this morning i decided to look into the basics of java networking. i found this amazing tutorial online so i decided to edit it and see what i could do with it.

    The tutorial provided a server and a client, the client connects to the server sends a message automatically then sends "bye" to close the connection. i have changed this where it lets me send a message but it only sends one message please take a look at the code below.

    message = (String)in.readObject();

    System.out.println("server>" + message);

    System.out.print("Type Message: ");

    Scanner mainMessage = new Scanner(System.in);

    String tehMainMessage = mainMessage.next();

    sendMessage(tehMainMessage);


    how could i loop this untill mainMessage = bye and then when it = bye it will drop out of the loop and carry on with the rest of the code.

    Thanks very much and i know i may be into deep before i can swim but how else are ya gunna learn haha.

    Thanks very much

    Here is the Full code for the client:

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

    //////////////////////////////////////////////////////////////////////////
    // THIS IS THE CLIENT //
    //////////////////////////////////////////////////////////////////////////


    public class Requester{
    Socket requestSocket;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    Requester(){}
    void run()
    {
    try{

    Scanner tehMessage = new Scanner(System.in); // Host - Default localhost
    Scanner tehPort = new Scanner(System.in); // Port - Default 2004


    //1. creating a socket to connect to the server
    System.out.print("Welcome, enter IP address: ");

    String localHost = tehMessage.next();

    System.out.print("Thanks, enter PORT: ");

    int porT = tehPort.nextInt();

    requestSocket = new Socket(localHost, porT);

    System.out.println("Connected to " + localHost + " in port " + porT + ".");
    //2. get Input and Output streams
    out = new ObjectOutputStream(requestSocket.getOutputStream() );
    out.flush();
    in = new ObjectInputStream(requestSocket.getInputStream());
    //3: Communicating with the server
    do{
    try{

    /*
    message = (String)in.readObject();
    System.out.println("server>" + message);
    sendMessage("Hi my server");
    message = "bye";
    sendMessage(message);
    */

    message = (String)in.readObject();

    System.out.println("server>" + message);

    System.out.print("Type Message: ");

    Scanner mainMessage = new Scanner(System.in);

    String tehMainMessage = mainMessage.next();

    sendMessage(tehMainMessage);

    /*
    message = "bye";
    sendMessage(message);
    */

    }
    catch(ClassNotFoundException classNot){
    System.err.println("data received in unknown format");
    }
    }while(!message.equals("bye"));
    }
    catch(UnknownHostException unknownHost){
    System.err.println("You are trying to connect to an unknown host!");
    }
    catch(IOException ioException){
    ioException.printStackTrace();
    }
    finally{
    //4: Closing connection
    try{
    in.close();
    out.close();
    requestSocket.close();
    }
    catch(IOException ioException){
    ioException.printStackTrace();
    }
    }
    }
    void sendMessage(String msg)
    {
    try{
    out.writeObject(msg);
    out.flush();
    System.out.println("client>" + msg);
    }
    catch(IOException ioException){
    ioException.printStackTrace();
    }
    }
    public static void main(String args[])
    {
    Requester client = new Requester();
    client.run();
    }
    }

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Java Code:
    do
    {
    System.out.print("Type Message: ");
    
    Scanner mainMessage = new Scanner(System.in);
    
    String tehMainMessage = mainMessage.next();
    
    sendMessage(tehMainMessage);
    } while(!tehMainMessage.equals("bye"))
    Try that.

  3. #3
    JonnySnip3r is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    thanks for the help dude but didnt work :( will try though :)

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    What is the code for the server?

  5. #5
    JonnySnip3r is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    Hey dude here is the code for the both of them :)

    Provider

    Java Code:
    import java.io.*;
    import java.net.*;
    
    	//////////////////////////////////////////////////////////////////////////
    	// 				THIS IS THE SERVER				  //
    	//////////////////////////////////////////////////////////////////////////
    
    
    public class Provider{
    	ServerSocket providerSocket;
    	Socket connection = null;
    	ObjectOutputStream out;
    	ObjectInputStream in;
    	String message;
    	Provider(){}
    	void run()
    	{
    		try{
    			//1. creating a server socket
    			providerSocket = new ServerSocket(2004, 10);
    			//2. Wait for connection
    			System.out.println("Waiting for connection...");
    			connection = providerSocket.accept();
    			System.out.println("Connection received from " + connection.getInetAddress().getHostName());
    			//3. get Input and Output streams
    			out = new ObjectOutputStream(connection.getOutputStream());
    			out.flush();
    			in = new ObjectInputStream(connection.getInputStream());
    			sendMessage("Connection successful");
    			//4. The two parts communicate via the input and output streams
    			do{
    				try{
    					message = (String)in.readObject();
    					System.out.println("client>" + message);
    					if (message.equals("!bye"))
    						sendMessage("bye");
    				}
    				catch(ClassNotFoundException classnot){
    					System.err.println("Data received in unknown format");
    				}
    			}while(!message.equals("bye"));
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    		finally{
    			//4: Closing connection
    			try{
    				in.close();
    				out.close();
    				providerSocket.close();
    			}
    			catch(IOException ioException){
    				ioException.printStackTrace();
    			}
    		}
    	}
    	void sendMessage(String msg)
    	{
    		try{
    			out.writeObject(msg);
    			out.flush();
    			System.out.println("server>" + msg);
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    	}
    	public static void main(String args[])
    	{
    		Provider server = new Provider();
    		while(true){
    			server.run();
    		}
    	}
    }
    and here is the requester

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.Scanner;
    
    	//////////////////////////////////////////////////////////////////////////
    	// 				THIS IS THE CLIENT				  //
    	//////////////////////////////////////////////////////////////////////////
    	
    	
    public class Requester{
    	Socket requestSocket;
    	ObjectOutputStream out;
     	ObjectInputStream in;
     	String message;
    	Requester(){}
    	void run()
    	{
    		try{
    		
    			Scanner tehMessage = new Scanner(System.in); // Host - Default localhost
    			Scanner tehPort = new Scanner(System.in); // Port - Default 2004
    			
    			
    			//1. creating a socket to connect to the server
    			System.out.print("Welcome, enter IP address: ");
    			
    			String localHost = tehMessage.next();
    			
    			System.out.print("Thanks, enter PORT: ");
    			
    			int porT = tehPort.nextInt();
    			
    			requestSocket = new Socket(localHost, porT);
    			
    			System.out.println("Connected to " + localHost + " in port " + porT + ".");
    			//2. get Input and Output streams
    			out = new ObjectOutputStream(requestSocket.getOutputStream());
    			out.flush();
    			in = new ObjectInputStream(requestSocket.getInputStream());
    			//3: Communicating with the server
    			do{
    				try{
    					
    					/*
    					message = (String)in.readObject();
    					System.out.println("server>" + message);
    					sendMessage("Hi my server");
    					message = "bye";
    					sendMessage(message);
    					*/
    					
    					message = (String)in.readObject();
    					
    					System.out.println("server>" + message);
    					
    					System.out.print("Type Message: ");
    					
    					Scanner mainMessage = new Scanner(System.in);
    										
    					String tehMainMessage = mainMessage.next();
    					
    					sendMessage(tehMainMessage);
    					
    					/*
    					message = "bye";
    					sendMessage(message);
    					*/
    					
    				}
    				catch(ClassNotFoundException classNot){
    					System.err.println("data received in unknown format");
    				}
    			}while(!message.equals("bye"));
    		}
    		catch(UnknownHostException unknownHost){
    			System.err.println("You are trying to connect to an unknown host!");
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    		finally{
    			//4: Closing connection
    			try{
    				in.close();
    				out.close();
    				requestSocket.close();
    			}
    			catch(IOException ioException){
    				ioException.printStackTrace();
    			}
    		}
    	}
    	void sendMessage(String msg)
    	{
    		try{
    			out.writeObject(msg);
    			out.flush();
    			System.out.println("client>" + msg);
    		}
    		catch(IOException ioException){
    			ioException.printStackTrace();
    		}
    	}
    	public static void main(String args[])
    	{
    		Requester client = new Requester();
    		client.run();
    	}
    }

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    What is happening is the first time through the loop, everything works, but then the 2nd time the client just sits there because it is wiating for input from the server "message = (String)in.readObject();"

Similar Threads

  1. Looping and Recurring
    By tim in forum New To Java
    Replies: 7
    Last Post: 12-22-2009, 08:52 PM
  2. looping problems
    By Blakester in forum New To Java
    Replies: 4
    Last Post: 10-05-2009, 08:26 PM
  3. Looping if statements
    By rice in forum New To Java
    Replies: 10
    Last Post: 10-02-2009, 02:50 AM
  4. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 12:38 PM
  5. Looping problem
    By Tanilo in forum New To Java
    Replies: 1
    Last Post: 08-01-2008, 06:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •