android - Searching the Server IP on Client instead of inputing -
server: pc
client: android
so client/server app consists in opening webpages , executing bot, fine if use router, able to connect in different places (different router/pc). searching "wi-fi search of ip" , got nothing. possible give server side fix ip? 192.168.1.68?
client code
public class androidclient extends activity { edittext episode; spinner spinner1; string result; button buttonconnect, buttonclear; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); episode = (edittext) findviewbyid(r.id.episode); buttonconnect = (button) findviewbyid(r.id.connect); buttonclear = (button) findviewbyid(r.id.clear); spinner1 = (spinner) findviewbyid(r.id.animes); arrayadapter<charsequence> adapter = arrayadapter.createfromresource(this, r.array.anime, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner1.setadapter(adapter); addlisteneronspinneritemselection(); addlisteneronbutton(); } public void addlisteneronspinneritemselection() { spinner1.setonitemselectedlistener(new customonitemselectedlistener()); } public void addlisteneronbutton() { spinner1 = (spinner) findviewbyid(r.id.animes); buttonconnect = (button) findviewbyid(r.id.connect); buttonconnect.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { myclienttask myclienttask = new myclienttask(); myclienttask.execute(); } }); } public class myclienttask extends asynctask<void, void, void> { @override protected void doinbackground(void... arg0) { socket socket = null; dataoutputstream dataoutputstream = null; datainputstream datainputstream = null; try { socket = new socket("10.1.3.68", 8080); dataoutputstream = new dataoutputstream( socket.getoutputstream()); datainputstream = new datainputstream(socket.getinputstream()); dataoutputstream.writeutf(episode.gettext() + "-" + string.valueof(spinner1.getselecteditem())); } catch (unknownhostexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } { if (socket != null) { try { socket.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } if (dataoutputstream != null) { try { dataoutputstream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } if (datainputstream != null) { try { datainputstream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } return null; } @override protected void onpostexecute(void result) { } } }
server code
public class serverside extends application { textfield texttitle; label labelsys, labelport, labelip; label labelmsg; checkbox optwelcome; serversocket serversocket; string message = ""; string result; @override public void start(stage primarystage) { texttitle = new textfield(); labelsys = new label(); labelport = new label(); labelip = new label(); labelmsg = new label(); labelip.settext(getipaddress()); vbox mainlayout = new vbox(); mainlayout.setpadding(new insets(5, 5, 5, 5)); mainlayout.setspacing(5); mainlayout.getchildren().addall(texttitle, labelsys, labelport, labelip, labelmsg); stackpane root = new stackpane(); root.getchildren().add(mainlayout); scene scene = new scene(root, 300, 400); primarystage.settitle("one piece"); primarystage.setscene(scene); primarystage.show(); thread socketserverthread = new thread(new socketserverthread()); socketserverthread.setdaemon(true); socketserverthread.start(); } public static void main(string[] args) { launch(args); } private class socketserverthread extends thread { static final int socketserverport = 8080; int count = 0; @override public void run() { try { socket socket = null; serversocket = new serversocket(socketserverport); platform.runlater(new runnable() { @override public void run() { labelport.settext("i'm waiting here: " + serversocket.getlocalport()); } }); while (true) { socket = serversocket.accept(); count++; //start thread //to prevent blocked empty datainputstream thread acceptedthread = new thread( new serversocketacceptedthread(socket, count)); acceptedthread.setdaemon(true); //terminate thread when program end acceptedthread.start(); } } catch (ioexception ex) { logger.getlogger(serverside.class.getname()).log(level.severe, null, ex); } } } private class serversocketacceptedthread extends thread { socket socket = null; datainputstream datainputstream = null; dataoutputstream dataoutputstream = null; int count; serversocketacceptedthread(socket s, int c) { socket = s; count = c; } @override public void run() { try { datainputstream = new datainputstream( socket.getinputstream()); dataoutputstream = new dataoutputstream( socket.getoutputstream()); //if datainputstream empty, //this thread blocked readutf(), //but not others string messagefromclient = datainputstream.readutf(); message += "#" + count + " " + socket.getinetaddress() + ":" + socket.getport() + "\n" + "msg client: " + messagefromclient + "\n"; platform.runlater(new runnable() { @override public void run() { labelmsg.settext(message); } }); string string = messagefromclient; string[] parts = string.split("-"); string episode = parts[0]; string anime = parts[1]; string onep = new string("one piece"); string naruto = new string ("naruto"); string bleach = new string ("bleach"); int epnumb = integer.parseint(episode); if (epnumb < 10) { result = "00" + episode; } else if (epnumb < 100 && epnumb >= 10) { result = "0" + episode; } else { result = episode; } if (anime.equals(onep)){ try { desktop desktop = java.awt.desktop.getdesktop(); uri ourl = new uri("http://kissanime.com/anime/one-piece/episode-"+ result); desktop.browse(ourl); } catch (exception e) { e.printstacktrace(); }} else { try { desktop desktop = java.awt.desktop.getdesktop(); uri ourl = new uri("http://kissanime.com/anime/naruto-shippuuden/episode-"+result); desktop.browse(ourl); } catch (exception e) { e.printstacktrace(); } } } catch (ioexception ex) { logger.getlogger(serverside.class.getname()).log(level.severe, null, ex); } { if (socket != null) { try { socket.close(); } catch (ioexception ex) { logger.getlogger(serverside.class.getname()).log(level.severe, null, ex); } } if (datainputstream != null) { try { datainputstream.close(); } catch (ioexception ex) { logger.getlogger(serverside.class.getname()).log(level.severe, null, ex); } } if (dataoutputstream != null) { try { dataoutputstream.close(); } catch (ioexception ex) { logger.getlogger(serverside.class.getname()).log(level.severe, null, ex); } } } } } private java.lang.string parseint(java.lang.string episode) { // todo auto-generated method stub return null; } private string getipaddress() { string ip = ""; try { enumeration<networkinterface> enumnetworkinterfaces = networkinterface .getnetworkinterfaces(); while (enumnetworkinterfaces.hasmoreelements()) { networkinterface networkinterface = enumnetworkinterfaces .nextelement(); enumeration<inetaddress> enuminetaddress = networkinterface .getinetaddresses(); while (enuminetaddress.hasmoreelements()) { inetaddress inetaddress = enuminetaddress.nextelement(); if (inetaddress.issitelocaladdress()) { ip += "sitelocaladdress: " + inetaddress.gethostaddress() + "\n"; } } } } catch (socketexception ex) { logger.getlogger(serverside.class.getname()).log(level.severe, null, ex); } return ip; } public datainputstream string(string string) { return null; } }
you can use serversocket(int port, int backlog, inetaddress bindaddr)
constructs new serversocket instance bound given localaddress , port.
Comments
Post a Comment