• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Java, how to make a server

Status
Not open for further replies.
Level 19
Joined
Jul 2, 2011
Messages
2,162
Hello,

I have been experimenting with making a server with java, so far I've been able to to transfer files easily over lan

but....

how do you transfer files over the internet using java? I really have no idea

help!
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,182
You could do it easily in HTML.
It has built in support for uploading files to the server.

It do not remember the specific attribute given, but it can be done with a <button></button>, minimal effort really.

I have not used ASP.net myself yet, but I know it exists. C# and java are pretty much identical so you could probably use it easily if you know java.
The main benefit of using a website is that the built in support for communication does not need to be coded by you.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
You could do it easily in HTML.
It has built in support for uploading files to the server.

It do not remember the specific attribute given, but it can be done with a <button></button>, minimal effort really.

I have not used ASP.net myself yet, but I know it exists. C# and java are pretty much identical so you could probably use it easily if you know java.
The main benefit of using a website is that the built in support for communication does not need to be coded by you.
sorry I only really know java
do you know any?
I mean do you know any java solutions?
 
Level 25
Joined
May 11, 2007
Messages
4,651
First step to code is to learn how to google.
You will find lots of tutorials for setting up Java servers if you google it.
nF7bYgv.jpg
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
First step to code is to learn how to google.
You will find lots of tutorials for setting up Java servers if you google it.
nF7bYgv.jpg
every Google example I've found only shows how to create a local connection. it's actually bloody annoying because even the ones that say they created an online connection, use a lan example to demonstrate their answer

I just need to know how do you set up the the socket to look on the net not your computer
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,178
how do you transfer files over the internet using java? I really have no idea
The same way you transfer them over LAN? Seeing how LAN likely uses IPv4/6 which is the same protocols used by the internet.
You would have to port forward on your device which is running the server.
Only if it is behind a firewall or NAT.
what if they have a dynamic IP address?
Well one approach around that is to use a domain name. One can use domain servers to look up the domain name and they will return the IP address. The server itself can query its own IP address and notify the domain names if it changes. Unlike IP addresses which might change with some ISPs, domain names are immutable once owned. Owning a domain name is often not free.

If you are behind a NAT then one binds the listening socket to the local IP address of the server. One then configures the NAT to port forward incoming communications targeting the public IP address to the server IP address. When someone on the internet targets that public IP address the NAT will forward the packet to the local IP address of the server and it will be received by the socket. Forward mappings usually need the protocol and port ranges to forward defined. If your server is the only system on the network one can assign it to the DMZ, where the router will forward all unexpected incoming packets to it. Be aware that some ISPs might block incoming traffic of certain types, especially for HTTP or email servers as they want you to pay for a premium business connection for those.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
The same way you transfer them over LAN? Seeing how LAN likely uses IPv4/6 which is the same protocols used by the internet.
Only if it is behind a firewall or NAT.
Well one approach around that is to use a domain name. One can use domain servers to look up the domain name and they will return the IP address. The server itself can query its own IP address and notify the domain names if it changes. Unlike IP addresses which might change with some ISPs, domain names are immutable once owned. Owning a domain name is often not free.

If you are behind a NAT then one binds the listening socket to the local IP address of the server. One then configures the NAT to port forward incoming communications targeting the public IP address to the server IP address. When someone on the internet targets that public IP address the NAT will forward the packet to the local IP address of the server and it will be received by the socket. Forward mappings usually need the protocol and port ranges to forward defined. If your server is the only system on the network one can assign it to the DMZ, where the router will forward all unexpected incoming packets to it. Be aware that some ISPs might block incoming traffic of certain types, especially for HTTP or email servers as they want you to pay for a premium business connection for those.

none of that information helps, does anyone go to you and come back with a working solution

the only thing you are succeeding at is frustrating us
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
Maybe post the code that works for a local network and people can explain how to adapt it for working on the public internet?
That is a great suggestion, it might help to see an actual example of how a server is made and then an example of how to make it work over the net instead of just over lan

Server Side
Code:
import java.net.*;

