Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Servers in java

Options
  • 19-09-2011 8:27pm
    #1
    Registered Users Posts: 3,500 ✭✭✭


    Hey,

    i am using the HeadFirst java book to study java. There is a simple program to
    teach about sockets. There is two classes in the program. A client and a server.

    When the client connects to the server, the server sends back a message.
    here is the code for the classes. its straight from the book

    Client


    import java.io.*;
    import java.net.*;
    
    public class DailyAdviceClient
    {
        public void go() {
            try {
                Socket s = new Socket("127.0.0.1", 4242);
                InputStreamReader streamReader = new InputStreamReader(s.getInputStream()); 
                BufferedReader reader = new BufferedReader(streamReader);
                
                String advice = reader.readLine();
                System.out.println("Today you should: " + advice);
                reader.close();
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        
        public static void main(String[] args)
        {
            DailyAdviceClient client = new DailyAdviceClient();
            client.go();
        }
    }
    
    and the server

    import java.io.*;
    import java.net.*;
    
    public class DailyAdviceServer1
    {
        String[] adviceList = {"Take smaller bites", "Go for the tight jeans. No they do NOT make you look fat",
            "One word: inappropriate", "Just for today, be honest.  Tell your boss what you *really* think", 
            "You might want to rethink that haircut"};
            
        public void go() {
            try {
                ServerSocket serverSock = new ServerSocket(4242);
                while (true)
                {
                    Socket sock = serverSock.accept();
                    
                    PrintWriter writer = new PrintWriter(sock.getOutputStream());
                    String advice = getAdvice();
                    writer.println(advice);
                    writer.close();
                    System.out.println(advice);
                }
            } catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        
        private String getAdvice() {
            int random = (int) (Math.random() * adviceList.length);
            return adviceList[random];
        }
        
        public static void main(String[] args)
        {
            DailyAdviceServer1 server = new DailyAdviceServer1();
            server.go();
        }
    
    }
    

    The code works just fine if i run the server and then run the client on my own
    pc. I tried entering my pc's ip address instead of 127.0.0.1 (which the book says is localhost) and got my friend to run to client on his own pc elsewhere.

    Nothing happened!

    What would i need to get this program to run on seperate machines? Is there alot more code to be done??


Comments

  • Closed Accounts Posts: 5,082 ✭✭✭Pygmalion


    jonny666 wrote: »
    What would i need to get this program to run on seperate machines? Is there alot more code to be done??

    Many ISPs these days have stuff like NAT set up, so regular customers can't run servers properly (can also interfere with gaming :mad:).
    It's entirely possible the program is working fine but the computers are never actually connecting to each other.

    If you want to test out something like this a local network would be handy.
    If you have a wireless router just connect both computers to the wireless and check the network settings for the local IP (most likely starts with 192.168), that should work fine.


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    I must try running it on my own network using two separate computers. Never even thought of that!! Haha!

    Would i have to do some sort of port forwarding to get it to work over two separate networks?


  • Registered Users Posts: 3,500 ✭✭✭Drexel


    I actually got it running!! I needed to forward a port on the router and it works like a charm then. Thanks!!


Advertisement