Jump to content
  • Advertisement
Sign in to follow this  
Mozly

How to Create a java Server

This topic is 2548 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

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();
}

}

Share this post


Link to post
Share on other sites
Advertisement

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?

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();
}

}



First of all, if you run the client and server on the same machine and have the client connect to 127.0.0.1 , does it work ?

If that works then you are most likely right and it is port forwarding in which case you have to consult your router manual as the exact way to configure it varies between models.

Share this post


Link to post
Share on other sites
Yes it works fine if I connect to local host. How to I forward the connection to port 80 on my pc?

Also I can't seem to connect if I portforward to any port other than port 80.

Moreover if I add this line after binding the server to a port: System.out.println(servSock.getInetAddress());
it gives me 0.0.0.0/0.0.0.0
Is it normal that it doesn't give me my local ip address?

Share this post


Link to post
Share on other sites

Yes it works fine if I connect to local host. How to I forward the connection to port 80 on my pc?

Also I can't seem to connect if I portforward to any port other than port 80.

Moreover if I add this line after binding the server to a port: System.out.println(servSock.getInetAddress());
it gives me 0.0.0.0/0.0.0.0
Is it normal that it doesn't give me my local ip address?


it depends on how you bind it, a listening socket bound to 0.0.0.0 should accept connections regardless of their origin, (Setting it to something specific is useful if you for example want to run two webservers, both at port 80 on the same machine and separate incoming connections based on which network connection they come in through)

As for the portforwading i've allready told you, You have to look at your routers manual, or, if you can't read manuals for some wierd reason, post the exact model you have and someone might google it for you. (Port forwarding has nothing to do with Java, it is entierly a router configuration issue)

Share this post


Link to post
Share on other sites
Thanks Simon for the prompt reply, I'm pretty sure I did the port forwarding correctly, I followed this guide: http://portforward.com/english/routers/port_forwarding/Netgear/WGR614v7/default.htm
which is the guide for my router.

I set the start port and the ending port to 80
and I set the server IP address to my local address.

When I port Forward on port 80 the serverSocket doesn't accept.

Of course I also tried changing it to a different port. The client doesn't even connect when I set it to a different port nor does the serversocket accept().

I hope this sheds some more details on my problem. How do I get the connection to work?

Share this post


Link to post
Share on other sites

Thanks Simon for the prompt reply, I'm pretty sure I did the port forwarding correctly, I followed this guide: http://portforward.c...4v7/default.htm
which is the guide for my router.

I set the start port and the ending port to 80
and I set the server IP address to my local address.

When I port Forward on port 80 the serverSocket doesn't accept.

Of course I also tried changing it to a different port. The client doesn't even connect when I set it to a different port nor does the serversocket accept().

I hope this sheds some more details on my problem. How do I get the connection to work?


Did you set it to the correct protocol aswell (sometimes you can choose between TCP and UDP)

If you are using port 80 TCP you could also test the port forwarding using a webserver such as apache (install and run it on your server and see if you can access it from the internet).

The firewall on your server might also cause issues with incoming connections that doesn't come from a LAN ip.

Share this post


Link to post
Share on other sites
hmm. I set up apache but I dont know how to test if I can connect I tried this code:

public static void main(String[] args)
{
try {
// Create a URLConnection object for a URL
URL url = new URL("http://54.554.54.65:80");
URLConnection conn = url.openConnection();

// List all the response headers from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
System.out.print(headerName);
String headerValue = conn.getHeaderField(i);

if (headerName == null && headerValue == null) {
// No more headers
break;
}
if (headerName == null) {
// The header value contains the server's HTTP version
}
}
} catch (Exception e) {
}
}



but I changed the url to my external ip address. and I get the message
nullServerWWW-Authenticatenull

It works when I try localhost on port 80. and I double checked that my router is forwarding to the local ip of this pc.

What is more interesting is that 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?

Share this post


Link to post
Share on other sites

hmm. I set up apache but I dont know how to test if I can connect I tried this code:

public static void main(String[] args)
{
try {
// Create a URLConnection object for a URL
URL url = new URL("http://54.554.54.65:80");
URLConnection conn = url.openConnection();

// List all the response headers from the server.
// Note: The first call to getHeaderFieldKey() will implicit send
// the HTTP request to the server.
for (int i=0; ; i++) {
String headerName = conn.getHeaderFieldKey(i);
System.out.print(headerName);
String headerValue = conn.getHeaderField(i);

if (headerName == null && headerValue == null) {
// No more headers
break;
}
if (headerName == null) {
// The header value contains the server's HTTP version
}
}
} catch (Exception e) {
}
}



but I changed the url to my external ip address. and I get the message
nullServerWWW-Authenticatenull

It works when I try localhost on port 80. and I double checked that my router is forwarding to the local ip of this pc.

What is more interesting is that 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?


Hmm, that looks like the router might intercept port 80 connections, (The routers configuration utility usually runs on port 80 aswell and will require authorization, it shouldn't be accessible from outside your LAN though).

Have you tried accessing your internet IP from a machine that isn't actually on your network (Your router might get confused if you try to connect to your internet ip from within the LAN since the internet IP is also your routers IP (It might think that you're trying to access its configuration tool))

Share this post


Link to post
Share on other sites
So I tried another computer and still no luck. I've been doing some more reading and I believe it has something to do with proxies.

Share this post


Link to post
Share on other sites

So I tried another computer and still no luck. I've been doing some more reading and I believe it has something to do with proxies.


Was that other computer on your LAN or somewhere else ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!