Results 1 to 5 of 5
  1. #1
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default Simple Protocol Error!!

    Hello Guys,

    I am confused and im not being able to get through this. In this code i have to send a command to a server and get a response. But when i send Echo n i must send a number with that. When the Server replies it must reply with the number that n is. here is the questions :$

    Client Request: ECHO n
    Server Response: n
    Meaning: Echoes n back to the client

    here n is a number that starts from 0 and increments when the server requests again and again

    here is the code i have till now.

    Java Code:
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    // A server that executes a simple protocol
    
    public class SimpleProtocolServer {
    	
    	public static void main(String [] args) throws IOException
    	{
    		final int SBAP_PORT = 8888;
    		ServerSocket server = new ServerSocket(SBAP_PORT);
    		System.out.println("Waiting for clients to connect...");
    		
    		while(true)
    		{
    			Socket s = server.accept();
    			System.out.println("Client Connected.");
    			SimpleProtocolService service = new SimpleProtocolService(s);
    			Thread t = new Thread(service);
    			t.start();
    		}
    	}
    }
    __________

    Java Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class SimpleProtocolService implements Runnable {
    	
    	Socket s;
    	int counter = 0;
        Scanner in;
        PrintWriter out;
    	
    	public SimpleProtocolService(Socket aSocket) 
    	{
    		s = aSocket;
    	}
    	
    	public void run()
    	{
    		try
    		{
    			try
    				{
    					in = new Scanner(s.getInputStream());
    					out = new PrintWriter(s.getOutputStream());
    					
    					doService();
    				}
    			finally
    			{
    				s.close();
    			}
    		}
    			
    			
    		catch (IOException exception)
    		{
    			exception.printStackTrace();
    		}
    	
    	
    	}
    	
    	public void doService() throws IOException 
    	{
    		
    		while (true)
    		{
    			if(!in.hasNext())
    					return;
    			
    			String command = in.next();
    			
    
    				executeCommand(command);
    		}
    	}
    	
    	public void executeCommand(String command) {
    		
    		
    		
    		if(command.equals("HELLO"))
    		{
    			out.print("Greetings!");
    			out.println();
    			out.flush();
    		}
    		
    		else if(command.equals("ECHO"))
    		{
    			counter++;
    			out.print(" Echo " + counter);
    			out.println();
    			out.flush();
    		}
    		
    		else if(command.equals("COUNT"))
    		{
    			out.print(counter);
    			out.println();
    			out.flush();
    		}
    		
    		else if(command.equals("QUIT"))
    		{
    			out.print("goodbye");
    			out.println();
    			out.flush();
    			return;
    		}
    	}
    	
    }
    _______________

    Java Code:
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.Scanner;
    
    
    public class SimpleProtocolClient {
    	
    	 /**
    	 This program tests the SimpleProtocolServer.
    	 */
    		 public static void main(String[] args) throws IOException
    		 {
    			 final int SBAP_PORT = 8888;
    			 Socket s = new Socket("localhost", SBAP_PORT);
    			 InputStream instream = s.getInputStream();
    			 OutputStream outstream = s.getOutputStream();
    		 
    		 Scanner in = new Scanner(instream);
    		 PrintWriter out = new PrintWriter(outstream);
    		 
    		 String command = "HELLO\n";
    		 System.out.print("Sending: " + command);
    		 out.print(command);
    		 out.flush();
    		 String response = in.nextLine();
    		 System.out.println("Receiving: " + response);
    		 
    		 command = "ECHO n\n";
    		 System.out.print("Sending: " + command);
    		 out.print(command);
    		 out.flush();
    		 response = in.nextLine();
    		 System.out.println("Receiving: " + response);
    		 
    		 command = "COUNT\n";
    		 
    		 System.out.print("Sending: " + command);
    		 out.print(command);
    		 out.flush();
    		 response = in.nextLine();
    		 System.out.println("Receiving: " + response);
    		 
    		 command = "QUIT\n";
    		 System.out.print("Sending: " + command);
    		 out.print(command);
    		 out.flush();
    		 response = in.nextLine();
    		 System.out.println("Receiving: " + response);
    		 
    		 s.close();
    }
    }
    I need help fast please guys :) THanks :D

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    4

    Default

    I didn't run the code, but looking over the code this stands out:

    Java Code:
    if(!in.hasNext())
       return;
    This will force the execution out of the loop immediately.

    You probably want to change it to:

    Java Code:
    if(!in.hasNext())
       continue;

    Also, you might want to put a pause in it so that you don't use up all of the CPU:

    Java Code:
    if(!in.hasNext()) {
       try { sleep(25); } catch(Exception e) {}
       continue;
    }
    My Hobby Project: LegacyClone

  3. #3
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    3

    Default

    @mrmatt1111
    No the return works only when the connection has closed so that is a valid statement.

    @OP
    So....what is your question/problem?

  4. #4
    hamad3914 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    hmmm Thank You "mrmatt1111" :D

    @ra4king ... my question is how do i make the server reply the number i type after ECHO

    for eg. if i type "ECHO 10" the server must reply and print "10" :)

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    3

    Default

    The code looks fine, what is your problem? Is it not replying?

Similar Threads

  1. 30 line simple gui, simple error, need help
    By cc11rocks in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 08:33 PM
  2. Error with my simple GUI code
    By Menre in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 10:49 PM
  3. No Such Method Error - simple
    By n00b in forum New To Java
    Replies: 9
    Last Post: 05-04-2010, 04:05 PM
  4. Simple Error ???
    By darkblue24 in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 08:15 AM
  5. compiling error should be a simple fix.
    By bottlecap in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 06:07 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
  •