Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.net.SocketException:套接字关闭,而套接字仍然打开_Java_Android_Sockets_Exception - Fatal编程技术网

java.net.SocketException:套接字关闭,而套接字仍然打开

java.net.SocketException:套接字关闭,而套接字仍然打开,java,android,sockets,exception,Java,Android,Sockets,Exception,因此,当收到来自谷歌云消息(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; priv

因此,当收到来自谷歌云消息(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 */
    }
}
我可以创建套接字并将数据发送到服务器,服务器也会收到,但当我想在手机上接收数据时,它会给出一个
java.net.SocketException:Socket关闭
异常。但与此同时,服务器仍在发送数据,手机上的插座也仍然处于打开状态()

但为什么会出现这种错误呢


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()
时,它正在工作。这是我已经遇到的问题,但我仍然不知道为什么会这样。

插座是关闭的。您在这个应用程序中关闭了它。对等方可能仍在尝试发送数据,但会出现“连接重置”异常。

至少您必须最终关闭内部的套接字连接实例。测试它是否为null,如果不这样做,则引用可能会保留在内存中。还要确保它没有在主线程中执行。之后,如果相同的错误仍然存在,请尝试重新安装应用程序以清理内存缓存


还要避免关闭工具类内的套接字实例。

Tools.sendDataToServer的作用是什么?你能发布那段代码吗?@eldjon:就是这段代码,它不需要out.close()行。测试null不会阻止实例在内存中持久化。Tools.close()只是
if!的“快捷方式”!空,然后关闭
。这不是一个好的做法吗?是的,这是一个问题,在关闭outputStream之后,套接字也将被关闭。但我仍然不知道为什么,因为文档中并没有真正提到它。