1

I have a problem. I close this application receive erroare. Although socket server has not been accessed. How can it solve? socket server is fragment. Can influence this?

    W/System.err: java.net.SocketException: Socket closed
W/System.err:     at java.net.PlainSocketImpl.socketAccept(Native Method)
W/System.err:     at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:389)
W/System.err:     at java.net.ServerSocket.implAccept(ServerSocket.java:531)
W/System.err:     at java.net.ServerSocket.accept(ServerSocket.java:499)
W/System.err:     at ro.vrt.videoplayerstreaming.Torrent_remote$SocketServerThread.run(Torrent_remote.java:92)
W/System.err:     at java.lang.Thread.run(Thread.java:761)

              --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: Thread-9
                  Process: ro.vrt.videoplayerstreaming, PID: 2652
                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
                      at ro.vrt.videoplayerstreaming.Torrent_remote$SocketServerThread.run(Torrent_remote.java:134)
                      at java.lang.Thread.run(Thread.java:761)
D/FA: Application backgrounded. Logging engagement
D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=560890}]
V/FA: Using local app measurement service
V/FA: Local AppMeasurementService is starting up
V/FA: Bound to IMeasurementService interface
V/FA: Connected to service
V/FA: Processing queued up service tasks: 1
V/FA: Logging event: origin=auto,name=_e,params=Bundle[{_o=auto, _et=560890}]
D/FA: Unable to get advertising id: com.google.android.gms.common.GooglePlayServicesNotAvailableException: com.google.android.gms.measurement.internal.zzt.zzly(Unknown Source)
V/FA: Saving event, name, data size: _e, 30
V/FA: Event recorded: Event{appId='ro.vrt.videoplayerstreaming', name='_e', params=Bundle[{_o=auto, _et=560890}]}
V/FA: Upload scheduled in approximately ms: 3095074
V/FA: Background event processing time, ms: 20
V/FA: Inactivity, disconnecting from AppMeasurementService
V/FA: onUnbind called for intent. action: com.google.android.gms.measurement.START
V/FA: Local AppMeasurementService is shutting down

code:

    mport android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;

public class Torrent_remote extends Fragment {

    TextView info, infoip, msg;
    String message = "";
    ServerSocket serverSocket;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_torrent_remote, container, false);

        info = (TextView) v.findViewById(R.id.info);
        infoip = (TextView) v.findViewById(R.id.infoip);
        msg = (TextView) v.findViewById(R.id.msg);

        infoip.setText(getIpAddress());

        Thread socketServerThread = new Thread(new SocketServerThread());
        socketServerThread.start();


        return v;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    private class SocketServerThread extends Thread {

        static final int SocketServerPORT = 8080;
        int count = 0;

        @Override
        public void run() {
            Socket socket = null;
            DataInputStream dataInputStream = null;
            DataOutputStream dataOutputStream = null;

            try {
                serverSocket = new ServerSocket(SocketServerPORT);
                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        info.setText("I'm waiting here: "
                                + serverSocket.getLocalPort());
                    }
                });

                while (true) {
                    socket = serverSocket.accept();
                    dataInputStream = new DataInputStream(
                            socket.getInputStream());
                    dataOutputStream = new DataOutputStream(
                            socket.getOutputStream());

                    String messageFromClient = "";

                    //If no message sent from client, this code will block the program
                    messageFromClient = dataInputStream.readUTF();

                    count++;
                    message += "#" + count + " from " + socket.getInetAddress()
                            + ":" + socket.getPort() + "\n"
                            + "Msg from client: " + messageFromClient + "\n";

                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            msg.setText(message);
                        }
                    });

                    String msgReply = "Hello from VRT Player Android Tv, msg nr #" + count;
                    dataOutputStream.writeUTF(msgReply);

                    String url = messageFromClient;
                    //Put the value
                    Torrent_fragment ldf = new Torrent_fragment();
                    Bundle args = new Bundle();
                    args.putString("url", url);
                    ldf.setArguments(args);
                    getFragmentManager().beginTransaction().replace(R.id.frame, ldf).commit();



                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                final String errMsg = e.toString();
                getActivity().runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        msg.setText(errMsg);
                    }
                });

            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (dataInputStream != null) {
                    try {
                        dataInputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (dataOutputStream != null) {
                    try {
                        dataOutputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

    }

    private String getIpAddress() {
        String ip = "";
        try {
            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                    .getNetworkInterfaces();
            while (enumNetworkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = enumNetworkInterfaces
                        .nextElement();
                Enumeration<InetAddress> enumInetAddress = networkInterface
                        .getInetAddresses();
                while (enumInetAddress.hasMoreElements()) {
                    InetAddress inetAddress = enumInetAddress.nextElement();

                    if (inetAddress.isSiteLocalAddress()) {
                        ip += "SiteLocalAddress: "
                                + inetAddress.getHostAddress() + "\n";
                    }

                }

            }

        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ip += "Something Wrong! " + e.toString() + "\n";
        }

        return ip;
    }
}

not always get this error. Thank you

Diaconu Liviu
  • 211
  • 4
  • 13
  • Do you *really* have a problem? All this means is that you closed he socket while blocked in `accept()` in another thread. That's an OK thing to do, and the result is this exception. Just ignore it. NB 'socket server has not been accessed' doesn't have anything to do with it. – user207421 Jul 02 '16 at 22:00

2 Answers2

1

You should check if the socket is already closed (using serverSocket.isClosed() before closing it. So, change your onDestroy code to this :

@Override
public void onDestroy() {
    super.onDestroy();

    if (serverSocket != null && !serverSocket.isClosed()) {
        try {
            serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

UPDATE

Looks like the problem is with this Thread :

SocketServerThread

this thread is running inside the fragment, so make sure you close this Thread before calling socket.close(); as you'e using socket.accept() inside this Thread, so this is causing error as you close the Socketinside onDestory method and then it tries to socket.accept() from the Thread as it's still running.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • then the best method is that before closing the application, activity from onback send socket.close from fragment ? – Diaconu Liviu Jul 02 '16 at 19:21
  • see this on how to stop a thread safely : http://stackoverflow.com/questions/8505707/android-best-and-safe-way-to-stop-thread – Ashish Ranjan Jul 02 '16 at 19:26
1

getActivity() returns null because the reference to the activity object is available when the onActivityCreated() callback is executed, not on the onCreateView().

I would suggest you to start the thread in the onActivityCreated() callback.

Lino
  • 5,084
  • 3
  • 21
  • 39