Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder DELOCH's Avatar
    Join Date
    Apr 2006
    Location
    Canada
    Posts
    537
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Networking confusion

    codes:
    SimpleClient.java:

    PHP Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class 
    SimpleClient 
    {
        public static 
    void main(String[] args)
        {
            
    Socket sock null;
            
    PrintWriter out null;
            
    Scanner in null;
             
            
    System.out.println("Teh 1337 Clientz ist onlinz!");
            try {
                
    sock = new Socket("localhost"1234);
                
    out = new PrintWriter(sock.getOutputStream());
                
    in = new Scanner(sock.getInputStream());
     
                
    out.print("get");
                
    String quote in.nextLine();
                
    System.out.println(quote);
                
    out.println("bye");
            } catch (
    Exception e) { }
        }

    SimpleServer.java:
    PHP Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class 
    SimpleServer
    {
        public static 
    void main(String[] args)
        {
            
    ServerSocket ss null;
            
    Socket sock null;
            
    PrintWriter out null;
            
    Scanner in null;
            
            
    System.out.println("Teh 1337 Systemz r onlinz!");
            try {
                
    ss = new ServerSocket(1234);
                
    sock ss.accept();
                
    System.out.println("Connection set for: " sock);
                
    out = new PrintWriter(sock.getOutputStream());
                
    in = new Scanner(sock.getInputStream());

                while (
    true) {
                    
    String s in.nextLine();
                    if (
    == "bye") {
                        break;
                    } else if (
    == "get") {
                        
    out.println("Random number: " + (Math.random() * 400));
                    } else {
                        
    out.println("Whaddja want frum mi!");
                    }
                }
                
    sock.close();
                
    ss.close();
            } catch (
    Exception e){};    
        }

    The server is made to respond to request "get" -- give random message
    The client is made to get that response and print it to console
    The client is never getting that response... :|

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    thats coz the communication is buffered and as long as you don't exceed the buffer the data will not be transferred...you have to call flush() on your output streams to immediately transfer the data
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder DELOCH's Avatar
    Join Date
    Apr 2006
    Location
    Canada
    Posts
    537
    Thanks
    4
    Thanked 2 Times in 2 Posts
    I called .flush() explicitly but nothing changed.
    my output stream performs autoflush anyway... :|

    nothing changed... still doesn't work

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by DELOCH View Post
    I called .flush() explicitly but nothing changed
    assuming you changed the prints to printlns (readLine expects a newline char before returning ) and fixed

    Code:
    if (s == "bye") {
                        break;
                    } else if (s == "get") {
                        out.println("Random number: " + (Math.random() * 400));
                    } else {
                        out.println("Whaddja want frum mi!");
                    }
    that it ain't gonna work..

    Quote Originally Posted by DELOCH View Post
    my output stream performs autoflush anyway... :|
    really? take a look here
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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