套接字绑定Android服务

时间:2013-11-18 17:36:12

标签: android sockets service

我有一个必须从服务器等待命令的服务。现在我需要从一开始就创建一个套接字,直到服务的生命结束。我认为在onCreate()方法中使用它并将Socket保存在私有变量中并在之后的BindService()中使用它但它不起作用。我的错误在哪里或错误在哪里?谢谢大家。

package com.example.testercomunicationandroidservlet;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.provider.ContactsContract.Contacts.Data;
import android.telephony.TelephonyManager;
import android.util.Log;

public class BindService extends Service {

    public BindService() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public IBinder onBind(Intent arg0) 
        {
            // TODO Auto-generated method stub
            return null;
        }

    public void onCreate()
        {
            try {
                    Log.i("BindService", "Service Started");
                    this.socket = new Socket("10.0.2.2", 1500);
                    this.input = new DataInputStream(this.socket.getInputStream());
                    this.output = new DataOutputStream(this.socket.getOutputStream());
                    Log.i("Test Server/Android Communication", "Socket: " + this.socket.getInetAddress().toString());
                    TelephonyManager  tm= (TelephonyManager) getSystemService(this.TELEPHONY_SERVICE);

                    this.output.writeUTF(tm.getDeviceId());

                    Log.i("Inviato IMEI: ", tm.getDeviceId());
                }
            catch (UnknownHostException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            catch (IOException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

    private Socket socket;
    private DataInputStream input;
    private DataOutputStream output;
}

logcat的

11-18 15:21:22.633: E/TCP(295): C: Error
11-18 15:21:22.633: E/TCP(295): java.net.SocketException: The operation timed out
11-18 15:21:22.633: E/TCP(295):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
11-18 15:21:22.633: E/TCP(295):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
11-18 15:21:22.633: E/TCP(295):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
11-18 15:21:22.633: E/TCP(295):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:219)
11-18 15:21:22.633: E/TCP(295):     at java.net.Socket.startupSocket(Socket.java:781)
11-18 15:21:22.633: E/TCP(295):     at java.net.Socket.<init>(Socket.java:316)
11-18 15:21:22.633: E/TCP(295):     at com.example.betacomunicate_clientandroid.TCPClient.run(TCPClient.java:53)
11-18 15:21:22.633: E/TCP(295):     at com.example.betacomunicate_clientandroid.MyActivity$connectTask.doInBackground(MyActivity.java:89)
11-18 15:21:22.633: E/TCP(295):     at com.example.betacomunicate_clientandroid.MyActivity$connectTask.doInBackground(MyActivity.java:1)
11-18 15:21:22.633: E/TCP(295):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
11-18 15:21:22.633: E/TCP(295):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-18 15:21:22.633: E/TCP(295):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-18 15:21:22.633: E/TCP(295):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-18 15:21:22.633: E/TCP(295):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-18 15:21:22.633: E/TCP(295):     at java.lang.Thread.run(Thread.java:1096)

1 个答案:

答案 0 :(得分:0)

您确定:

  • android和服务器都在同一个网络上? (它们位于同一个局域网上,其根目录不能隔离DHCP客户端)
  • android有互联网访问权限(adb shell ping kde.org)
  • android实际上可以到达服务器吗? (adb shell telnet 10.02.2.2 1500)
  • 10.0.2.2:1500有服务器吗? (查看浏览器或telnet 10.02.2.2 1500从您的计算机)

到目前为止唯一错误的是android无法连接到您的服务器。

最后,但这更像是一个设计选择,你确定要使用这样的自定义protocole吗?什么都不喜欢HTTP / REST / JSON / PROTOBUF?