class TCPServer {
public static void main(String argv[]) throws Exception {
  String clientSentence;
  String capitalizedSentence;
  ServerSocket welcomeSocket = new ServerSocket(6789);

  while (true) {
   Socket connectionSocket = welcomeSocket.accept();
   BufferedReader inFromClient =
    new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
   DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
   clientSentence = inFromClient.readLine();
   System.out.println("Received: " + clientSentence);
   capitalizedSentence = clientSentence.toUpperCase() + '\n';
   outToClient.writeBytes(capitalizedSentence);
  }
}
}

Client Side
Code:
import java.net.*;

class TCPClient {
public static void main(String argv[]) throws Exception {
  String sentence;
  String modifiedSentence;
  BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  Socket clientSocket = new Socket("localhost", 6789);
  DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
  BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  sentence = inFromUser.readLine();
  outToServer.writeBytes(sentence + '\n');
  modifiedSentence = inFromServer.readLine();
  System.out.println("FROM SERVER: " + modifiedSentence);
  clientSocket.close();
}
}
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,178
The server side code is already internet ready. All that needs to be done is change the client code to target the server's public IP address rather than "localhost". In the case of IPv4 addresses this will be something in the form of "XXX.XXX.XXX.XXX" or in the case of IPv6 like "XXXX::XXXX:XXXX".
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
The server side code is already internet ready. All that needs to be done is change the client code to target the server's public IP address rather than "localhost". In the case of IPv4 addresses this will be something in the form of "XXX.XXX.XXX.XXX" or in the case of IPv6 like "XXXX::XXXX:XXXX".
is there any way to retrieve that address automatically?


for instance like when I want to run it on a friends computer

I've seen it been done before but I have no idea how
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,178
is there any way to retrieve that address automatically?
Most web services bind their server public IP address to a domain name. The client then performs domain name lookup on that domain name which returns back an IP address to connect to the server.

One would then connect to it somewhat like...
Code:
Socket clientSocket = new Socket("hiveworkshop.com", 6789);
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
Most web services bind their server public IP address to a domain name. The client then performs domain name lookup on that domain name which returns back an IP address to connect to the server.

One would then connect to it somewhat like...
Code:
Socket clientSocket = new Socket("hiveworkshop.com", 6789);
How would you go about retrieving data from a web domain?

I've actually heard this solution before only it said you would have to create a web domain, this if it is possible is a better solution than creating a website. Yet I do not understand how the data would then be retrieved?
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,178
How would you go about retrieving data from a web domain?
Using a URI. Not sure what this has to do with your earlier question.
I've actually heard this solution before only it said you would have to create a web domain, this if it is possible is a better solution than creating a website. Yet I do not understand how the data would then be retrieved?
The data would be retrieved however you plan it to be retrieved. If you make a website you can retrieve resources with URIs. If you make a service on the server then you can connect directly to it from its IP/domain name and send data directly to/from the server. Without knowing what you want your program to do I cannot suggest how to ship data from client to server, I can only describe various approaches.

By the sounds of it you have very little understanding of how the internet works. I suggest spending a few hours on Wikipedia learning about the internet, IPv4/6, domain names and protocols like TCP, HTTP, FTP, SSL etc.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
Using a URI. Not sure what this has to do with your earlier question.
The data would be retrieved however you plan it to be retrieved. If you make a website you can retrieve resources with URIs. If you make a service on the server then you can connect directly to it from its IP/domain name and send data directly to/from the server. Without knowing what you want your program to do I cannot suggest how to ship data from client to server, I can only describe various approaches.

By the sounds of it you have very little understanding of how the internet works. I suggest spending a few hours on Wikipedia learning about the internet, IPv4/6, domain names and protocols like TCP, HTTP, FTP, SSL etc.
URI you say?

I don't understand how the URI will get there though but I guess it won't hurt to try... hmm this also means that the end user would have to know what URI is there in order to retrieve it? Huh so many questions but I guess again I will only find out if it is possible if I try and make it work

In a lan you send a message directly to the user telling them what files are available to retrieve. However with this internet domain idea you can't do that, so how would the user know what files are available? That is all I'm saying for now, I'll have to test it to find out more
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
nah it's ok I've got the answer

it's super easy ^-^

