Java socket connection time out in Android Implementation -
i'm writing simple android code file transfer on java sockets. first of all, hard coded file path in server implementation check if files received @ client side or not. worked fine. created simple file browser separate java class user browse through files , select them well. purpose, created simple intent launch file browser class (the intent fired after socket initialization). problem starts here. before file browser thing, app working fine (with pre-defined set of files). now, i'm getting socket closed exception every time run app. doesn't reach intent firing stage. please me. i'm self taught , still learning android programming. here code snippets:
server:
public class mainactivity extends activity { arraylist<file> filearray = new arraylist<file>(); arraylist<string> filepathnamearray = new arraylist<string>(); socket socket = null; serversocket serversocket = null; int request_code = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); thread trd = new thread(new runnable() { @override public void run() { initializeserver(); intent launchfilemanager = new intent(getbasecontext(), filechooseractivity.class); startactivityforresult(launchfilemanager, request_code); if (!filearray.isempty()) { (int = 0; < filearray.size(); i++) copyfile(filearray.get(i), filearray.get(i).getname()); } } }); trd.start(); } private void initializeserver() { try { serversocket = new serversocket(4444); } catch (ioexception e) { e.printstacktrace(); log.d("listen failed", "listening port 4444 failed"); } try { socket = serversocket.accept(); } catch (ioexception e) { e.printstacktrace(); log.d("binding failed", "couldn't accept socket connection"); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_code) { if (resultcode == result_ok) { filepathnamearray = data.getextras().getstringarraylist("key");// file uri received for(int f=0;f<filepathnamearray.size();f++){ filearray.add(new file(filepathnamearray.get(f))); //create file array } } if (resultcode == result_canceled) { toast canceltoast = toast.maketext(getbasecontext(), "nothing selected", toast.length_short); canceltoast.show(); } } }
filechooser returning intent result:
selectiondone.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // selfilepathlist file path list of selected files intent returnintent=new intent(); returnintent.putstringarraylistextra("key", selfilepathlist); setresult(result_ok, returnintent); finish(); } }); }
client:
public class receiver { static socket socket = null; public static void main(string[] args) { inetaddress serverip; try { serverip = inetaddress.getbyname("10.87.3.91"); socket = new socket(serverip, 4444); if (socket != null) { system.out.println("connection established!!"); (int j = 0; j < 5; j++) receivedos(); } } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } }
i've added necessary manifest permission:
<uses-sdk android:minsdkversion="8" android:targetsdkversion="18" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.change_wifi_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage"/>
console output:
03-24 22:26:02.452: w/system.err(22973): java.net.socketexception: socket closed 03-24 22:26:02.452: w/system.err(22973): @ libcore.io.posix.accept(native method) 03-24 22:26:02.452: w/system.err(22973): @ libcore.io.blockguardos.accept(blockguardos.java:55) 03-24 22:26:02.452: w/system.err(22973): @ java.net.plainsocketimpl.accept(plainsocketimpl.java:98) 03-24 22:26:02.452: w/system.err(22973): @ java.net.serversocket.implaccept(serversocket.java:202) 03-24 22:26:02.452: w/system.err(22973): @ java.net.serversocket.accept(serversocket.java:127) 03-24 22:26:02.452: w/system.err(22973): @ com.example.zztest.mainactivity.initializeserver(mainactivity.java:57) 03-24 22:26:02.452: w/system.err(22973): @ com.example.zztest.mainactivity.access$0(mainactivity.java:49) 03-24 22:26:02.452: w/system.err(22973): @ com.example.zztest.mainactivity$1.run(mainactivity.java:36) 03-24 22:26:02.462: w/system.err(22973): @ java.lang.thread.run(thread.java:856) 03-24 22:26:02.462: d/binding failed(22973): couldn't accept socket connection
any idea or suggestion highly appreciated. in advance!
Comments
Post a Comment