DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: having java server/client problem

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Posts
    28

    having java server/client problem

    My problem here is that everytime the client is asked to key in a number, on the server it keeps repeating:

    Client from /127.0.0.1 connected.
    Client said: Greetings
    Client from /127.0.0.1 connected.
    Client said: Greetings

    etc.. how can i stop this guys? It's basically a game and the server uses MagicNumber to generate a random number and the client has to guess it within 10 goes!

    Server:

    Code:
    public class GuessNumberServer {
        public static void main (String args[]) throws IOException {
    
    	MagicNumber mnumb = new MagicNumber();
    
    	ServerSocket server = new ServerSocket(1357);
    	System.out.println("Waiting for client..");
    	
    	while (true) {
    	    try {
    		Socket client = server.accept();
    		
    		System.out.println("Client from " + client.getInetAddress() + " connected.");
    		
    		DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
    		DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
    		
    		String inmessage = in.readUTF();
    		System.out.println("Client said: " + inmessage);
    		
    		int clientnumber = in.readInt();
    		String result = mnumb.guess(clientnumber);
    		
    		if ((result == "Congratulations! Your guess was correct.") || (mnumb.getFinished())) {
    		    String end = "The game is now over.";
    
    		    out.writeUTF(end);
    		    out.flush();
    
    		    client.close();
    		    server.close();
    		} else {
    		    out.writeUTF(result);
    		    out.flush();
    		}
    	    } catch (Exception e) { }
    	}
    	
        }
        
    }// GuessNumberServer
    Client:

    Code:
    public class GuessNumberClient {
        public static void main (String args[]) throws IOException {
    	
    	while (true) {
    
    	   try {
    	       Socket server = new Socket("localhost", 1357);
    	       
    	       DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
    	       DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));
    	       
    	       String outmessage = "Greetings";
    	       out.writeUTF(outmessage);
    	       out.flush();
    	       
    	       System.out.println("Key in a guess number:");
    	       BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    	       int number = Integer.parseInt(stdin.readLine());
    	       
    	       if ((number < 1) || (number > 100)) {
    		   System.out.println("You have chosen to end the game.");
    		   server.close();
    		   break;
    	       } else {
    		   out.writeInt(number);
    		   out.flush();
    		   
    		   String inmessage = in.readUTF();
    		   System.out.println("The server said: " + inmessage);
    	       }
    	       
    	   } catch (Exception e) { }
    
    	}
    	
        } 
        
    }// GuessNumberClient
    Any help will be much appreciated! Here is an attachment if you guys want to see the problem for yourself when you run both the server and client together:
    Attached Files Attached Files

  2. #2
    Join Date
    Jul 2004
    Posts
    83
    if it helps 127.0.0.1 is your own machines IP address.
    They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.

  3. #3
    Join Date
    Jan 2006
    Posts
    28
    yes i know/ localhost and 127.0.0.1 both same

    but it keeps saying greetings repeatedly, im trying to stop it?

    anyone help?

Similar Threads

  1. Replies: 3
    Last Post: 03-21-2007, 03:28 PM
  2. java problem
    By KateP in forum Java
    Replies: 2
    Last Post: 04-30-2003, 05:52 PM
  3. Replies: 0
    Last Post: 02-20-2002, 01:05 AM
  4. Replies: 0
    Last Post: 02-01-2002, 12:08 AM
  5. problem with java frames URGENT!!!!
    By ramanand in forum Java
    Replies: 0
    Last Post: 10-17-2000, 05: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center