东坡下载:内容最丰富最安全的下载站!

首页IT技术 → 在Android下巧妙使用Socket连接网络中的电脑

在Android下巧妙使用Socket连接网络中的电脑

相关文章发表评论 来源:本站时间:2011/4/19 19:29:29字体大小:A-A+

更多

作者:点击:2588次评论:0次标签:

  既然是使用SOCKET连接网络上的电脑,就分为两部分

  其一,Android上面的Client

  其二,PC上的Server

  Android上面的实现代码如下

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

   //向服务器发送信息

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

   }

   }

  }

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

   //向服务器发送信息

   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的项目

  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("接收用户连接:");

   //接受客户端请求

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

   }

   }

  

  }

 

相关评论

阅读本文后您有什么感想? 已有 人给出评价!

  • 2791 喜欢喜欢
  • 2101 顶
  • 800 难过难过
  • 1219 囧
  • 4049 围观围观
  • 5602 无聊无聊
热门评论
最新评论
昵称:
表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
字数: 0/500 (您的评论需要经过审核才能显示)

本类常用软件