Results 1 to 5 of 5
Thread: Simple Protocol Error!!
- 04-14-2011, 06:04 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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; } } }
I need help fast please guys :) THanks :DJava 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(); } }
- 04-14-2011, 08:22 PM #2
I didn't run the code, but looking over the code this stands out:
This will force the execution out of the loop immediately.Java Code:if(!in.hasNext()) return;
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
- 04-15-2011, 02:23 AM #3
@mrmatt1111
No the return works only when the connection has closed so that is a valid statement.
@OP
So....what is your question/problem?
- 04-15-2011, 10:22 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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" :)
- 04-15-2011, 09:07 PM #5
Similar Threads
-
30 line simple gui, simple error, need help
By cc11rocks in forum New To JavaReplies: 6Last Post: 02-05-2011, 08:33 PM -
Error with my simple GUI code
By Menre in forum New To JavaReplies: 6Last Post: 06-28-2010, 10:49 PM -
No Such Method Error - simple
By n00b in forum New To JavaReplies: 9Last Post: 05-04-2010, 04:05 PM -
Simple Error ???
By darkblue24 in forum New To JavaReplies: 3Last Post: 03-26-2010, 08:15 AM -
compiling error should be a simple fix.
By bottlecap in forum New To JavaReplies: 2Last Post: 01-23-2010, 06:07 AM


LinkBack URL
About LinkBacks

Bookmarks