java.net.SocketException套接字在android中分别关闭

时间:2015-03-28 23:33:08

标签: java android sockets

这是我的tcp服务器线程

class Tcpserver implements Runnable{

        @Override
        public void run() {
            try
            {
                m_server=new ServerSocket(2001);
                Log.v("tcp", "srvrsckt crtd");
                final Socket connectedsocket=m_server.accept();
                Log.v("tcp", "client cnctd");
                Message clientmessage=Message.obtain();
                Log.v("tcp", "clnt msg obtain");
                ObjectInputStream ois= new ObjectInputStream(connectedsocket.getInputStream());
                String strMessage=(String) ois.readObject();
                clientmessage.obj=strMessage;
                Log.v("tcp", "b4 call handlr");
                mHandler.sendMessage(clientmessage);

                Log.v("tcp", "o/p stm obtain");
                Button b=(Button)findViewById(R.id.button2);
                b.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View v) {
                        try { Socket connectedsocketdup=connectedsocket;
                            Log.v("tcp","socketcrtdonclick");
                            ObjectOutputStream oos=new ObjectOutputStream(connectedsocketdup.getOutputStream());
                            Log.v("tcp","tcpsrvronclick");
                            oos.writeObject("Hai....mmx");
                            //oos.writeChars("hi...mmx");
                            Log.v("tcp","write");
                        } catch (IOException e1) {
                            Log.v("tcp","error"+e);
                            e1.printStackTrace();
                        }
                    }
                });
                Log.v("tcp", "write");
                ois.close();
                //oos.close();
                m_server.close();
            }

            catch(Exception e)
            {
                Message msg3=Message.obtain();
                msg3.obj=e.getMessage();
                mHandler.sendMessage(msg3);
            }
        }
    }

当我通过点击按钮然后错误

来调用单击fn时
  

java.net.SocketException套接字已关闭

获得

日志猫在下面给出 *

  

03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   V / tcp:socketcrtdonclick 03-29 04:50:11.530
  3482-3482 / com.example.pranavtv.loudspeaker V / tcp:   errorandroid.widget.EditText@40573ab0 03-29 04:50:11.530
  3482-3482 / com.example.pranavtv.loudspeaker W / System.err:   java.net.SocketException:套接字关闭03-29 04:50:11.530
  3482-3482 / com.example.pranavtv.loudspeaker W / System.err:at   org.apache.harmony.luni.net.PlainSocketImpl.checkNotClosed(PlainSocketImpl.java:140)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at   org.apache.harmony.luni.net.PlainSocketImpl.getOutputStream(PlainSocketImpl.java:240)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at java.net.Socket.getOutputStream(Socket.java:453)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at   com.example.pranavtv.loudspeaker.Wall $ TCPSERVER $ 1.onClick(Wall.java:237)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:在android.view.View.performClick(View.java:2485)03-29   04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:在android.view.View $ PerformClick.run(View.java:9080)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:在android.os.Handler.handleCallback(Handler.java:587)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:在android.os.Handler.dispatchMessage(Handler.java:92)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:在android.os.Looper.loop(Looper.java:130)03-29   04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at   android.app.ActivityThread.main(ActivityThread.java:3714)03-29   04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at java.lang.reflect.Method.invokeNative(Native Method)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at java.lang.reflect.Method.invoke(Method.java:507)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:853)   03-29 04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)03-29   04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at   de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)03-29   04:50:11.530 3482-3482 / com.example.pranavtv.loudspeaker   W / System.err:at dalvik.system.NativeStart.main(Native Method)

*

1 个答案:

答案 0 :(得分:-1)

此异常和消息表示关闭了套接字,然后继续使用它。

您可能不知道关闭套接字的输入或输出流会关闭另一个流和套接字。您正在关闭该代码底部的ObjectInputStream,但您仍然需要运行处理程序才能打开套接字。

与你在其他地方读到的内容相反,意味着对等方关闭了连接。

请勿为每条消息创建新的ObjectOutputStream。您应该在两端使用相同的对象输入和输出流作为套接字的生命周期。否则你会得到StreamCorruptedException.