Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Client / Server connection not working except localhost

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Client / Server connection not working except localhost

    I created a server and a client and it's working with localhost. But when using any IP except localhost for the client it won't work. I tested it with both LAN and Hamachi. When using my own Hamachi IP it also won't work, that's why I think it has nothing to do with port forwarding or something like that.


    This is the initialisation code for my server:

    public static void init()
        	{
        		try
        		{	
        			serverSocket = new ServerSocket(port, 4, InetAddress.getByName("localhost"));
        			new ClientThread(serverSocket); // The thread that's sending the data, not important for my problem, because I can't even connect to the server
        		}
        		catch (IOException e)
        		{
        			e.printStackTrace();
        		}
        	}

    And the client code:

    public static void init()
        	{
        		try
        		{
        			clientSocket = new Socket(serverIP, port);
        			System.out.println("Connected!\nBuffering...");
        			in = new ObjectInputStream(clientSocket.getInputStream());
        			out = new ObjectOutputStream(clientSocket.getOutputStream());
        			String name = "Client "+new Random().nextLong();
        			Client.name = name;
        			System.out.println("Buffered\nPinging for 256 bytes...");
        			out.writeObject(name.getBytes());
        			out.flush();
        			long latency = in.readLong();
        			System.out.println("Latency: "+(System.currentTimeMillis()-latency));
     
        			System.out.println("Starting threads...");
        			new ThreadSend(out);
        			new ThreadReceive(in);
        		}
        		catch (IOException e)
        		{
        			e.printStackTrace();
        		}
        	}

    Are there any errors in my code or is it something else?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Client / Server connection not working except localhost

    Quote Originally Posted by Bingo90 View Post
    But when using any IP except localhost for the client it won't work. I tested it with both LAN and Hamachi. When using my own Hamachi IP it also won't work, that's why I think it has nothing to do with port forwarding or something like that.
    If the server is on a distinct machine from the client, there are (at 99,9%) certainly some administrative issues about ports, firewalls and so on.
    You must assure that the "path" up to your server software is "open" and not blocked by some sw/hw entity. This has nothing to do with programming.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Client / Server connection not working except localhost

    Server and client are on the same computer.

    EDIT: Problem somehow solved itself
    Last edited by Bingo90; March 6th, 2014 at 10:03 AM.

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Client / Server connection not working except localhost

    Quote Originally Posted by Bingo90 View Post
    Server and client are on the same computer.
    If the client connects to "localhost" there are certainly (at 99,9%) no problems. And if your client/server works well in this situation you have done your job as programmer.
    If the client connects to an address that resolves to another machine or even, in the end, arrives at the same client machine but in a way that the connection "enters" from the outside (or at least triggers the local sw firewall) ..... here you can have problems. And here starts your job as network administrator.
    Point.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Server/Client Applet- Connection Reset Problem
    By CD8ED in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 16th, 2013, 02:49 PM
  2. Replies: 0
    Last Post: May 31st, 2012, 05:35 PM
  3. ...CONNECTION OF CLIENT AND SERVER RUNNING ON DIFFERENT COMPUTERS
    By baraka.programmer in forum Java Networking
    Replies: 1
    Last Post: August 9th, 2011, 07:11 AM
  4. Replies: 0
    Last Post: May 10th, 2011, 07:02 AM
  5. Replies: 0
    Last Post: February 24th, 2011, 06:31 AM