我正在制作一个android应用程序,其中两个设备将通过WiFip2p2网络连接,并且客户端将从服务器接收图像。经过一段时间的测试,我已经开始收到“ java.net.SocketException:软件导致连接中止”异常。 现在我知道在stackoverflow中也有类似的问题,但是这些问题都没有一个完美的答案。 我知道此异常是不可预测的,解决该异常的唯一方法是重新启动设备,但是此异常在我的应用程序中抛出的频率很高,我想知道是否可以通过编程方式处理此异常。
这是Serer部分:
public class ServerThread extends Thread
{
@Override
public void run()
{
socketForServer=null;
try {
serverSocket = new ServerSocket(SocketServerPORT);
while (true) {
socketForServer = serverSocket.accept();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socketForServer != null) {
try {
socketForServer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}}
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
@Override
public void run() {
ContentResolver cr = getContentResolver();
try {
InputStream is = cr.openInputStream(uri);
final Bitmap bitmap= MediaStore.Images.Media.getBitmap( ServerSocketThread.this.getContentResolver(),uri);
//converting bitmap to byte array
byte[] bytes = null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bytes = stream.toByteArray();
bitmap.recycle();
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(bytes);
oos.flush();
socket.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
deletePersistentGroups();
disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void deletePersistentGroups(){
try {
Method[] methods = WifiP2pManager.class.getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals("deletePersistentGroup")) {
// Delete any persistent group
for (int netid = 0; netid < 32; netid++) {
methods[i].invoke(SendOrReceive.mWifiP2pManager, SendOrReceive.mChannel, netid, null);
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void disconnect() {
if (SendOrReceive.mWifiP2pManager != null && SendOrReceive.mChannel != null) {
SendOrReceive.mWifiP2pManager.requestGroupInfo(SendOrReceive.mChannel, new WifiP2pManager.GroupInfoListener() {
@Override
public void onGroupInfoAvailable(WifiP2pGroup group) {
if (group != null && SendOrReceive.mWifiP2pManager != null && SendOrReceive.mChannel != null
&& group.isGroupOwner()) {
SendOrReceive.mWifiP2pManager.removeGroup(SendOrReceive.mChannel, new WifiP2pManager.ActionListener() {
@Override
public void onSuccess() {
}
@Override
public void onFailure(int reason) {
}
});
}
}
});
}
}
客户端部分:
try {
socket = new Socket(address, portNumber);
File fileDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"LetsShare");
if (fileDir.exists() == false) {
fileDir.mkdirs();
}
File file = new File(fileDir, "test.jpg");
file.createNewFile();
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
FileOutputStream fos = null;
byte[] bytes = null;
try {
bytes = (byte[]) ois.readObject();
} catch (ClassNotFoundException e) {
}catch (EOFException e)
{
}
final Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
if(myBitmap==null){
Client.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("CONDITION","null");
}
});}
else{
Client.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("CONDITION","not null");
}
});
}
try {
fos = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
Client.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Client.this,
"Finished",
Toast.LENGTH_LONG).show();
}
});
scanMedia(file.toString());
//File toSet = new File(file.toString());
//imageView.setImageBitmap(BitmapFactory.decodeFile(toSet.getAbsolutePath()));
} catch (Exception e) {
Client.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Client.this,
"sorry",
Toast.LENGTH_LONG).show();
}
});
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
final String eMsg = "Something wrong from client: " + e.getMessage();
Client.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Client.this,
eMsg,
Toast.LENGTH_LONG).show();
}
});
} finally {
if (socket != null) {
try {
socket.close();
disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在客户端抛出了异常
try {
bytes = (byte[]) ois.readObject();
}
堆栈跟踪在下面给出
13:37:56.690 2552-3273/com.example.hp.letsshare W/System.err: java.net.SocketException: Software caused connection abort
06-23 13:37:56.692 2552-3273/com.example.hp.letsshare W/System.err: at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:151)
at java.net.SocketInputStream.read(SocketInputStream.java:120)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2316)
06-23 13:37:56.693 2552-3273/com.example.hp.letsshare W/System.err: at java.io.ObjectInputStream$BlockDataInputStream.read(ObjectInputStream.java:2718)
at java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:2748)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1688)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1345)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:373)
at com.example.hp.letsshare.Client$ClientThread.run(Client.java:150)
有人可以帮忙吗?