Why do you still need help a month later with this super easy thing?

You know what the irony is? this time it really is a super easy thing, because there is literally no difference, as far as your code knows, between connecting to localhost:80 or connecting to hiveworkshop:80. All of that is handled by external things (router, DNS servers, etc.)

If you know the server's IP, you can of course connect to it directly. Put 207.154.206.177 in your browser and it connects to the Hive. Truly magical.

If you want your own domain name, which is reasonable, you can register one for free on all sorts of free DNS services (I use FreeDNS - Free DNS - Dynamic DNS - Static DNS subdomain and domain hosting).
The next step is to setup your router such that it will update your IP in the DNS service every some N time, assuming your ISP gave you a dynamic IP that changes once in a while and/or every time the router is rebooted. This is called Dynamic DNS, which every half-decent router should give you access to (otherwise a script running locally on your computer can do the same).

Lastly, you will obviously need to open whatever port you want to listen to in your router, assuming it has its own NAT (which it undoubtedly has).
 
Last edited:
Level 19
Joined
Jul 2, 2011
Messages
2,162
Why do you still need help a month later with this super easy thing?

You know what the irony is? this time it really is a super easy thing, because there is literally no difference, as far as your code knows, between connecting to localhost:80 or connecting to hiveworkshop:80. All of that is handled by external things (router, DNS servers, etc.)

If you know the server's IP, you can of course connect to it directly. Put 207.154.206.177 in your browser and it connects to the Hive. Truly magical.

If you want your own domain name, which is reasonable, you can register one for free on all sorts of free DNS services (I use FreeDNS - Free DNS - Dynamic DNS - Static DNS subdomain and domain hosting).
The next step is to setup your router such that it will update your IP in the DNS service every some N time, assuming your ISP gave you a dynamic IP that changes once in a while and/or every time the router is rebooted. This is called Dynamic DNS, which every half-decent router should give you access to (otherwise a script running locally on your computer can do the same).

Lastly, you will obviously need to open whatever port you want to listen to in your router, assuming it has its own NAT (which it undoubtedly has).
yeah someone told me the solution is to use a http server which works but I don't like it. if your solution really works without any changes to my code then wow that would be amazing!
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,178
yeah someone told me the solution is to use a http server which works but I don't like it.
You did not do the reading I suggested did you? HTTP has nothing to do with domain names. HTTP is a protocol to access resources, along with FTP, FTPS HTTPS, SVN, GIT, etc. Protocols have nothing to do with domain names. For example http://192.168.1.254/ is a resource identifier that uses an IPv4 address rather than a domain name.
if your solution really works without any changes to my code then wow that would be amazing!
Java automatically performs domain name lookup. If one uses C/C++ Berkley sockets then one has to perform them as a separate operation to first resolve the target IP address and then create the sockets.

If you want to simply move files between server and client I recommend using a FTP server on the server. FTP is an old but pretty standardised way of sending files between computers. Even Windows Explorer natively supports interacting with FTP servers, although in a buggy sort of way. Of course FTP is not the only way.
 
Level 19
Joined
Jul 2, 2011
Messages
2,162
You did not do the reading I suggested did you? HTTP has nothing to do with domain names. HTTP is a protocol to access resources, along with FTP, FTPS HTTPS, SVN, GIT, etc. Protocols have nothing to do with domain names. For example http://192.168.1.254/ is a resource identifier that uses an IPv4 address rather than a domain name.
Java automatically performs domain name lookup. If one uses C/C++ Berkley sockets then one has to perform them as a separate operation to first resolve the target IP address and then create the sockets.

If you want to simply move files between server and client I recommend using a FTP server on the server. FTP is an old but pretty standardised way of sending files between computers. Even Windows Explorer natively supports interacting with FTP servers, although in a buggy sort of way. Of course FTP is not the only way.
ok tried it your way and surprisingly it sort of works

it brings up an error, 'connection timed out' but I'm really surprised it works at all.

it is saying connection timed out because I don't have assess to that site but that just means I need to find a site that grants access

I still wish I didn't have to go though a site but it deems direct computer to computer is not possible
 
Status
Not open for further replies.
Top