安卓socket通信小例子,运用缓存区,发送有限个字节

服务器端

 

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

public class MyPlayerServerActivity extends Activity {
    /** Called when the activity is first created. */
 
 private ServerSocket ss;
 private boolean isRuning=true;
 Socket s;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new Thread()
        {
         public void run()
         {
          try {
        ss=new ServerSocket(8888);
        while(isRuning)
        {
         s=ss.accept();
         while(true)
         {
          DataInputStream dis=new DataInputStream(s.getInputStream());
             short b=dis.readShort();
             Log.v("a", "aaa");
             Log.v("b", b+"");
             Log.v("c", "ccc");
         }
         
        }
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
         }
        }.start();
    }
    @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
  if (keyCode == KeyEvent.KEYCODE_BACK)
  {
   try {
    
    isRuning=false;
    ss.close();
    finish();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   Log.v("socket已关闭...", "socket已关闭...");
  }
  return false;
 }
}

 

 

客户端

 

 

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyPlayerClientActivity extends Activity {
    /** Called when the activity is first created. */
 
 private Button btn1;
 private Button btn2;
 private Button btn3;
 private Button btn4;
 
 OutputStream os=null;
 DataOutputStream dos=null;
 Socket socket=null;
 
 
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        btn3=(Button)findViewById(R.id.button3);
        btn4=(Button)findViewById(R.id.button4);
        try {
   socket=new Socket("192.168.1.101", 8888);
   os=socket.getOutputStream();
  } catch (UnknownHostException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }
        btn1.setOnClickListener(
          new Button.OnClickListener()
          {
           public void onClick(View v)
           {
            try {
             byte[] bytes = new byte[2];
             ByteBuffer buffer = ByteBuffer.wrap(bytes);
             buffer.putShort((short)1);
             os.write(bytes);
             bytes=null;
             Toast.makeText(MyPlayerClientActivity.this, "已发送111...", Toast.LENGTH_SHORT).show();
//             socket.close();
            } catch (Exception e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
           }
          }
          );
        btn2.setOnClickListener(
          new Button.OnClickListener()
          {
           public void onClick(View v)
           {
            try {
             byte[] bytes = new byte[2];
             ByteBuffer buffer = ByteBuffer.wrap(bytes);
             buffer.putShort((short)2);
             os.write(bytes);
             Toast.makeText(MyPlayerClientActivity.this, "已发送222...", Toast.LENGTH_SHORT).show();
//             socket.close();
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
           }
          }
          );
        btn3.setOnClickListener(
          new Button.OnClickListener()
          {
           public void onClick(View v)
           {
            try {
             byte[] bytes = new byte[2];
             ByteBuffer buffer = ByteBuffer.wrap(bytes);
             buffer.putShort((short)3);
             os.write(bytes);
             Toast.makeText(MyPlayerClientActivity.this, "已发送333...", Toast.LENGTH_SHORT).show();
//             socket.close();
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
           }
          }
          );
        btn4.setOnClickListener(
          new Button.OnClickListener()
          {
           public void onClick(View v)
           {
            try {
             byte[] bytes = new byte[2];
             ByteBuffer buffer = ByteBuffer.wrap(bytes);
             buffer.putShort((short)444);
             os.write(bytes);
             Toast.makeText(MyPlayerClientActivity.this, "已发送444...", Toast.LENGTH_SHORT).show();
//             socket.close();
            } catch (Exception e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
           }
          }
          );
       
    }
}

 

服务端 main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="服务器已启动111" />

</LinearLayout>

 

客户端main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值