java - Simple client server program with local IP address -
i'm trying started basic client server stuff. have java server socket program running on 1 of computers. android phone running simple application , acting client. want phone able connect java program on computer.
the client tries connect computer's local ip address, can't connect. ioexception. here's android code i'm using:
try { echosocket = new socket("192.168.0.19", 7077); system.out.println("length: "+ "socket created!"); } catch (unknownhostexception e) { system.out.println("length: "+ "don't know host."); } catch (ioexception e) { system.out.println("couldn't i/o " + "the connection."); }
if run both client , server, on computer , try connect computer's local ip (same above), works fine. want connect phone.
any ideas need do?
edit: here's stack trace:
07-21 15:37:34.223: w/system.err(3474): android.os.networkonmainthreadexception 07-21 15:37:34.223: w/system.err(3474): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 07-21 15:37:34.223: w/system.err(3474): @ libcore.io.blockguardos.connect(blockguardos.java:84) 07-21 15:37:34.223: w/system.err(3474): @ libcore.io.iobridge.connecterrno(iobridge.java:127) 07-21 15:37:34.223: w/system.err(3474): @ libcore.io.iobridge.connect(iobridge.java:112) 07-21 15:37:34.223: w/system.err(3474): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 07-21 15:37:34.223: w/system.err(3474): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:172) 07-21 15:37:34.223: w/system.err(3474): @ java.net.socket.startupsocket(socket.java:566) 07-21 15:37:34.223: w/system.err(3474): @ java.net.socket.tryalladdresses(socket.java:127) 07-21 15:37:34.223: w/system.err(3474): @ java.net.socket.<init>(socket.java:177) 07-21 15:37:34.223: w/system.err(3474): @ java.net.socket.<init>(socket.java:149) 07-21 15:37:34.223: w/system.err(3474): @ com.nikhildev.cricketeye.mainactivity.startvideocamera(mainactivity.java:45) 07-21 15:37:34.223: w/system.err(3474): @ java.lang.reflect.method.invokenative(native method) 07-21 15:37:34.223: w/system.err(3474): @ java.lang.reflect.method.invoke(method.java:511) 07-21 15:37:34.223: w/system.err(3474): @ android.view.view$1.onclick(view.java:3594) 07-21 15:37:34.223: w/system.err(3474): @ android.view.view.performclick(view.java:4204) 07-21 15:37:34.223: w/system.err(3474): @ android.view.view$performclick.run(view.java:17355) 07-21 15:37:34.223: w/system.err(3474): @ android.os.handler.handlecallback(handler.java:725) 07-21 15:37:34.223: w/system.err(3474): @ android.os.handler.dispatchmessage(handler.java:92) 07-21 15:37:34.223: w/system.err(3474): @ android.os.looper.loop(looper.java:137) 07-21 15:37:34.223: w/system.err(3474): @ android.app.activitythread.main(activitythread.java:5041) 07-21 15:37:34.233: w/system.err(3474): @ java.lang.reflect.method.invokenative(native method) 07-21 15:37:34.233: w/system.err(3474): @ java.lang.reflect.method.invoke(method.java:511) 07-21 15:37:34.233: w/system.err(3474): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-21 15:37:34.233: w/system.err(3474): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-21 15:37:34.233: w/system.err(3474): @ dalvik.system.nativestart.main(native method)
with android , sockets need watch out few things
- android needs client, since ip address expected change more of server
- the port number use should > 1024
- if on emulator, need aware emulator's ip 10.something. android documentation.
- permissions in manifest
lastly, issue running network api in main ui thread. need run same in async task. thats why getting strictmode exception thingy.
edit :
as ping, check if can ping network router. , if can ping outside world issue router blocking ping inside.
Comments
Post a Comment