This discussion is archived
14 Replies Latest reply: Jul 5, 2008 8:19 PM by 807589 RSS

Unable to connect using Socket

807589 Newbie
Currently Being Moderated
Hi everyone,
So, I only recently started working with networking using Java, and I must admit it's pretty easy and cool.
I wrote a small server/client application where:
1. The client connects to the server.
2. The server accepts the connection and created a thread for the client.
3. Using a protocol, the server responds to the client's data.

I have two questions:
1. When testing on one machine and connecting to 127.0.0.1 (which I think is like saying localhost) I am able to connect with more than one client, but only the first client is able to get responses from the server. Is this due to the port being busy with the first client? B/c my architecture should enable the server to deal with many clients.

2. (the more important question) I was trying to test my client/server app by running the server on one computer at home and trying to connect to it with a client from another computer. The connection times out. I am using an InetAddress object's getByName() method and passing it the host's IP address. The InetAddress object received from the static getByName() method I am passing to the client's Socket() constructor. I even tried to run the server on the same computer as the client and connect to it using that same computer's IP address, same problem occurs. Should I disable the windows firewalls on the computers? All computers are running XP. Also, my computers at home are connected wirelessly to a router.

Please help.
Thank you.
  • 1. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    You're probably going to need to post some code, but...

    1) Is your server multi-threaded? If it's not, it can only handle one request at a time (and if it's not in a loop, only one period).

    2) Are the computers on the same subnet? Can they ping eachother? What port are you using? You'll probably need to open the port in the firewall on the server computer.
  • 2. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    Well, the server is multi-threaded - it runs a loop that accepts clients and opens a separate thread for each client.

    As for the connectivity, I'm not so sure what a subnet is, but I will tell you that both computers I'm using are connected to the internet through the same wireless router... I also tried to connect to a server running at home from a work computer, and was unable to do so...
  • 3. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    public class Server 
    {
    public static void main(String[] args)
    {
    int port = 4444;
    boolean listening = true;
    ServerSocket serverSocket = null;
    LinkedList<Thread> threads = new LinkedList<Thread>();
    
    try
    {
    serverSocket = new ServerSocket(port);
    }
    catch(IOException e)
    {
    System.out.println("Failed to listen to port: " + port);
    System.exit(1);
    }
    
    while(listening)
    {
    try
    {
    Thread t = new ServerThread(serverSocket.accept());
    threads.add(t);
    t.start();
    System.out.println(threads.size() + " clients connected");
    }
    catch(IOException e)
    {
    System.out.println("Lost the port");
    listening = false;
    }
    }
    
    try
    {
    serverSocket.close(); 
    }
    catch(IOException e)
    {
    System.out.println("Failed to close socket");
    }
    }
    }
    
    public class ServerThread extends Thread 
    {
    private Socket clientSocket = null;
    public ServerThread(Socket socket)
    {
    boolean error = false;
    clientSocket = socket;
    PrintWriter out = null;
    BufferedReader in = null;
    
    try
    {
    out = new PrintWriter(clientSocket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }
    catch(IOException e)
    {
    error = true;
    }
    
    Protocol protocol = new Protocol();
    String toClient = "nothing";
    String fromClient = "nothing";
    
    while(protocol.terminateServer() == false && error == false)
    {
    toClient = protocol.serverSays();
    out.println(toClient);
    try
    {
    fromClient = in.readLine();
    }
    catch(IOException e)
    {
    out.println("Connection error");
    error = true;
    }
    protocol.clientSays(fromClient);
    }
    }
    
    
    }
    
    public class Client 
    {
    
    public static void main(String[] args) 
    {
    int port = 4444;
    String ip = "24.165.94.238";
    Socket socket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    boolean terminate = false;
    
    //connect
    
    InetAddress host = null;
    
    try
    {
    host = InetAddress.getByName(ip);
    }
    catch(UnknownHostException e)
    {
    output("could not find host");
    }
    
    
    try
    {
    output("Connecting to server at port " + port);
    socket = new Socket(host,port);
    }
    catch(Exception e)
    {
    output("failed to connect to server at " + ip + " port " + port);
    output(e.getMessage());
    
    System.exit(1);
    }
    output("connected to server");
    
    //initialize
    try
    {
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    catch(IOException e)
    {
    output("failed to initialize I/O streams");
    System.exit(1);
    }
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer = "";
    String toServer = "";
    
    //communicate
    while(!terminate)
    {
    try
    {
    fromServer = in.readLine();
    output(fromServer);
    toServer = stdIn.readLine();
    out.println(toServer);
    }
    catch(IOException e)
    {
    output("Connection to server lost");
    terminate = true;
    }
    
    if("terminate".equals(fromServer))
    {
    output("Server terminated client");
    terminate = true;
    }
    }
    
    //terminate
    try
    {
    in.close();
    out.close();
    socket.close();
    stdIn.close();
    } 
    catch(IOException e)
    {
    output("failed to close I/O streams or socket");
    }
    }
    
    public static void output(String text)
    {
    System.out.println(text);
    }
    
    }
    Edited by: Warlax on Jul 5, 2008 7:21 PM
  • 4. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    1) Then I'll need to see some code, I don't like making blind guesses.
    Edit: Ah, you tossed it up there. It's unreadable outside of code tags though... stick it in some and then I'll read it.

    2) Computers on a network have an IP address. A subnet is a range that is visible to a specific group of computers. For example:

    Computer 1:
    IP Address: 192.168.0.10
    Subnet Mask: 255.255.255.0

    Computer 1 can see any other computer on the network with IP address "192.168.0.X" because that's the subnet it's in.

    If your work computer is outside your home, it definitely won't connect without some tweaking. Routers block pretty much everything unless you open it up in the settings.

    Pretty much, if you can't ping it then you can't do anything with it.

    Edited by: sprizor on Jul 6, 2008 2:18 AM
  • 5. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    There, sorry about the formatting thing.
    Yeah, I already tried pinging my home computer from my work computer, no luck.
    But do you think the problem is server side and not client side? I mean, where do I have to change something? At work (client) or at home (server)?
    Also, what about my attempts to connect from a single computer to itself when running bith the server and the client on the same computer?
    When I use localhost it connects but when I use my own ip address, it fails.
  • 6. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    Note that in this post I'm using generic network settings that come on default routers.


    Your server, at home, behind your router, has an address similar to "192.168.1.100."
    Your router, at home, has an address similar to "24.165.94.238."

    Your client, at work, behind a router, has an address similar to "10.1.1.56."
    Your employer's router, at work, has an address similar to "22.34.250.23."

    The internet handles 22.34.250.23 talking to 24.165.94.238, and your employer's router can forward a request from 10.1.1.56 to 24.165.94.238.

    However, that doesn't help when the server is running at 192.168.1.100. The only way you can connect to it is by going into your router's configuration and telling it to forward port 4444 to 192.168.1.100.


    server <---- [ROUTER, --> ONLY ] <------ (((inter <--> net))) <------ [ROUTER, <-- ONLY] <------ client
    192.168.1.100 <---- [24.165.94.238 --> ONLY ] <------ (((inter <--> net))) <------ [22.34.250.23 <-- ONLY] <------ 10.1.1.56
  • 7. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    Okay, I see.
    So basicaly, if I understand correctly, I need to go the server's router and tell it to forward everything that comes in on port 4444 to the IP address of the server computer connected to that router, correct?
    How would you go about doing that?

    Also, when going to websites such as "www.whatismyipaddress.com" what IP address is showing? The actual computer's or the router to which the computer is hooked up?

    Thank you
  • 8. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    Warlax wrote:
    Okay, I see.
    So basicaly, if I understand correctly, I need to go the server's router and tell it to forward everything that comes in on port 4444 to the IP address of the server computer connected to that router, correct?
    How would you go about doing that?

    Also, when going to websites such as "www.whatismyipaddress.com" what IP address is showing? The actual computer's or the router to which the computer is hooked up?

    Thank you
    Yes, that is correct.

    If your comptuer's IP is 192.168.1.100, then your router most likely resides at http://192.168.1.1/ and you can configure it through a browser. There's usually a default password, which you can look up online or just try combinations of "admin" "root" or blank.

    whatsmyip.org and such sites tell you your router's IP. I guess I should have clarified - your router actually has two addresses, one for your internal network and one for the internet (that your ISP provides). The internal one is what I said in the last paragraph, the external one is the one you can find from those sites.

    To find your computer's IP, do this:

    Start... run.... cmd
    ipconfig
  • 9. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    Yes, I already knew about ipconfig, can't believe I went to a website to tell my IP.
    When I run ipconfig there are two address, one is "IP address" (ends with 138) and the other is "default gateway" (ends with 1)
    Which one of these is my router's and which one is my computer? My guess would be that default gateway == router, correct?
  • 10. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    The gateway is your router, yes.

    A gateway takes your request for an address on a different subnet (since you can't normally see it, right?) and forwards it to the correct place. A router doesn't HAVE to be a gateway, but they often are.

    99.9999% of the time, any IP that ends with .1 is a router or gateway of some sort.

    Your ISP gives your router an address and a gateway just as your router gives your computer an address and gateway. That's how the internet works.
  • 11. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    sprizor, you have been extremely helpful and I really feel like I learned a lot.
    Thank you.
  • 12. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    I wrote a networking / sockets tutorial for some random website once... wonder if it still exists... hrmm.
  • 13. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    One last question,

    If I do set up my router (server router) to relay everything on port 4444 to the server computer, would that also enable communication from the server computer back to the client's computer?
  • 14. Re: Unable to connect using Socket
    807589 Newbie
    Currently Being Moderated
    sprizor rules