Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX - TCP for all platforms  (Read 2921 times)
0 Members and 1 Guest are viewing this topic.
Offline Herjan
« Posted 2013-07-29 14:41:27 »

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

Offline jca2323

Senior Newbie


Medals: 1



« Reply #1 - Posted 2013-07-29 18:14:07 »

About the android app crash try adding this to the Android app's manifest file:
<uses-permission android:name="android.permission.INTERNET" />

I am also developing a game in LibGDX. Since I want to maintain full cross-platform I use this to create a socket:
1  
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!
Offline Herjan
« Reply #2 - Posted 2013-07-29 19:28:06 »

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! Cheesy
I'm starting my android emulator now and I'm gonna try this!

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

<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />

<application>
blabla..
</application>

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?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jca2323

Senior Newbie


Medals: 1



« Reply #3 - Posted 2013-07-29 19:52:16 »

Can you try to post the stacktrace from LogCat here, I can't do much without it.
Offline Herjan
« Reply #4 - Posted 2013-07-29 20:01:16 »

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.<init>(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.<init>(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.<init>(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

Offline jca2323

Senior Newbie


Medals: 1



« Reply #5 - Posted 2013-07-29 20:06:27 »

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
Offline Herjan
« Reply #6 - Posted 2013-07-29 20:10:53 »

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?

Offline jca2323

Senior Newbie


Medals: 1



« Reply #7 - Posted 2013-07-29 20:12:15 »

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!
Offline Herjan
« Reply #8 - Posted 2013-07-29 20:20:34 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (36 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (41 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (35 views)
2014-09-21 00:26:15

UprightPath (48 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!