如何使用套接字将Android移动设备上的移动应用程序连接到笔记本电脑服务器?

时间:2011-12-19 14:00:11

标签: java android client wireless

我有一台在我的笔记本电脑上运行的非常简单的Java服务器。另一方面,我有我的平板电脑,也有一个非常简单的客户端,以配合我的服务器,但它无法正常工作。 两者都在同一个无线网络上运行。

服务器示例         ServerSocket serverSocket = null;         套接字socket = null;         DataInputStream dataInputStream = null;         DataOutputStream dataOutputStream = null;

    try {
        serverSocket = new ServerSocket(8888);
        System.out.println("Listening :8888");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    while(true){
        try {
            socket = serverSocket.accept();
            dataInputStream = new DataInputStream(socket.getInputStream());
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            System.out.println("ip: " + socket.getInetAddress());
            System.out.println("message: " + dataInputStream.readUTF());
            dataOutputStream.writeUTF("Hello!");

简单的客户端代码:

mysock = new Socket(server_adress, port_number);

我尝试过设置清单WIFI权限。试图关闭计算机防火墙。 关于无线连接如何工作可能会阻塞连接,我应该知道些什么吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

您需要为INTERNET设置权限。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

客户方:

String adresaServer = "192.168.4.120";

        int PORT = 8095;
        Socket socket = null;

        DataInputStream in = null;

        try {
            socket = new Socket();
            SocketAddress adr = new InetSocketAddress(adresaServer, PORT);
            socket.connect(adr, 1500);

            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
            out.flush();
        } catch (SocketTimeoutException e) {
            System.err.println(" Error at CONNECTINGG: \n" + e);

        } catch (UnknownHostException e) {
            System.err.println(" Serverul nu poate fi gasit \n" + e);
            System.exit(1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        out.writeFloat(sensors.getValueGyroZ());
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

服务器端:

ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(PORT);
            serverSocket.setSoTimeout(1600000);

            System.out.println(" Waiting a client ... ");
            Socket socket = serverSocket.accept();
            int i = 0;
            DataOutputStream out = new DataOutputStream(
                    socket.getOutputStream());



            while (true) {


                DataInputStream in = new DataInputStream(
                        socket.getInputStream());



            System.out.println(String.valueOf(in.readFloat()));

            }

        } catch (IOException e) {
            System.err.println(" Eroare IO \n" + e);


        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }