问题描述
packagecom.example.test3;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.Socket;importandroid.support.v7.app.ActionBarActivity;importandroid.support.v4.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.Menu;importandroid.view.MenuItem;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;publicclassMainActivityextendsActionBarActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if(savedInstanceState==null){getSupportFragmentManager().beginTransaction().add(R.id.container,newPlaceholderFragment()).commit();}}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.main,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){//Handleactionbaritemclickshere.Theactionbarwill//automaticallyhandleclicksontheHome/Upbutton,solong//asyouspecifyaparentactivityinAndroidManifest.xml.Buttonbt2=(Button)findViewById(R.id.button2);bt2.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){t.start();}});intid=item.getItemId();if(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}/***Aplaceholderfragmentcontainingasimpleview.*/Threadt=newThread(newRunnable(){publicvoidrun(){Socketsocket;Stringstr1="";Stringstr2="";EditTexteditText2=(EditText)findViewById(R.id.et2);EditTexteditText1=(EditText)findViewById(R.id.et1);str1=editText1.getText().toString();str2=editText2.getText().toString();intport=Integer.parseInt(str2);try{socket=newSocket(str1,port);InputStreamin=socket.getInputStream();OutputStreamout=socket.getOutputStream();DataInputStreamdin=newDataInputStream(in);DataOutputStreamdout=newDataOutputStream(out);EditTexteditText3=(EditText)findViewById(R.id.et3);Stringstr3=editText3.getText().toString();byte[]b=hexStringToBytes(str3);dout.write(b);byte[]bufferread=null;din.read(bufferread);EditTexteditText4=(EditText)findViewById(R.id.et4);editText4.setText(printHexString(bufferread));din.close();dout.close();in.close();out.close();}catch(IOExceptione){System.out.println("error"+e);}catch(Exceptione){System.out.println("error"+e);}}});publicclassbtnclickListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewarg0){t.start();}}publicstaticStringprintHexString(byte[]b){Stringhex=null;for(inti=0;i<b.length;i++){hex=Integer.toHexString(b[i]&0xFF);if(hex.length()==1){hex='0'+hex;}}returnhex;}publicstaticbyte[]hexStringToBytes(StringhexString){hexString=hexString.replace("","");if(hexString==null||hexString.equals("")){returnnull;}hexString=hexString.toUpperCase();intlength=hexString.length()/2;char[]hexChars=hexString.toCharArray();byte[]d=newbyte[length];for(inti=0;i<length;i++){intpos=i*2;d[i]=(byte)(charToByte(hexChars[pos])<<4|charToByte(hexChars[pos+1]));}returnd;}/***Convertchartobyte*@paramcchar*@returnbyte*/publicstaticbytecharToByte(charc){return(byte)"0123456789ABCDEF".indexOf(c);}publicstaticclassPlaceholderFragmentextendsFragment{publicPlaceholderFragment(){}@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ViewrootView=inflater.inflate(R.layout.fragment_main,container,false);returnrootView;}}}