這個程式寫起來有點亂,用了很多方法去測試client,了解相關的code

---------------------------------------------------------------------

package com.example.client02;


//import java.io.ByteArrayOutputStream;
//import java.lang.*;
import java.io.IOException;
//import java.io.InputStream;
import java.io.BufferedOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.InetSocketAddress;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
import java.io.BufferedInputStream;
import java.io.BufferedReader;

public class MainActivity extends Activity {
 
 TextView textResponse;

 Button buttonConnect, buttonClear;
 //Socket socket=null;
 //InetSocketAddress isa = new InetSocketAddress("192.168.50.130",8765);
// BufferedOutputStream out=null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  buttonConnect = (Button)findViewById(R.id.connect);
  buttonClear = (Button)findViewById(R.id.clear);
  textResponse = (TextView)findViewById(R.id.response);
 
  buttonConnect.setOnClickListener(buttonConnectOnClickListener);

  buttonClear.setOnClickListener(new OnClickListener(){
    
   @Override
   public void onClick(View v) {
    textResponse.setText("");
   }});
 }
 
 OnClickListener buttonConnectOnClickListener =
   new OnClickListener(){

    @Override
    public void onClick(View arg0) {

        
        
        Timer timer = new Timer();
 
        timer.schedule(new ClientTask(),  1000, 3000);

    }};

    public class SocketClient extends java.lang.Thread {
        BufferedReader serverin;
        String obj;
        Message servermsg = new Message();
     
        @SuppressWarnings("unused")
        private SocketClient() {
            // 不使用的建構子
        }
*/
    //可以用clienttask or task(response);
 
    class ClientTask extends TimerTask {

        public void run(){

            String response="";
           Task( response);

        }
      

    }
private void Task(String response)
{
      
      

     InetSocketAddress isa = new InetSocketAddress("192.168.50.130",8765);
       Socket socket=null;
    
        socket = new Socket();
         try {
       
            
            socket.connect(isa, 1000);//1 sec        
            BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
          out=new BufferedOutputStream(socket.getOutputStream());
         // 送出字串
           String message="hello android";
    
         out.write(message.getBytes());
         out.flush();
         byte[] buffer = new byte[1024];
         ByteArrayOutputStream byteArrayOutputStream =  new ByteArrayOutputStream(1024);
       
         InputStream inputStream = socket.getInputStream();  


            int length=inputStream.read(buffer);

             byteArrayOutputStream.write(buffer, 0, length);

            response += byteArrayOutputStream.toString("UTF-8");

          byteArrayOutputStream.flush();
          byteArrayOutputStream.close();
          inputStream.close();
          out.close();

         socket.close();
        }
        
        catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "UnknownHostException: " + e.toString();
           } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            response = "IOException: " + e.toString();
           }
        
    
        
}
 

}

文章標籤
創作者介紹

colin異世界

colin 發表在 痞客邦 PIXNET 留言(0) 人氣()