東坡下載:內(nèi)容最豐富最安全的下載站!

首頁(yè)IT技術(shù) → 在Android下巧妙使用Socket連接網(wǎng)絡(luò)中的電腦

在Android下巧妙使用Socket連接網(wǎng)絡(luò)中的電腦

相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站時(shí)間:2011/4/19 19:29:29字體大。A-A+

更多

作者:點(diǎn)擊:2588次評(píng)論:0次標(biāo)簽:

  既然是使用SOCKET連接網(wǎng)絡(luò)上的電腦,就分為兩部分

  其一,Android上面的Client

  其二,PC上的Server

  Android上面的實(shí)現(xiàn)代碼如下

  view plaincopy to clipboardprint?

  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 \r\n";

   try {

   socket=new Socket("10.0.2.2",54321);

   //向服務(wù)器發(fā)送信息

   PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

   out.println(mesg);

  

   //接受服務(wù)器的信息

   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,"數(shù)據(jù)錯(cuò)誤");

   }

   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());

   }

   }

  }

  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 \r\n";

   try {

   socket=new Socket("10.0.2.2",54321);

   //向服務(wù)器發(fā)送信息

   PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

   out.println(mesg);

  

   //接受服務(wù)器的信息

   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,"數(shù)據(jù)錯(cuò)誤");

   }

   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 創(chuàng)建一個(gè)java的項(xiàng)目

  view plaincopy to clipboardprint?

  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\n");

  

   try {

   ServerSocket serverSocket=new ServerSocket(54321);

   while(true)

   {

   System.out.println("接收用戶連接:");

   //接受客戶端請(qǐng)求

   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);

   //向服務(wù)器發(fā)送消息

   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());

   }

   }

  

  }

 

相關(guān)評(píng)論

閱讀本文后您有什么感想? 已有 人給出評(píng)價(jià)!

  • 2791 喜歡喜歡
  • 2101 頂
  • 800 難過(guò)難過(guò)
  • 1219 囧
  • 4049 圍觀圍觀
  • 5602 無(wú)聊無(wú)聊
熱門評(píng)論
最新評(píng)論
發(fā)表評(píng)論 查看所有評(píng)論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)