Wednesday 15 June 2011

sockets - client Windows and Android server over USB -



sockets - client Windows and Android server over USB -

i'm making android app communicate computer windows. found must easy forwarding port adb commands.

so seek create client/server connection on usb have problems

my server on windows:

public class server { public static void main(string[] args) throws ioexception { system.out.println("echoclient.main()"); socket client = null; // initialize server socket seek { server = new serversocket(38300); server.setsotimeout(timeout * 1000); // effort take connection client = server.accept(); globals.socketout = new printstream(client.getoutputstream()); globals.socketin = new bufferedreader(new inputstreamreader(client.getinputstream())); // globals.socketin.yy } grab (sockettimeoutexception e) { // print out timeout connectionstatus = "connection has timed out! please seek again"; system.out.println(connectionstatus); } grab (ioexception e) { system.out.println("error"+ e); } { // close server socket seek { if (server != null) server.close(); } grab (ioexception ec) { system.out.println("cannot close server socket"+ ec); } } if (client != null) { system.out.println("connected"); } } public static class globals { private static string typeoftransmission ; static printstream socketout = null; static bufferedreader socketin = null; public static synchronized string gettypetransmission(){ homecoming typeoftransmission; } public static synchronized void settypetransmission(string s){ typeoftransmission = s; } } }

android app client

private runnable initializeconnection = new thread() { public void run() { socket client = null; // initialize server socket seek { log.d("ip",getlocalipaddress()); client = new socket(getlocalipaddress(), 38300); globals.socketin = new scanner(new inputstreamreader( client.getinputstream())); globals.socketout = new printwriter(client.getoutputstream()); // globals.socketin.yy } grab (sockettimeoutexception e) { // print out timeout connectionstatus = "connection has timed out! please seek again"; mhandler.post(showconnectionstatus); } grab (ioexception e) { log.e(tag, "" + e); } { // close server socket seek { if (server != null) server.close(); } grab (ioexception ec) { log.e(tag, "cannot close server socket"+ ec); } } if (client != null) { globals.connected = true; // print out success connectionstatus = "connection succesful!"; log.d(tag, "connected!"); mhandler.post(showconnectionstatus); while (globals.socketin.hasnext()) { socketdata = globals.socketin.next(); mhandler.post(socketstatus); } } } }; public string getlocalipaddress(){ try{ for(enumeration<networkinterface> en =networkinterface.getnetworkinterfaces();en.hasmoreelements();){ networkinterface intf = en.nextelement(); for(enumeration<inetaddress> enumipaddress= intf.getinetaddresses();enumipaddress.hasmoreelements();){ inetaddress inetaddress = enumipaddress.nextelement(); if (!inetaddress.isloopbackaddress()){ homecoming inetaddress.gethostaddress().tostring(); } } } }catch(socketexception ex){ log.e("serveractivity",ex.tostring()); } homecoming null; }

logger

01-29 11:43:49.640: d/ip(2981): fe80::a806:ff:fec6:d3d%p2p0 01-29 11:43:49.650: e/connection(2981): java.net.connectexception: failed connect /fe80::a806:ff:fec6:d3d%p2p0%4 (port 38300): connect failed: econnrefused (connection refused)

thank you

android sockets usb client server

No comments:

Post a Comment