java - Wifi Socket address not valid,Android -
whenever create serversocket
, watch socket address calling getlocalsocketaddress()
, see:
0.0.0.0/0.0.0.0:xxxxx( xxxx random port no)
my code server is:
try{ boolean end = false; serversocket ss = new serversocket(0); system.out.println("program running, server address:" + ss.getlocalsocketaddress().tostring()); while(!end){ //server waiting client here, if needed socket s = ss.accept(); system.out.println("socket connected !"); bufferedreader input = new bufferedreader(new inputstreamreader(s.getinputstream())); printwriter output = new printwriter(s.getoutputstream(),true); //autoflush string st = input.readline(); system.out.println("tcp example client: "+st); output.println("good bye , fish :)"); s.close(); } ss.close(); } catch (exception ex) { ex.printstacktrace(); }
dont assign "0" in serversocket
ports 0-65535 , 1 - 65534 used.. try use ports above 1024, used other known services, telnet, ftp, http, etc...
see code...it might execute code.....
the server side code example:
public class servertest { serversocket s; public void go() { try { s = new serversocket(4445); while (true) { socket incoming = s.accept(); thread t = new thread(new mycon(incoming)); t.start(); } } catch (ioexception e) { e.printstacktrace(); } } class mycon implements runnable { socket incoming; public mycon(socket incoming) { this.incoming = incoming; } @override public void run() { try { printwriter pw = new printwriter(incoming.getoutputstream(), true); inputstreamreader isr = new inputstreamreader( incoming.getinputstream()); bufferedreader br = new bufferedreader(isr); string inp = null; boolean isdone = true; system.out.println("type : bye"); system.out.println(); while (isdone && ((inp = br.readline()) != null)) { system.out.println(inp); if (inp.trim().equals("bye")) { system.out .println("thanks connecting...bye now"); isdone = false; s.close(); } } } catch (ioexception e) { // todo auto-generated catch block try { s.close(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } e.printstacktrace(); } } } public static void main(string[] args) { new servertest().go(); } }
Comments
Post a Comment