Results 1 to 2 of 2

Thread: Chat Server

  1. #1
    mrbrendano is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Chat Server

    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


    Java Code:
    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

    Java Code:
    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??

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: Chat Server

    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.
    By that you mean that you were on your house and your friend on his?

    If that's the case then in your client class the socket should connect YOUR "public" IP. (since you are the server that clients want to connect to)
    Just google whatsmyip and the first result is the page you are looking for!

    Try it and let me know what happens!
    Last edited by DeNiS_M; 09-25-2011 at 06:05 PM.

Similar Threads

  1. Replies: 2
    Last Post: 03-21-2010, 03:31 PM
  2. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 01:22 AM
  3. midlet chat server
    By nfitte in forum Networking
    Replies: 0
    Last Post: 07-09-2009, 10:08 AM
  4. [SOLVED] UDP chat client server
    By Koren3 in forum Networking
    Replies: 2
    Last Post: 04-25-2009, 02:51 AM
  5. simple chat server
    By sari in forum New To Java
    Replies: 0
    Last Post: 02-06-2009, 03:30 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
  •