Android客户端未连接到python服务器

时间:2019-04-11 19:13:55

标签: java android python server client

我正在用android和raspberry pi开发一个IoT项目。我需要通过android客户端与树莓派上运行的python服务器进行通信。我已经完成了双方的代码,但是客户端和服务器之间的连接没有发生。以下是两者的代码。

Python服务器:

from socket import *
from time import ctime
#import RPi.GPIO as GPIO

ctrCmd = ['Up', 'Down']

host = ''
port = 5540
Buffsize = 1024
addr =(host,port)

tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(addr)
tcpSerSock.listen(5)

while True:
    print ('waiting for connection')
    tcpCliSock,addr = tcpSerSock.accept()
    print ('Connected from :', addr)

    while True:
        data =''
        data = tcpCliSock.recv(Buffsize)
        if not data:
            break
        if data == ctrCmd[0]:
            lock
        if data == ctrCmd[1]:
            unlock



Android客户端:

package com.example.akhtar.check;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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

public class MainActivity extends AppCompatActivity {
    Button on;
    Button off;
    EditText address;
    Socket myAppSocket = null;
    public static String ipadd = "";
    public static int port = 5540;
    public static String CMD = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        on = findViewById(R.id.butOn);
        off = findViewById(R.id.butoff);
        address = findViewById(R.id.editText);
        ipadd = address.getText().toString();
        Log.i("ip","raly");

        on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CMD = "on";
                Log.i("myApp", "no network");
                Socket_AsyncTask CMD_lock = new Socket_AsyncTask();
                Log.i("olamba", "kuni");
                CMD_lock.execute();
                Log.i("osha", "kuni");
            }
        });
        off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CMD = "off";
                Socket_AsyncTask CMD_lock = new Socket_AsyncTask();
                CMD_lock.execute();
            }
        });


    }
    public class Socket_AsyncTask extends AsyncTask<Void,Void,Void>
    {


        @Override
        protected Void doInBackground(Void... params) {
            try{
                InetAddress inetaddress = InetAddress.getByName(ipadd);
                Log.i("raly", "no network");
                Socket socket = new Socket(inetaddress, port);
                Log.i("supra", "no network");

                DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataOutputStream.writeByte(Integer.parseInt(CMD));
                dataOutputStream.close();
                socket.close();

            }catch(UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
            return null;
        }
    }

}

运行服务器和客户端后,必须进行连接,并且应在python服务器上显示“从地址”连接。

0 个答案:

没有答案