通过套接字从手机向计算机发送命令

时间:2018-01-16 22:53:23

标签: java android sockets pc

我尝试使用套接字将命令从手机发送到计算机。 我在这里尝试了答案: Android and PC Socket connection

但经过一番挖掘后我发现你需要使用Async任务,所以我尝试了这个: Using AsyncTask for android network connection

但由于某种原因,我的套接字超时了。有没有办法找出原因?因为错误我无法告诉:

Logcat的错误:enter image description here

这是客户端代码:

(function() {
  'use strict';
  angular.module('agencyApp', [])
    .controller('HoursController', HoursController)
    .directive('timePicker', timePicker);

  function HoursController() {
var vm = this;

vm.print = function() {
  console.log(vm);
};
vm.setClosed = setClosed;
vm.setAllWeekdayClosed = setAllWeekdayClosed;
vm.weekdays = [{
  Day: 'Monday',
  fromTime: "Select",
  toTime: "Select"
}, {
  Day: 'Tuesday',
  fromTime: "Select",
  toTime: "Select"
}, {
  Day: 'Wednesday',
  fromTime: "Select",
  toTime: "Select"
}, {
  Day: 'Thursday',
  fromTime: "Select",
  toTime: "Select"
}, {
  Day: 'Friday',
  fromTime: "Select",
  toTime: "Select"
}, {
  Day: 'Saturday',
  fromTime: "closed",
  toTime: "closed"
}, {
  Day: 'Sunday',
  fromTime: "closed",
  toTime: "closed"
}];

vm.closed = [
  false, false, false, false, false, true, true
];

function setClosed(index) {
  if (vm.closed[index]) {
    vm.weekdays[index].toTime = vm.weekdays[index].fromTime = 'closed';
  }
  if (!vm.closed[index]) {
    vm.weekdays[index].fromTime = 'Select';
    vm.weekdays[index].toTime = 'Select';
  }
};

function setAllWeekdayClosed(index) {
  if (vm.closed[index]) {
    vm.weekdays[index].toTime = vm.weekdays[index].fromTime = 'closed';
  }
  if (!vm.closed[index]) {
    vm.weekdays[index].fromTime = '7:00 am';
    vm.weekdays[index].toTime = '3:00 pm';
  }
};

1 个答案:

答案 0 :(得分:0)

我用Java为此编写了一个Server,并在模拟器中对其进行了测试。 我有两件事要做:

  1. 如果您使用的是Android模拟器,则服务器IP为“10.0.2.2”。 LocalHost是de Emulator虚拟机。
  2. 您的应用程序需要清单中的权限Internet
  3. 这是服务器代码

    /**
     *
     * @author Jean-Pierre
     */
    public class SocketServer {
    
        private static final int portnumber = 4444;
        public static void main(String[] args) {
            SocketServer socketServer = new SocketServer();
            socketServer.run();
        }    
    
        /**
         * Reads a String from the client
         * Converts it to Uppercase
         * and sends it Back.
         */
        private void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(portnumber);
                Socket clientSocket = serverSocket.accept();
                System.out.println("connected with :" + clientSocket.getInetAddress());
                PrintWriter out = 
                        new PrintWriter(clientSocket.getOutputStream(), true);
                InputStreamReader is = 
                        new InputStreamReader(clientSocket.getInputStream());
                BufferedReader in = 
                        new BufferedReader(is);
                while (true) {
                    String line = in.readLine();
                    if (line != null) {
                        System.out.println("recieved:" + line);
                        out.println(line.toUpperCase()); 
                    }    
                }
            } catch (IOException ex) {
                Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }