java - Android telnet client through apache commons -
i new android, want write telnet client app android. have written code using apache commons library telnet. able connect telnet server (for have used asyctask concept) , able read login banner after long time don't know causing that. have modified example given in apache commons work android code.i have 2 java codes in "src" folder (mainactivity.java , telnetclientexample.java). first, want login prompt , want user interact please help, in advance :) posting screen capture of getting.
mainactivity.java
package com.example.telnetapp; import java.io.bufferedinputstream; import java.io.inputstream; import java.io.printwriter; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity{ public static int port_int_address; private edittext server,port; private button connect; public static string ip,port_address; public printwriter out; public static textview textresponse; static string response; public inputstream instr; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); server = (edittext)findviewbyid(r.id.edittext1); port = (edittext)findviewbyid(r.id.edittext2); connect = (button)findviewbyid(r.id.connect); textresponse = (textview)findviewbyid(r.id.response); connect.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ip = server.gettext().tostring(); port_address = port.gettext().tostring(); port_int_address = integer.parseint(port_address); try{ myclienttask task = new myclienttask(ip, port_int_address); task.execute(); } catch(exception e){toast.maketext(getapplicationcontext(), e.tostring(), toast.length_long).show();} } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public class myclienttask extends asynctask<void, void, void> { string dstaddress; int dstport; telnetclientexample job; myclienttask(string inetaddress, int port){ dstaddress = inetaddress; dstport = port; } @override protected void doinbackground(void... arg0) { try { job = new telnetclientexample(); job.backgroundjob(); } catch (exception e) { toast.maketext(getapplicationcontext(), e.tostring(), toast.length_short).show(); } return null; } @override protected void onpostexecute(void result) { try { instr = telnetclientexample.tc.getinputstream(); byte[] buff = new byte[1024]; int ret_read = 0; { ret_read = instr.read(buff); if(ret_read > 0) { mainactivity.response += new string(buff, 0, ret_read); } } while (ret_read >= 0); } catch (exception e) { mainactivity.response += e.tostring(); mainactivity.response += "from reading telnet server ! \n"; } textresponse.settext(response); super.onpostexecute(result); } } }
telnetclientexample.java
package com.example.telnetapp; import java.io.ioexception; import java.io.inputstream; import org.apache.commons.net.telnet.telnetclient; public class telnetclientexample { public string remoteip = mainactivity.ip; public int remoteport= mainactivity.port_int_address; public static telnetclient tc = null; public void backgroundjob() throws ioexception { tc = new telnetclient(); try { tc.connect(remoteip, remoteport); } catch (exception e) { } } public void close_con(){ try { tc.disconnect(); } catch (exception e) { mainactivity.response += e.tostring(); mainactivity.response += "from reading disconnecting telnet server ! \n"; } } } problem in code snippet . please , me interactive session. protected void onpostexecute(void result) { try { instr = telnetclientexample.tc.getinputstream(); byte[] buff = new byte[1024]; int ret_read = 0; { ret_read = instr.read(buff); if(ret_read > 0) { mainactivity.response += new string(buff, 0, ret_read); } } while (ret_read >= 0); } catch (exception e) { mainactivity.response += e.tostring(); mainactivity.response += "from reading telnet server ! \n"; } textresponse.settext(response); super.onpostexecute(result); }
Comments
Post a Comment