机器人 - 服务器插槽 [英] Android - Server Socket

查看:164
本文介绍了机器人 - 服务器插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于Android中的Socket通信几个问题。

I have few question regarding Socket Communication in Android.

1)我已经开发了服务器套接字的应用程序。

1)I have developed server socket app for android that listens to port 8888.

a)当我主持我的服务器上的模拟器我无法通过客户端应用程序,我有我的电脑上,因为既要传达给它(仿真器及放大器;客户端)的应用程序都在我的笔记本电脑和放大器;在同一网络上我认为他们应该能够相互通信。

a) When i host my server on the emulator I'm unable to communicate to it through Client application that I have on my PC since both (Emulator & Client) app are on my laptop & on the same network i think that they should be able to communicate with each other.

二)当我部署在同一台服务器的应用我的Andr​​oid移动设备上,并尝试通过同一个客户端应用程序,我有我的电脑上沟通呢,客户端应用程序提供了一个超时例外,它无法沟通吧。

b) When i deploy the same server app on my android mobile device and try to communicate it through the same Client Application that I have on my PC, the client application gives a timeout exception as its unable to communicate to it.

我的第一个问题是如何测试与仿真器和放大器的服务器/客户端套接字的应用程序; 1 Android设备?我甚至可以用我的电脑的客户端套接字应用程序来测试我的服务器套接字?

My first question is How can i test server/client socket app with Emulator & 1 android device? Can i even use my PC's client socket application to test my server socket?

**我有客户端的Socket申请我的其他应用程序,使世界上没有其他问题与客户端应用程序。

**I have Client Socket Application for my other application so theres no problem with the client application.

2)我的第二个问题是测试Android设备我要转发所需的端口上我的服务器应用程序吗?    a)对于模拟器:我怎样才能转发端口?    b)对于设备:我怎样才能转发端口?    三)我可以把端口编程?

2) My second question is to test my server app on the android device do I have to forward the desired port? a) For Emulator: How can I forward port? b) For Device: How can i forward port? c) Can i forward port programmatically?

**只为信息:

我使用Eclipse作为机器人的研究与开发的工具。

I'm using Eclipse as android developement tool.

**我的服务器code,因为也可能是问题,我的服务器套接字code了。

** MY Server Code as there can also be problem with my server socket code too.

Socket socket = null;
            DataInputStream dataInputStream = null;
            DataOutputStream dataOutputStream = null;
            ServerSocket serverSocket = null;
        try
        {  
            serverSocket = new ServerSocket(SERVERPORT);
            System.out.println("Listening :" + SERVERPORT);
            System.out.println("Server IP:" + SERVERIP);
        }
        catch (Exception e) 
        {               
            e.printStackTrace();
        }
        while(true)
        {
            try
            {
                socket = serverSocket.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println("ip: " + socket.getInetAddress());                   
                String str = in.readLine();
                System.out.println("message Received: " + str);
            } 
            catch (Exception e) 
            {                   
                e.printStackTrace();
            }               
            finally
            {
                if( socket!= null)
                {
                    try 
                    {
                        socket.close();
                    }
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
                if( dataInputStream!= null)
                {
                    try
                    {
                        dataInputStream.close();
                    }
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }                   
                if( dataOutputStream!= null)
                {
                    try 
                    {
                        dataOutputStream.close();
                    }
                    catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                }
            }
        }

编辑: 一个非常有趣的是,如果我设置端口8080的PC客户端的Socket应用都连接到Android应用我的Andr​​oid设备上,但我没有收到套接字我的服务器和我发送的数据。链接=> =插座的ServerSocket.accept()后没有任何反应;

Edited: A very interesting part is that if i set port to 8080 PC's Client Socket App do connect to Android App on my android device but i don't receive socket on my server nor the data I send. Nothing happens after the link => socket = serversocket.accept();

此外,我已经设置权限的清单。

Also I have set the permission in the manifest.


推荐答案

对于第一个问题,如何从仿真器访问网络:模拟器运行在它自己的网络地址空间,分离出你的电脑。你必须在网络上配置网络重定向到接入设备。

For your first question, on how to access the network from the emulator: the emulator runs on its own network address space, isolated from your PC. You have to configure network redirection to access devices on your network.

见的http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking

这篇关于机器人 - 服务器插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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