Original Post
So I want to Create a simple java client and server that echo to each other through an internet network (not in LAN). The issue is port Forwarding.
Just as a test, I setup my pc with a static ip and I have my router forwarding to that ip (start port 80, end port 80). 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.
I suspect that the router is not forwarding to the right port. How to I make it forward to port 80 on my pc?
Just as a test, I setup my pc with a static ip and I have my router forwarding to that ip (start port 80, end port 80). 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.
I suspect that the router is not forwarding to the right port. How to I make it forward to port 80 on my pc?
package Server;
import java.net.*;
import java.io.*;
public class Server {
private static final int PORT = 80;
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();
}
}