Results 1 to 3 of 3
  1. #1
    mozly is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Accessing a Java server from the Internet

    So I want to Create a simple java client and server that echo to each other through an internet network (not in LAN).
    Just as a test, I setup my pc with a static ip and I have my router forwarding to that ip (start port 8800, end port 8800). Everything works when I test it locally. However not when I connect from a computer on an external network. I know that my client app is working fine because It doesn't throw any errors and it does connect to the specified global ip address I gave it. The problem lies in my server app. The thread goes to sleep when I call server.accept() and never accepts any incoming connections.

    Also I know I did port forwarding correctly because I checked Open Port Check Tool after reading the someone having the same problem on stack overflow

    Moreover, I installed an apache web server at port 80; just as a test, I tried to http my ip on a web browser(chrome) on port 80 and it prompted me for my username and password. Could it be that my router denies access to the client because I need to authenticate first? if so how? Otherwise, what is my problem?


    here is the server code:
    Java Code:
    package Server;
    
    import java.net.*;
    import java.io.*;
    
    public class Server {
    	
    	private static final int PORT = 8800;
    	private ServerSocket servSock;
    	private Socket clientSock;
    	private static Server server; //singleton server
    	
    	/*
    	Static getter method to get reference to singleton server
    	*/
    	public static Server ServerAccess() 
    	{
    		try{
    			if(server == null)
    				server = new Server(PORT);
    			}catch(IOException e)
    			{
    				System.out.println("IOException has occured constructing server");
    			}
    		return server;
    	}
    	
    	/*
    	Define a port on construction
    	*/
    	private Server(int port) throws IOException
    	{
    		servSock = new ServerSocket(port);
    	}
    	
    	/*
    	main accept thread
    	*/
    	public void AcceptIncoming()
    	{
    		try{
    			clientSock = servSock.accept(); //wait for the client
    			OutputStream clientOut = clientSock.getOutputStream(); //get client's output
    			DataOutputStream toClient = new DataOutputStream (clientOut);
    			
    			//send msg
    			toClient.writeUTF("hello");
    			System.out.println("wrote to client");
    			
    			//close all connections
    			toClient.close();
    			clientOut.close();
    			clientSock.close();
    		}catch(IOException e)
    		{
    			System.out.println("IOException occured while accepting client");
    		}
    	}
    	
    	public void CloseConnection()
    	{
    		try{
    			servSock.close();
    			}
    		catch(IOException e)
    		{
    			System.out.println("There was an IOException closing socket");
    		}
    	}
    	
    	
    	public static void main(String args[])
    	{
    		//accept an incomoing call once
    		Server.ServerAccess().AcceptIncoming();
    		Server.ServerAccess().CloseConnection();
    	}
    
    }

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default Re: Accessing a Java server from the Internet

    You need to open the port on your computer too. If you are using Windows, add an Inbound Connection rule to the Windows Firewall.

  3. #3
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default Re: Accessing a Java server from the Internet

    what username/password is the server asking, is it the domain credentials? and did you try printing out statuses like once the connection is established, you print 'connection established' on the console, and so on. although the program is not throwing exceptions, you might see the state of the connection.

    you could also make a console printing on the server side.
    [why are you annoyed with my sig?]

Similar Threads

  1. How to fetch java function from the internet
    By sumosumo84 in forum Networking
    Replies: 1
    Last Post: 02-12-2011, 01:50 AM
  2. Replies: 9
    Last Post: 09-01-2009, 08:17 PM
  3. Using java to access internet account
    By elgem in forum Advanced Java
    Replies: 3
    Last Post: 01-18-2009, 07:49 PM
  4. Replies: 3
    Last Post: 12-09-2008, 09:17 AM
  5. Help - problem with java when using internet
    By badabing in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 02:39 PM

Posting Permissions

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