-1
我工作的項目,我需要一次兩個或更多的套接字連接。我注意到,直到Android 4.x我的應用程序正常工作,並在Android 4.0.4它只是沒有做第二次連接。我做了簡單的項目,以確保它像,和早期版本的兩個連接就OK了,在4.0.4它只是什麼都不做,沒有錯誤,沒有例外,沒有日誌消息....多TCP套接字連接上4.0.4簡化版,工作的Android
我如何連接雙插槽在安卓4.0.4
我的測試服務器
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
public class Main {
public static void main(String[] args) throws IOException {
ServerSocket socket1 = new ServerSocket(9045);
ServerSocket socket2 = new ServerSocket(9046);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Listener th1 = new Listener(socket1, "||");
Listener th2 = new Listener(socket2, "==");
System.out.println("Starting....");
new Thread(th1).start();
new Thread(th2).start();
while (true){
if (br.readLine().endsWith("exit"))
break;
}
th1.stop();
th2.stop();
socket1.close();
socket2.close();
}
}
class Listener implements Runnable{
int i = 1;
boolean isRunning = true;
ServerSocket socket;
String tag;
public Listener(ServerSocket socket1, String tag) {
socket = socket1;
this.tag = tag;
}
@Override
public void run() {
System.out.println(tag + " started");
while (isRunning){
try {
socket.accept();
System.out.println(tag + " accepted " + i++);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void stop() {
isRunning = false;
}
}
Android項目一次
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="Start 1"
android:onClick="clickStart1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:text="Start 2"
android:onClick="clickStart2" />
</RelativeLayout>
MainActivity.java
package com.example.twosocketsapk;
import com.example.twosocketsapk.SocketClientTask.SocketListener;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity implements SocketListener{
SocketClientTask task1;
SocketClientTask task2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
task1 = new SocketClientTask(this, "192.168.0.21", 9045);
task2 = new SocketClientTask(this, "192.168.0.21", 9046);
}
public void clickStart1(View v) {
if (task1.getStatus() != AsyncTask.Status.RUNNING)
task1.execute();
}
public void clickStart2(View v) {
if (task2.getStatus() != AsyncTask.Status.RUNNING)
task2.execute();
}
@Override
public void onConnect(String message) {
}
@Override
public void onGetMessage(String message) {
}
}
SocketClientTask.java
package com.example.twosocketsapk;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.util.Log;
public class SocketClientTask extends AsyncTask<String, String, String> {
private static final String TAG = SocketClientTask.class.getSimpleName();
private SocketListener messageHandler;
private Socket socket = null;
private PrintWriter socketOut = null;
private BufferedReader socketIn = null;
private Boolean isConnected = false;
String ip;
int port;
private boolean isRunning = true;
public SocketClientTask(SocketListener messageHandler, String ip, int port) {
this.messageHandler = messageHandler;
this.ip = ip;
this.port = port;
}
public Boolean isConnected() {
return isConnected;
}
public void runReading() throws UnknownHostException, IOException{
socket = new Socket(ip, port);
socketOut = new PrintWriter(socket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
isConnected = true;
while (isRunning) {
publishProgress(socketIn.readLine());
}
socketOut.close();
socketIn.close();
socket.close();
}
public boolean sendMessage(String line) {
if (socketOut != null && !socketOut.checkError()) {
socketOut.println(line);
socketOut.flush();
return true;
} else {
Log.w(TAG, "message: " + line + "; wasn't sent");
return false;
}
}
@Override
protected String doInBackground(String... params) {
try {
runReading();
} catch (UnknownHostException e) {
isConnected = false;
Log.e(TAG, "Socket Client ERROR", e);
} catch (IOException e) {
isConnected = false;
Log.e(TAG, "Socket Client ERROR", e);
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
messageHandler.onGetMessage(values[0]);
}
public void stop() {
isRunning = false;
}
// --
public interface SocketListener {
void onConnect(String message);
void onGetMessage(String message);
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.twosocketsapk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.twosocketsapk.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
附:對不起,我的英語,這不是我的母語
謝謝,但問題是不存在的,我的測試服務器不接受第二連接插座的任何客戶端。比方說,首先我連接到socket1然後當我試圖連接第二它只是在Android 4.0.4中沒有做任何事情。你可以在模擬器中試試這個。 – MartasSan 2013-04-26 11:49:50
正如我所說,你正在閱讀線路,但你並沒有發送它們。你的服務器代碼沒有任何類型的I/O,所以當然你的readLine()塊。由於沒有例外,你沒有任何證據表明它不接受第二個客戶。 – EJP 2013-04-26 12:34:47
我通過thees線判斷: socket.accept(); System.out.println(tag +「accepted」+ i ++); 如果我的套接字服務器不接受客戶端 - 沒有連接。 然後,爲什麼然後這段代碼在較低的android版本上工作 – MartasSan 2013-04-26 12:44:45