libGDX includes some classes for cross-platform network operations. These classes are more commonly known as Gdx.net (source)

Features

  • Cross-platform HTTP requests
  • Multi-platform TCP client and server socket support (excludes GWT) with configurable settings
  • Optimized TCP client and server settings aiming for low-latency
  • Cross-platform browser access. (ex: You can create a link to your website in game and it will open the browser on all platforms.)

Implementation

Class Explanation:

  • Net.java is an interface used for the cross-platform networking. This is where you can get the objects needed to communicate with the network.
  • Socket.java is an interface that provides you with the remote socket address, connection state, and a java.io.InputStream and java.io.OutputStream to work with the socket.
  • SocketHints.java is a class used to configure TCP client sockets
  • ServerSocket.java is an interface used to create TCP server sockets. It provides the standard accept() method to get a TCP client that connected.
  • ServerSocketHints.java is a class used to configure TCP server sockets.
  • HttpStatus.java is a class used to give an easy way to see what the status code returned is.
  • HttpParameterUtils.java is a class used to provide utility methods for HTTP requests.
  • HttpRequestBuilder is a class to help with creating HttpRequests.

To create a TCP client socket use this little piece of code:

  1. Socket socket = Gdx.net.newClientSocket(Protocol protocol, String host, int port, SocketHints hints);

To create a TCP server socket use this:

  1. ServerSocket server = Gdx.net.newServerSocket(Protocol protocol, int port, ServerSocketHints hints);

To send an HTTP Request use this:

  1. HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
  2. HttpRequest httpRequest = requestBuilder.newRequest().method(HttpMethods.GET).url("http://www.google.de").build();
  3. Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);

To send a GET HTTP Request with arguments use this:

  1. HttpRequestBuilder requestBuilder = new HttpRequestBuilder();
  2. HttpRequest httpRequest = requestBuilder.newRequest().method(HttpMethods.GET).url("http://www.google.de").content("q=libgdx&example=example").build();
  3. Gdx.net.sendHttpRequest(httpRequest, httpResponseListener);

To open the system browser use this:

  1. Gdx.net.openURI(String URI)

Receiving Response

There are different technique to flexibly receive response back from HTTP request as shown above. Example in Kotlin as follows.

  1. Via class which implemented HttpResponseListener

    For this, you have to create a class and implements HTTPResponseListener, then supply a method parameter with instance of such class.

    1. class MyReceiverOfResult : HttpResponseListener {
    2. override fun cancelled() {
    3. // do something when request gets cancelled
    4. }
    5. override fun failed(t: Throwable?) {
    6. // do something when it fails
    7. }
    8. override fun handleHttpResponse(httpResponse: Net.HttpResponse) {
    9. // do something when gets result back
    10. }
    11. }
    12. ...
    13. // assume you hold instance of such class as variable namely `receiver`.
    14. // then you just pass it to `sendHttpRequest()` method
    15. Gdx.net.sendHttpRequest(req, receiver)
  2. Anonymous object

    1. Gdx.net.sendHttpRequest(req, object: Net.HttpResponseListener {
    2. override fun cancelled() {
    3. // do something when request gets cancelled
    4. }
    5. override fun failed(t: Throwable?) {
    6. // do something when it fails
    7. }
    8. override fun handleHttpResponse(httpResponse: Net.HttpResponse) {
    9. // do something when gets result back
    10. }
    11. })

Depends on your use case and which way you find it more flexible to do so.

Notes

There are various notes needed when working with networking on different platforms.

  1. TCP client and server sockets do not work on GWT. This is due to java.net not being supported on GWT and there is not a viable alternative at this point other than websockets.
  2. Opening the browser is not supported on the headless backend, Android Daydreams, or Android Live Wallpapers. This is due to the limitations with the implementation and/or platform.
  3. On Android: You must have the following permission declared in the AndroidManifest.xml file to access the network: <uses-permission android:name="android.permission.INTERNET" />
  4. On Android: You cannot access the network on the main thread without disabling strict mode. This is done to prevent network operations from hanging the main thread. See here
  5. When targeting mobile devices: Be careful about how you implement networking. The wireless radios themselves are a big power drain when on. Also be careful about data limits that could be imposed on a 1G/2G/3G/4G LTE network. libGDX has configuration optimizations done to allow low-latency, but still have the benefits of TCP.
  6. Supported networking configurations vary between backend and java implementation.
  7. Battery drain is more common when data is being sent and received due to the power needed by the radios.
  8. Be sure to set the Content-Type header for POST requests. Not all backends default to the same value (due to differences in the underlying implementations). The most common value for this header is application/x-www-form-urlencoded; however, depending on the type of data you are sending, you may need a different value (such as application/xml or application/json).

See Also

Great articles about mobile data battery efficiency here

Source code for Gdx.net classes here