Android的插座没有连接到笔记本电脑服务器 - 但当地的Java程序可以 [英] Android Socket doesn't connect to laptop server - but local Java program can

查看:115
本文介绍了Android的插座没有连接到笔记本电脑服务器 - 但当地的Java程序可以的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络是一种痛苦。在使用服务AsyncTasks我试图启动socket通讯:

Networking being a pain. In a service using AsyncTasks I attempt to start socket communication:

public class TransmitService extends Service {

    private Socket echoSocket = null;
    private static PrintWriter out = null;
    private String HOST = null;
    private int PORT = -1;
    private static Context context;
    public static boolean isConnected = false;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO do something useful
        context = getApplicationContext();
        HOST = intent.getExtras().getString("HOST");
        PORT = intent.getExtras().getInt("PORT");
        Toast.makeText(context, "HOST/PORT: "+HOST+"/"+PORT, Toast.LENGTH_LONG).show();
        new initNetworkTask().execute();
        return Service.START_NOT_STICKY;
    }

    // Send the orientation data
    public static void sendData(float f1, float f2, float f3) {
        new sendDataTask().execute(f1, f2, f3);
    }

    static class sendDataTask extends AsyncTask<Float, Void, Void> {

        @Override
        protected Void doInBackground(Float... params) {
            try {
                JSONObject j = new JSONObject();
                j.put("yaw", params[0]);
                j.put("pitch", params[1]);
                j.put("roll", params[2]);
                String jString = j.toString();
                out.println(jString);
            } catch (Exception e) {
                Log.e("sendDataTask", e.toString());
            }
            return null;
        }

    }

    class initNetworkTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                echoSocket = new Socket(HOST, PORT);
                out = new PrintWriter(echoSocket.getOutputStream(), true);
                out.println("Welcome.");
                isConnected = true;
            } catch (Exception e) {
                Log.e("initNetworkTask", e.toString());
                isConnected = false;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO for communication return IBinder implementation
        return null;
    }
}

我的服务器只是在我的笔记本电脑上运行一个python脚本:

My "server" is just a python script running on my laptop:

import socket

HOST = '192.168.###.#'     #(numbers omitted from S/O question)             
PORT = 10000             
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if data is not None:
        print data
conn.close()

下面的服务器工作得很好与同一笔记本电脑上运行此Java客户端:

The following server works just fine with this Java client running on the same laptop:

public class DataSender {
    public static void main(String[] args) throws IOException {

        Socket echoSocket = null;
        PrintWriter out = null;
        //BufferedReader in = null;

        try {
            echoSocket = new Socket("192.168.###.#", 10000);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            //in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for " + "the connection to: taranis.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String userInput;

        while ((userInput = stdIn.readLine()) != null) {
            out.println(userInput); //out.println - THIS IS HOW DATA IS SENT
            //System.out.println("echo: " + in.readLine());
        }

        out.close();
        //in.close();
        stdIn.close();
        echoSocket.close();
    }
}

我真的很想得到这个在我的Andr​​oid应用程序的工作 - 我的目的是通过 TransmitService.sendData(F1,F2,F3)连续流方向的数据我笔记本电脑。

I really want to get this to work in my Android app - my purpose is to continuously stream orientation data via TransmitService.sendData(f1,f2,f3) to my laptop.

在测试方面:我已经关闭了Windows防火墙,我也有同样的WiFi连接(星巴克)的测试,并尝试了一些其他的端口(80,4444,4445,5000)。

In terms of testing: I have turned off windows firewall, I have tested on the same WiFi connection (Starbucks) and tried a few other ports (80, 4444, 4445, 5000).

注释错误我在Android应用程序得到的是:

The comment error I get in my Android app is:

java.net.ConnectException:无法连接到/192.168.###.#(端口10000):连接失败:ETIMEDOUT(连接超时)

感谢您抽空看看,会很乐意提供更多信息/运行更多的测试来得到这个问题的解决。我也有兴趣在考虑通过互联网从手机发送方向的数据,以笔记本电脑的其他解决方案。

Thanks for taking a look, would be happy to provide more information / run more tests to get this issue resolved. I'm also interested in considering other solutions for sending the orientation data from phone to laptop over internet.

推荐答案

哦,孩子,这是愚蠢和尴尬。基本上我是使用了错误的IP地址(192.168 ....)。

Oh boy this is silly and embarrassing. Basically I was using the wrong IP address (192.168....).

在Windows中,使用IPCONFIG(Linux的命令:ifconfig),然后选择相应的无线局域网(WLAN)的IP地址。我用对应于虚拟机或一些掷骰子另一个IP地址。

In Windows, use ipconfig (Linux: ifconfig), and choose the IP address corresponding to Wireless LAN (wlan). I was using another IP address corresponding to a virtual machine or some crap.

希望这可以帮助别人,将来别人,我讨厌这些类型的问题。

Hope this helps someone else in the future, I hate these kind of issues.

这篇关于Android的插座没有连接到笔记本电脑服务器 - 但当地的Java程序可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