java - Client Program on PC connect to Server program on Android -
i trying make simple server app , client program in order extend
their work in later time use them in graduation project
i have client code java simple program on pc
and have android thread in put server programm on android phone
the server should listen ports
and register them on accept
the problem when run emulator , when use local address "127.0.0.1"
on both client , server first start server , launches thread succefullt
but when run client programm catches "connection refused exception "
when build apk , launch server android phone on ip "192.168.3.100" server start
the client runs correctly ,but when send number nothing shows in android activity
any advice appreciated
my server code
package com.example.laptophome.securesys; /** * created laptop home on 13-apr-17. */ import android.content.context; import android.os.looper; import android.widget.toast; import java.io.inputstreamreader; import java.net.inetsocketaddress; import java.net.socket; import java.nio.channels.selectionkey; import java.nio.channels.selector; import java.nio.channels.serversocketchannel; import java.nio.channels.socketchannel; import java.util.iterator; public class connection extends thread { private boolean isbinded = false; private string ipaddress; private context cx; private int num; //private socket socket; private inputstreamreader inread; public serversocketchannel server; // private volatile boolean isrunning =true; public connection(string ipaddress ,context cx) { this.ipaddress = ipaddress; this.cx = cx; } @override public void run() { try { selector selector = selector.open(); int[] ports = {5050, 4001, 6000}; (int port : ports) { server = serversocketchannel.open(); server.configureblocking(false); server.socket().bind(new inetsocketaddress(ipaddress, port)); server.register(selector, selectionkey.op_accept); } while (selector.isopen()) { selector.select(); iterator iterator = selector.selectedkeys().iterator(); if (!iterator.hasnext()){toast.maketext(cx, "iterrator empty ", toast.length_short).show();} while (iterator.hasnext()) { selectionkey key = (selectionkey) iterator.next(); if (key.isacceptable()) { socketchannel client = server.accept(); if (client != null) { toast.maketext(cx, "new image recieved", toast.length_short).show(); //this toas doesn't appear ever socket socket = client.socket(); inread = new inputstreamreader(socket.getinputstream()); num = inread.read(); toast.maketext(cx, "new = " + num, toast.length_short).show(); } } } } } catch (exception e) { looper.prepare(); toast.maketext(cx,"error" + e.getmessage().tostring(),toast.length_short).show(); } }}
my client program
public class client { public static void main(string args[]) throws ioexception{ string num=" "; scanner sc= new scanner(system.in); try { socket s = new socket("192.168.3.100",5050); while(true){ printstream p = new printstream(s.getoutputstream()); system.out.println(p.tostring()); num=sc.next(); p.println(num); } } catch (exception e ) { system.out.println(e);}}}
Comments
Post a Comment