通过不在同一网络上的插座连接两个设备?

时间:2017-02-26 12:43:28

标签: android python sockets network-programming

在处理涉及python套接字服务器和Android客户端的项目时,我无法弄清楚如何连接我的手机和我的计算机的套接字而不让他们使用相同的网络。这对我的项目至关重要。它真的可能吗?如果是这样,它是如何完成的? 我得到的错误是连接中的ETIMEOUT,这意味着根本没有建立连接。 在下面的示例中,我最不需要看到的是服务器程序注意到android套接字已尝试连接到它。

基本Android客户端仅用于示例:

    public class sybel extends AppCompatActivity {
    private Socket socket;
    private String serverIpAddress = "192.168.0.106";
    private static final int REDIRECTED_SERVERPORT = 7777;
    public PrintWriter out;
    public BufferedReader in ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sybel);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
        new NetBeacons().execute();
}
public class NetBeacons extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(final String... params) {
        try {
            try {
                socket = new Socket();
                socket.connect(new InetSocketAddress(serverIpAddress, REDIRECTED_SERVERPORT), 0);
                Log.d("XXX",socket.isConnected()+"");
                PrintWriter outToServer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
                DataInputStream dis = new DataInputStream(socket.getInputStream());

                //here it sends the data
                outToServer.print("hi");
                outToServer.flush();
                Log.i("sybel",dis.readUTF());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    protected void onPostExecute(String result) {
        Log.d("XXX",result);

    }


    protected void onPreExecute() {


    }

    protected void onProgressUpdate(Void... values) {

    }
}

基本Python服务器:

    import socket
    import select
    server_socket=socket.socket()
    server_socket.bind(('0.0.0.0', 7777))
    server_socket.listen(5)
    open_client_sockets = []
    while True:
        rlist,wlist,xlist = select.select([server_socket]+open_client_sockets,[],[])
        for current_socket in rlist:
            if current_socket is server_socket:
                (new_socket, address) = server_socket.accept()
                open_client_sockets.append(new_socket)
            else:
                data = current_socket.recv(1024)

                if data=="":
                    open_client_sockets.remove(current_socket)
                    print "Connection with client closed"
                else:
                    print data        

非常感谢你的帮助!

0 个答案:

没有答案