0
任何人都可以幫助我嗎?我是新來的Android開發,我一直在尋找,但沒有解決辦法的工作,我也將此方法應用於線程,但它還是老樣子沒有奏效ServerSocket.Bind()拋出異常java.net.BindException:綁定失敗:EADDRINUSE(地址已在使用中)
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
while(true){
try {
serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(8990));//this throws exception
pcSocket=serverSocket.accept();
reader=new BufferedReader(new InputStreamReader(pcSocket.getInputStream()));
writer=new PrintWriter(pcSocket.getOutputStream());
while((line=reader.readLine())!=null)message+=line;
writer.print(message+"Sent from android");
writer.flush();
writer.close();
pcSocket.close();
serverSocket.close();
return START_STICKY;
}catch (Exception e){
e.printStackTrace();
}
}
}
異常詳細信息
W/System.err﹕ java.net.BindException: bind failed: EADDRINUSE (Address already in use)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at libcore.io.IoBridge.bind(IoBridge.java:89)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:150)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at java.net.ServerSocket.bind(ServerSocket.java:319)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at java.net.ServerSocket.bind(ServerSocket.java:282)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at com.hiddensoft.android.smsannouncer.MessageReply.onStartCommand(MessageReply.java:46)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2868)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at android.app.ActivityThread.access$2100(ActivityThread.java:151)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:110)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at android.os.Looper.loop(Looper.java:193)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5292)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-22 12:41:58.739 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
04-22 12:41:58.740 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
04-22 12:41:58.740 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-22 12:41:58.740 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)
04-22 12:41:58.740 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at libcore.io.Posix.bind(Native Method)
04-22 12:41:58.740 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at libcore.io.ForwardingOs.bind(ForwardingOs.java:40)
04-22 12:41:58.740 20055-20055/com.hiddensoft.android.smsannouncer W/System.err﹕ at libcore.io.IoBridge.bind(IoBridge.java:87)
服務已啓動,並採空通過單擊按鈕
好的,但我會按時收到數據,如果不是例外,將停止服務。我必須運行服務運行更多的數據從PC接收 – SoftEye
刪除返回語句和套接字仍然aceting連接。 – julen26
如果我刪除了返回語句,那麼它會產生錯誤,因爲onStartCommand需要返回一些東西 – SoftEye