我正在编写一个简单的Android代码,用于通过Java套接字进行文件传输。首先,我在服务器实现中硬编码了我的文件路径,以检查文件是否在客户端成功接收。它工作正常。然后我创建了一个简单的文件浏览器作为单独的Java类,以便用户可以浏览文件并选择它们。为此,我创建了一个简单的意图来启动文件浏览器类(意图在套接字初始化后触发)。我的问题从这里开始。 在此文件浏览器之前,我的应用程序运行正常(使用预定义的文件集)。但现在,每次运行应用程序时,我都会收到套接字关闭异常。它甚至没有达到意图射击阶段。请帮我。我主要是自学并且还在学习Android编程。这是我的代码片段:
服务器:
public class MainActivity extends Activity {
ArrayList<File> fileArray = new ArrayList<File>();
ArrayList<String> filepathNameArray = new ArrayList<String>();
Socket socket = null;
ServerSocket serversocket = null;
int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread trd = new Thread(new Runnable() {
@Override
public void run() {
initializeServer();
Intent launchFileManager = new Intent(getBaseContext(),
FileChooserActivity.class);
startActivityForResult(launchFileManager, REQUEST_CODE);
if (!fileArray.isEmpty()) {
for (int i = 0; i < fileArray.size(); i++)
copyFile(fileArray.get(i), fileArray.get(i).getName());
}
}
});
trd.start();
}
private void initializeServer() {
try {
serversocket = new ServerSocket(4444);
} catch (IOException e) {
e.printStackTrace();
Log.d("Listen failed", "Listening to port 4444 failed");
}
try {
socket = serversocket.accept();
} catch (IOException e) {
e.printStackTrace();
Log.d("Binding Failed", "Couldn't accept socket connection");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
filepathNameArray = data.getExtras().getStringArrayList("key");// file URI received
for(int f=0;f<filepathNameArray.size();f++){
fileArray.add(new File(filepathNameArray.get(f))); //create file array
}
}
if (resultCode == RESULT_CANCELED) {
Toast cancelToast = Toast.makeText(getBaseContext(),
"Nothing selected", Toast.LENGTH_SHORT);
cancelToast.show();
}
}
}
FileChooser返回意图结果:
selectionDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// selFilePathList is the file path list of selected files
Intent returnIntent=new Intent();
returnIntent.putStringArrayListExtra("key", selFilePathList);
setResult(RESULT_OK, returnIntent);
finish();
}
});
}
客户端:
public class Receiver {
static Socket socket = null;
public static void main(String[] args) {
InetAddress serverIP;
try {
serverIP = InetAddress.getByName("10.87.3.91");
socket = new Socket(serverIP, 4444);
if (socket != null) {
System.out.println("Connection established!!");
for (int j = 0; j < 5; j++)
receiveDOS();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我还添加了必要的舱单许可:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
控制台输出:
03-24 22:26:02.452: W/System.err(22973): java.net.SocketException: Socket closed
03-24 22:26:02.452: W/System.err(22973): at libcore.io.Posix.accept(Native Method)
03-24 22:26:02.452: W/System.err(22973): at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:55)
03-24 22:26:02.452: W/System.err(22973): at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
03-24 22:26:02.452: W/System.err(22973): at java.net.ServerSocket.implAccept(ServerSocket.java:202)
03-24 22:26:02.452: W/System.err(22973): at java.net.ServerSocket.accept(ServerSocket.java:127)
03-24 22:26:02.452: W/System.err(22973): at com.example.zztest.MainActivity.initializeServer(MainActivity.java:57)
03-24 22:26:02.452: W/System.err(22973): at com.example.zztest.MainActivity.access$0(MainActivity.java:49)
03-24 22:26:02.452: W/System.err(22973): at com.example.zztest.MainActivity$1.run(MainActivity.java:36)
03-24 22:26:02.462: W/System.err(22973): at java.lang.Thread.run(Thread.java:856)
03-24 22:26:02.462: D/Binding Failed(22973): Couldn't accept socket connection
任何想法或建议都将受到高度赞赏。提前谢谢!