-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