java.net.SocketException异常:当插座仍然是开放套接字关闭 [英] java.net.SocketException: Socket is closed while socket is still open

查看:226
本文介绍了java.net.SocketException异常:当插座仍然是开放套接字关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想,当有来自谷歌云消息(GCM)的通知接收来自我的服务器的消息。我的code是以下内容:

So I'm trying to receive a message from my server when there comes a notification from Google Cloud Messaging (GCM). My code is the following:

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:套接字关闭例外。但与此同时,服务器仍发送数据,在手机上的插座也依然开放(根据调试器[图] )。

I can create the Socket and send Data to the server, wich is also received, but when I want to receive data on the phone afterwards, it gives an java.net.SocketException: Socket is closed Exception. But at the same time, the server is still sending data and the socket on the phone is also still open (according to debugger [PIC]).

但为什么这个错误发生?

But why does this Error occur?



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()被注释掉了,这是工作。那是一个问题,我已经有了,但我仍然没有线索为什么会是这样的。

When out.close() is commented out, it is working. Thats a problem I already had, but I still don't have a clue why it is like this.

推荐答案

插槽的关闭。你关闭了它,在此应用程序。对等仍可能尝试发送数据,但它会得到一个连接复位的异常。

The Socket is closed. You closed it, in this application. The peer may still be trying to send data but it will get a 'connection reset' exception.

这篇关于java.net.SocketException异常:当插座仍然是开放套接字关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