Android下使用Socket連接網絡電腦 – Android移動開發技術文章_手機開發 Android移動開發教學課程

冷風


既然是使用SOCKET連接網絡上的電腦,就分為兩部分


其一,Android上面的Client


其二,PC上的Server


Android上面的實現代碼如下


package Hello.MySocket;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MySocket extends Activity {
 private final String DEBUG_TAG=”lengfeng”;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Log.v(“lengfeng”,”Android Start Connect”);
       
    Socket socket=null;
    String mesg=”my name is log
“;
    try {
     socket=new Socket(“10.0.2.2”,54321);
     //向服務器發送信息
     PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
     out.println(mesg);
     
     //接受服務器的信息
     BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
     String mstr=br.readLine();
     if(mstr!=null)
     {
      Log.e(DEBUG_TAG,mesg);
     }else
     {
      Log.e(DEBUG_TAG,”數據錯誤”);
     }
     out.close();
     br.close();
     socket.close();
    } catch (UnknownHostException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }catch(Exception e)
    {
     Log.e(DEBUG_TAG,e.toString());
    }
    }
}


PC上面的Server 創建一個java的項目


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
 
public class MyJavaServer {
 /**
  * @param args
  */
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.print(“MyJavaServer Start Runing
“);
  
  try {
   ServerSocket serverSocket=new ServerSocket(54321);
   while(true)
   {
    System.out.println(“接收用戶連接:”);
    //接受客戶端請求
    Socket client=serverSocket.accept();
    System.out.println(“accept:”);
    try
    {
     //接受客戶端信息
     BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
     String str=in.readLine();
     System.out.println(“read:”+str);
     //向服務器發送消息
     PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())),true);
     out.println(“Server Message:”+str);
     in.close();
     out.close();
    }catch(Exception ex)
    {
     System.out.println(ex.getMessage());
     ex.printStackTrace();
    }
    finally
    {
     client.close();
     System.out.println(“close”);
    }
   }
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
 }
 
}


 

You May Also Like