Results 1 to 7 of 7
  1. #1
    DC200 is offline Member
    Join Date
    Dec 2008
    Posts
    49
    Rep Power
    0

    Question Simple server-client

    I've been trying for the past two hours to figure out why this piece of code isn't working. The client is supposed to receive a message ("two") from the server, but the client screen stays completely blank.

    Any help on getting this thing working would be appreciated. Then I can finally get to messing around with the code in order to understand how it actually works.

    Client
    Java Code:
    public class SimpleClient
          {
             public static void main (String [] args) throws IOException
                   {
                      final int APP_PORT = 3816;
                      Socket s = new Socket ("localhost", APP_PORT);
                      InputStream instream = s.getInputStream ();
                      OutputStream outstream = s.getOutputStream ();
                      Scanner in = new Scanner (instream);
                      PrintWriter out = new PrintWriter (outstream);
    
    				  out.print ("one");
                      out.flush ();
    
                      String response = in.nextLine ();
                      System.out.println (response);
    
                      s.close ();
                   }
          }
    Server
    Java Code:
    public class SimpleServer
          {
    		   public static void main (String [] args) throws IOException
    		         {
    					 final int APP_PORT = 3816;
    					 ServerSocket ss = new ServerSocket (APP_PORT);
    
    					 System.out.println ("Waiting for clients to connect");
    					 while (true)
    					      {
    							  Socket s = ss.accept ();
    							  System.out.println ("Client connected...");
    					          Service service = new Service (s);
    					          Thread t = new Thread (service);
    					          t.start ();
    					      }
    				 }
    	  }
    Service
    Java Code:
    public class Service implements Runnable {
    	Socket s;
    	Scanner in;
    	PrintWriter out;
    	InputStream instream;
    	OutputStream outstream;
    
    	public Service (Socket socket) {s = socket;}
    
    	public void run (){
    		try {
    			instream = s.getInputStream ();
    			outstream = s.getOutputStream ();
    			in = new Scanner (instream);
    			out = new PrintWriter (outstream);
    			System.out.print("Service running");
    			performService ();
    		}
    		catch (IOException ioe) {
    			ioe.getMessage ();
    		}
    	}
    
    	public void performService () throws IOException {
    		while (true) {
    			String command = in.next ();
    			if (in.next().equalsIgnoreCase ("one")) {
    				out.print ("server: two");
    				out.flush ();
    			}
    			else {
    				out.print ("server: three");
    				out.flush ();
    			}
    		}
    	}
    }
    Last edited by DC200; 11-11-2009 at 04:37 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I think you should read more about the socket programming before fix this code. Just start from the server, what your server class does and service class?

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    How many threads are you going to hijack?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    WTF?! Am I preventing others to give you an answer? You were told not to hijack threads and you still continue doing so. So why should I be bothered to help if you can't understand a simple advice.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    Belete is offline Member
    Join Date
    Dec 2009
    Location
    AddisAbaba
    Posts
    10
    Rep Power
    0

    Default

    I still donn now what is wrong with u evry person in one thread can help the others eventhough the threads are not the same, just i donn need to start another thread for simple problem if u can just help me other wise just donn try to write me back

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Belete, please see the private message that I have sent you.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Quote Originally Posted by Belete View Post
    I still donn now what is wrong with u evry person in one thread can help the others eventhough the threads are not the same, just i donn need to start another thread for simple problem if u can just help me other wise just donn try to write me back
    Hey! Guess what? Writing back still works.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Simple server/client text problem
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-02-2009, 11:33 AM
  2. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  3. Replies: 3
    Last Post: 01-07-2008, 08:01 AM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 11:36 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
  •