Android之Socket通信&&Handler 发送接收多条数据

在同一wifi下,自己没试成功,日了狗了~

public class SimpleActivity extends Activity {
    private static final String TAG = SimpleActivity.class.getSimpleName();
    @Bind(R.id.button2)
    Button button2;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.textView2)
    TextView textView2;

Handler han = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        String s = (String) msg.obj;
        textView2.setText(s);
        Log.i(TAG, "run: "+s);

    }
};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.button2)
    public void onClick() {

                connectServer(editText.getText().toString());

    }

    private void connectServer(final String s) {

        Log.i(TAG, "connectServer: "+s);
        new Thread(new Runnable() {
            @Override
            public void run() {

                String serverIp = "192.168.137.1";
                Message m;
                try {
                    m = Message.obtain();
                    Socket socket = new Socket(serverIp, 8877);
                    Log.i(TAG, "connectServer: "+s);
                    DataInputStream din = new DataInputStream(socket.getInputStream());
                    DataOutputStream dot = new DataOutputStream(socket.getOutputStream());
                    dot.writeUTF(s);
                    Log.i(TAG, "run: "+"here");
                    m.obj = din.readUTF();

                    System.out.println(din.readUTF());

                    din.close();
                    dot.close();
                    socket.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
}

服务器

public class MySrver {
    static ServerSocket serverSocket;

    public static void main(String[] args) {
        try{
            serverSocket = new ServerSocket(8877);
            System.out.println("监听8877接口....");

            while(true){
                Socket socket = serverSocket.accept();
                DataInputStream disStream = new DataInputStream(socket.getInputStream());

                DataOutputStream dotStream = new DataOutputStream(socket.getOutputStream());

                System.out.println("客服端信息:"+disStream.readUTF());
                dotStream.writeUTF("成功连接服务器");//写入输入流
                disStream.close();
                dotStream.close();
                socket.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.yang.yin.yinyangapplication.SimpleActivity"
    android:id="@+id/aaa">




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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2"
        android:layout_gravity="center_horizontal" />


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yang.yin.yinyangapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".SimpleActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

    <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.jakewharton:butterknife:7.0.0'
}

2017-06-03

发送数据

  final Message msg = Message.obtain();
                    msg.what = 1;

                    Bundle bundle = new Bundle();
                    bundle.putInt("gpioReal", gpioReal);
                    bundle.putBoolean("lightCurrentFlag", powerManager.isScreenOn());
                    msg.setData(bundle);
//                    msg.obj = gpioReal;

                        mHandler.sendMessage(msg);

接收数据

  if (msg.what == 1) {
                Bundle bundle = msg.getData();
                int gpioReal = bundle.getInt("gpioReal");             
                lightCurrentFlag = bundle.getBoolean("lightCurrentFlag");
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值