【问题标题】:Client and Server in same app to send and then receive Bytes客户端和服务器在同一个应用程序中发送然后接收字节
【发布时间】:2017-07-24 19:22:00
【问题描述】:

我试图用 WiFi 替换蓝牙连接。我最初的项目有一个 PIC 微控制器,它可以记录时间、日期和温度,并控制一些输出。我很久以前写了一个应用程序(3 年,从那以后就没有做过了!),它使用蓝牙将数据从 Android 发送和接收到 PIC,工作正常。 到目前为止,我已经将 ESP8266 连接到 Pic 微控制器,ESP 使用 ESP-LINK 固件并连接到我的家庭网络。图片将从 ESP8266 接收到的内容转发到 RealTerm,以便我可以监控。我已经创建了一个测试应用程序,它正在创建一个套接字并向 PIC 发送一个字节,它工作正常并且接收到的字节正常,代码如下。

package com.example.carl.sockets;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    private Socket socket;
    private static final int SERVERPORT = 23;
    private static final String SERVER_IP = "00.00.00.00"; // enter IP of ESP8266 here

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new Thread(new ClientThread()).start();
    }

    public void onClick(View view) {
        try {
            EditText et = (EditText) findViewById(R.id.EditText01);
            Integer i = Integer.parseInt(et.getText().toString());

            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            if(i > 0 && i < 255){
                out.write(i);
            }else{
                Toast.makeText(this,"Number out of Range !",Toast.LENGTH_SHORT).show();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class ClientThread implements Runnable {
        @Override
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                socket = new Socket(serverAddr, SERVERPORT);
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

一旦字节被发送到 PIC,它将发送一个或多个字节作为响应。我现在需要读取从 PIC 返回的一个或多个字节并将它们显示在 TextView 中。我会提前知道 PIC 会发送多少字节,应用会收到多少字节。

我查看了大量代码,但似乎都是针对单独的服务器和客户端的,我不想要(或者我想要)并且无法弄清楚这一点。

这可能以前被问过,但我花了很长时间寻找,任何帮助或建议将不胜感激。

【问题讨论】:

    标签: java android sockets


    【解决方案1】:

    我现在已经完成了这项工作,可以通过 WiFi 从我的 PIC 向我的 Android 应用程序发送一个字节并接收一个或多个字节。 我创建了一个新线程来等待输入流上的数据。我的主要问题是确保在启动此线程之前连接套接字,一旦我怀疑它工作正常。 当接收到数据时,它会通过处理程序发送回主代码。我的处理程序根据接收到的内容向我的应用程序输出文本,这七个字节是临时读数以及时间和日期。仅显示单个字节。 附上我的代码可能不漂亮!欢迎任何建设性的批评。

    package com.example.carl.sockets;
    
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String TAG = "Main Activity - Sockets";
        private Handler h;
        private Socket socket;
        private static final int SERVERPORT = 23;
        private static final String SERVER_IP = "192.168.1.78"; // enter IP of ESP8266 here
        private receiveData rd;
        protected static final int SUCCESS_CONNECT = 0;
        protected static final int NOW_DATA_RECEIVED = 1;
        protected static final int ONE_BYTE_RECEIVED = 2;
        private boolean connectedSocket = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            new Thread(new ClientThread()).start();
    
            while(!connectedSocket){}
    
            rd = new receiveData(socket);
            rd.start();
    
        h = new Handler() {
                public void handleMessage(android.os.Message msg) {
                    switch (msg.what) {
                        case SUCCESS_CONNECT:
                            break;
                        case NOW_DATA_RECEIVED:
                            Log.d(TAG,"Handler - In 7 bytes received");
                            byte[] nowData = (byte[]) msg.obj;
                            // process data
                            int tempFrac = 0;
                            int tempUnits = 0;
                            // construct time and date string, pad to 2 figures, unsigned values
                            String hourStr = Integer.toString(nowData[2]&0xFF);
                            hourStr = "00".substring(hourStr.length()) + hourStr;
                            String minStr = Integer.toString(nowData[3]&0xFF);
                            minStr = "00".substring(minStr.length()) + minStr;
                            String dayStr = Integer.toString(nowData[4]&0xFF);
                            dayStr = "00".substring(dayStr.length()) + dayStr;
                            String monthStr = Integer.toString(nowData[5]&0xFF);
                            monthStr = "00".substring(monthStr.length()) + monthStr;
                            String yearStr = Integer.toString(nowData[6]&0xFF);
                            yearStr = "00".substring(yearStr.length()) + yearStr;
                            String nowDataString = new String(hourStr +":" + minStr + " " + dayStr + "/" + monthStr + "/" + yearStr);
                            // get unsigned values
                            int tempMSB = nowData[0] & 0xFF;
                            int tempLSB = nowData[1] & 0xFF;
                            // add LSB and MSB
                            int tempVar = tempLSB + (tempMSB<<8);
                            // if Zero
                            if(tempVar == 0){
                                // result zero so display zero degrees
                                // put in text view
                                TextView txtData = (TextView) findViewById(R.id.textView);
                                txtData.setText(nowDataString + " Temp = 0.0c");
                            }else if(tempMSB >= 0x80){
                                // result negative
                                tempVar = (~tempVar) + 1;
                                tempFrac = tempVar & 0x0F;
                                tempFrac = tempFrac*625/1000;
                                tempUnits = (tempVar>>4)&0x7F;
                                String tempString = new String("-"+Integer.toString(tempUnits)+"."+Integer.toString(tempFrac));
                                // put in text view
                                TextView txtData = (TextView) findViewById(R.id.textView);
                                txtData.setText(nowDataString + " Temp = " + tempString + "c");
                            }else{
                                // result positive
                                tempFrac = tempVar & 0x0F;
                                tempFrac = tempFrac*625/1000;
                                tempVar = tempVar>>4;
                                tempUnits = tempVar & 0x7F;
                                String tempString = new String(Integer.toString(tempUnits)+"."+Integer.toString(tempFrac));
                                // put in text view
                                TextView txtData = (TextView) findViewById(R.id.textView);
                                txtData.setText(nowDataString + " Temp = " + tempString + "c");
                            }
                            break;
                        case ONE_BYTE_RECEIVED:
                            Log.d(TAG,"Handler - In 1 byte received");
                            byte[] recByte = (byte[]) msg.obj;
                            int receivedByte = (int)recByte[0];
                            TextView txtData = (TextView) findViewById(R.id.textView);
                            txtData.setText( "One Byte Received = " + Integer.toString(receivedByte));
                            Log.d(TAG, "In handler, one byte received = "+ Integer.toString(receivedByte));
                            break;
                    }
                }
            };
        }
    
        @Override
        public void onPause(){
            super.onPause();
    
            try{
                socket.close();
            }catch(IOException e){
                Log.d(TAG,"Error closing socket in on pause");
            }
            rd.stopRdThread();
        }
    
        public void onClick(View view) {
            try {
                EditText et = (EditText) findViewById(R.id.EditText01);
                Integer i = Integer.parseInt(et.getText().toString());
    
                DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                if (i > 0 && i < 255) {
                    out.write(i);
                    Log.d(TAG,"Byte Sending - "+ i.toString());
                } else {
                    Toast.makeText(this, "Number out of Range !", Toast.LENGTH_SHORT).show();
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        class ClientThread implements Runnable {
            @Override
            public void run() {
    
                try {
                    InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                    socket = new Socket(serverAddr, SERVERPORT);
                    if(socket.isConnected()){
                        Log.d(TAG, "in ClientThread - socket connected");
                        connectedSocket = true;
                    }else{
                        Log.d(TAG, "in ClientThread - socket not connected");
                        connectedSocket = false;
                    }
                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    
        class receiveData extends Thread {
            private Socket socketLocal;
            private volatile boolean exit = false;
            DataInputStream in;
            byte[] fullBuffer = new byte[7];
            byte[] buffer = new byte[100];
            int bytes; // bytes returned from read()
            int bytesCount = 0;
    
            // constructor
            receiveData(Socket socket){
                this.socketLocal = socket;
            }
    
            public void run(){
                try {
                    if (socketLocal.isConnected()) {
                        Log.d(TAG, "In receiveData, Socket connected");
                        in = new DataInputStream(socketLocal.getInputStream());
                    }
                }catch(Exception e){
                        Log.d(TAG, "in receiveData - run exception - " + e.toString());
                }
    
                while(!exit){
                    try {
                        if (socketLocal.isConnected()) {
                            if (in.available() > 0) {
                                //input = in.readByte();     // Get number of bytes and message in "buffer"
                                bytes = in.read(buffer);
                                Log.d(TAG,"Bytes Read = "+Integer.toString(bytes));
                                System.arraycopy(buffer,0,fullBuffer,bytesCount,bytes);
                                bytesCount = bytesCount + bytes;
                                if(bytesCount >= 7){
                                    h.obtainMessage(NOW_DATA_RECEIVED, bytesCount, -1, fullBuffer).sendToTarget();     // Send to message queue Handler
                                    Log.d(TAG,"In handler, 7 bytes Message sent");
                                    bytesCount = 0;
                                }else if(bytesCount == 1){
                                    h.obtainMessage(ONE_BYTE_RECEIVED, bytesCount, -1, fullBuffer).sendToTarget();
                                    Log.d(TAG, "In handler, 1 byte message sent");
                                    bytesCount = 0;
                                }
                            }
                        }else{
                            break;
                        }
                    }catch(Exception e){
                        Log.d(TAG, "Read Error - " + e.toString());
                    }
                }
            }
    
            public void stopRdThread(){
                exit = true;
                try {
                    socketLocal.close();
                }catch(Exception e){
                    Log.d(TAG, "error closing socket - " + e.toString());
                }
            }
        }
    }
    

    我现在唯一的问题是内存泄漏,我想? 我收到一条消息- 07-30 17:46:01.154 15417-15445/com.example.carl.sockets D/dalvikvm: GC_FOR_ALLOC 释放 512K,22% 释放 9692K/12332K,暂停 18ms,共 18ms

    应用启动后,显示器显示我正在使用 9.4 MB。

    【讨论】:

      【解决方案2】:

      我对泄漏进行了排序,它是 in.available(),我用来测试是否出于某种原因存在数据。真的不明白!去掉了 receiveData 中的两个 if() 语句,现在效果很好。

      【讨论】:

        猜你喜欢
        • 2017-11-04
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多