LibGDX - TCP for all platforms

Hello all,

First, I don’t know for 100% if this has to be in the java libraries section or here, but I thought it fitted the best here…

Ok, so I made a game with help of LibGDX, its a turn-based strategy, that I want to publish on android and iOS, and maybe on desktop…
Now I want to play against my grandma, so I need to make this multiplayer for at least android and iOS…

I had problems because the ClientSocket is working on java application, but was not working on my android emulator, this caused my error:
ClientSocket socketname = Gdx.net.newClientSocket(Protocol.TCP, “127.0.0.1”, 1337, hints);
I changed “127.0.0.1” (local host) to my internal ip “192.168.1.82” (static ip) and it worked.

Maybe some people are helped with this :wink:

About the android app crash try adding this to the Android app’s manifest file:

I am also developing a game in LibGDX. Since I want to maintain full cross-platform I use this to create a socket:

Socket socket = Gdx.net.newClientSocket(Protocol.TCP, hostname, port, SocketHints hint)

The socket is Gdx.net.Socket
The hostname is a string
The port is an integer
The SocketHints are custom options for the socket, set it to null to use the system’s defaults

The socket has a getInputStream() and getOutputStream() methods which give you a java.io.InputStream and java.io.OutputStream

Hope this helps!

Aha, so the TCP LibGDX provides works well on all platforms?
Sorry, so it is not lacking networking completely (UDP, UDP)

Android manifest must be the evil! :smiley:
I’m starting my android emulator now and I’m gonna try this!

Edit:
Ok, so I put it here:
<manifest

blabla..

And if I click at the Permissions tab I see: android.permission.INTERNET, so it seems all fine, but I still get the same error, Unfortunately myGame has stopped

What do I do wrong?

Can you try to post the stacktrace from LogCat here, I can’t do much without it.

Code:
SocketHints hints = new SocketHints();
server = Gdx.net.newClientSocket(Protocol.TCP, “127.0.0.1”, 1337, hints);
try {
server.getOutputStream().write(“Hello, baby!\n”.getBytes());
} catch (IOException e) {
Gdx.app.log(“OK”, “COULDNT WRITE TO SERVER!”);//just for debugging
}

07-29 20:02:07.336: I/ARMAssembler(847): generated scanline__00000177:03545444_00009001_00000000 [159 ipp] (215 ins) at [0x4cc0a1f0:0x4cc0a54c] in 2424793 ns
07-29 20:02:09.286: W/dalvikvm(847): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
07-29 20:02:09.306: E/AndroidRuntime(847): FATAL EXCEPTION: GLThread 81
07-29 20:02:09.306: E/AndroidRuntime(847): com.badlogic.gdx.utils.GdxRuntimeException: Error making a socket connection to 127.0.0.1:1337
07-29 20:02:09.306: E/AndroidRuntime(847): at com.badlogic.gdx.backends.android.AndroidSocket.(AndroidSocket.java:55)
07-29 20:02:09.306: E/AndroidRuntime(847): at com.badlogic.gdx.backends.android.AndroidNet.newClientSocket(AndroidNet.java:87)
07-29 20:02:09.306: E/AndroidRuntime(847): at com.herjan.MultiPlayerMenu.(MultiPlayerMenu.java:20)
07-29 20:02:09.306: E/AndroidRuntime(847): at com.herjan.MenuInputHandler.touchUp(MenuInputHandler.java:48)
07-29 20:02:09.306: E/AndroidRuntime(847): at com.badlogic.gdx.backends.android.AndroidInput.processEvents(AndroidInput.java:359)
07-29 20:02:09.306: E/AndroidRuntime(847): at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:456)
07-29 20:02:09.306: E/AndroidRuntime(847): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
07-29 20:02:09.306: E/AndroidRuntime(847): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
07-29 20:02:09.306: E/AndroidRuntime(847): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 1337) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)
07-29 20:02:09.306: E/AndroidRuntime(847): at libcore.io.IoBridge.isConnected(IoBridge.java:224)
07-29 20:02:09.306: E/AndroidRuntime(847): at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
07-29 20:02:09.306: E/AndroidRuntime(847): at libcore.io.IoBridge.connect(IoBridge.java:112)
07-29 20:02:09.306: E/AndroidRuntime(847): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-29 20:02:09.306: E/AndroidRuntime(847): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-29 20:02:09.306: E/AndroidRuntime(847): at java.net.Socket.connect(Socket.java:842)
07-29 20:02:09.306: E/AndroidRuntime(847): at com.badlogic.gdx.backends.android.AndroidSocket.(AndroidSocket.java:48)
07-29 20:02:09.306: E/AndroidRuntime(847): … 7 more
07-29 20:02:09.306: E/AndroidRuntime(847): Caused by: libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
07-29 20:02:09.306: E/AndroidRuntime(847): at libcore.io.IoBridge.isConnected(IoBridge.java:208)
07-29 20:02:09.306: E/AndroidRuntime(847): … 13 more
07-29 20:02:14.906: I/Process(847): Sending signal. PID: 847 SIG: 9

From these copied messages: first was green, then orange, then all red, except the last (green) one

Is the server running on the computer you are running the emulator from? If it is try using the computers internal ip address (ex: 192.168.1.x) instead of localhost

Wauw, thanks, that works, LOL!

Epic error… Whole day f**ked up because of… THIS… Just searched everywhere for solutions, is it this… Im curious, why this error, what caused this error?

It has to do with the emulator itself. I learned this after spending two hours doing trial and error on a rcon client for android just to find the emulator caused it. Glad to here it works. Good luck on your game!

Thanks, just stay checking this forum, I will post it in the WIP section when the game is ready for it :wink: