我尝试使用套接字将命令从手机发送到计算机。 我在这里尝试了答案: Android and PC Socket connection
但经过一番挖掘后我发现你需要使用Async任务,所以我尝试了这个: Using AsyncTask for android network connection
但由于某种原因,我的套接字超时了。有没有办法找出原因?因为错误我无法告诉:
这是客户端代码:
(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';
}
};
答案 0 :(得分:0)
我用Java为此编写了一个Server,并在模拟器中对其进行了测试。 我有两件事要做:
这是服务器代码
/**
*
* @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);
}
}
}