java.net.SocketException:当socket仍然打开时,Socket关闭

时间:2014-07-26 17:35:01

标签: java android sockets exception

因此,当Google云消息传递(GCM)发出通知时,我正在尝试从我的服务器收到消息。我的代码如下:

public class GcmIntentService extends IntentService {
 //This class is executed by the BradcastReceiver when the devices
 //gets a message from GCM
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType();

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Log.i("INTENTSERVICE", extras.toString());
                sendNotification(extras);
            }
            Log.e("WAKELOCK","Wakelock will end");
            GcmBroadcastReceiver.completeWakefulIntent(intent);
        }
    }

    private void sendNotification(final Bundle extras){
                Socket socket = null;
                FileOutputStream fileStream;
                MessageSet ms = null;
                try {

                    socket = new Socket(getString(R.string.server_address), 37133);
                    Tools.sendDataToServer(getApplicationContext(), 
                        socket, 
                        MessageTypes.PULLMESSAGE, 
                        extras.getString("content")); //sending does perfectly work

                    //Receive contents of Message
                    DataInputStream dis = 
                        new DataInputStream(socket.getInputStream()); // but now is doesn't 
                                                  //want to do more and the exception is thrown
                    byte type = dis.readByte();
                    /* doing things */ 
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    //Tools.close(socket);
                }

                /* post notification */
    }
}

我可以创建Socket并将数据发送到服务器,也可以收到,但是当我想要在手机上接收数据时,它会产生java.net.SocketException: Socket is closed异常。但与此同时,服务器仍在发送数据,手机上的套接字仍处于打开状态(according to debugger [PIC])。

但为什么会出现此错误?

<小时/> 请求的代码Tools.sendDataToServer

public static void sendDataToServer(Context context, Socket socket, int messageType, String content){
        try {
            SharedPreferences prefs = context.getSharedPreferences(context.getString(R.string.SharedPrefs),0);
            OutputStream out = socket.getOutputStream();

            out.write(messageType);
            byte[] msgBytes = (prefs.getString("userID","") + "|;" + prefs.getString("userSession","") + "|;" + content).getBytes("UTF-8");
            Tools.writeDataLengthToStream(out, msgBytes.length);
            out.write(msgBytes,0,msgBytes.length);
            out.flush();
            //out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

out.close()被注释掉时,它正在运行。这是我已经遇到的问题,但我仍然不知道为什么会这样。

2 个答案:

答案 0 :(得分:1)

套接字已关闭。你在这个应用程序中关闭了它。对等体可能仍在尝试发送数据,但它将获得“连接重置”异常。

答案 1 :(得分:0)

至少你必须最终关闭里面的Socket连接实例。测试它是否为空,如果你不这样做,引用可能会在内存中持续存在。并确保它没有在主线程中执行。之后如果同样的错误仍然存​​在,请尝试重新安装应用程序以清理内存缓存。

并且还要避免关闭工具类中的Socket实例。