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?
我有一个问题。我关闭这个应用程序接收erroare。虽然socket服务器没有被访问。如何解决吗?套接字服务器片段。会影响吗?
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
不总是得到这个错误。谢谢你!
2 个解决方案
#1
1
You should check if the socket is already closed (using serverSocket.isClosed()
before closing it. So, change your onDestroy
code to this :
您应该检查套接字是否已经关闭(使用serverSocket.isClosed(),然后关闭它。所以,将你的onDestroy代码改为:
@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 Socket
inside onDestory
method and then it tries to socket.accept()
from the Thread
as it's still running.
此线程正在片段中运行,因此请确保在调用socket.close()之前关闭该线程;在此线程中使用socket.accept(),因此当您关闭socketin onDestory方法时,它会导致错误,然后它会尝试socket.accept(),因为它仍在运行。
#2
1
getActivity()
returns null because the reference to the activity object is available when the onActivityCreated()
callback is executed, not on the onCreateView()
.
getActivity()返回null,因为当onActivityCreated()回调被执行时,对活动对象的引用是可用的,而不是onCreateView()。
I would suggest you to start the thread in the onActivityCreated()
callback.
我建议您在onActivityCreated()回调中启动线程。
#1
1
You should check if the socket is already closed (using serverSocket.isClosed()
before closing it. So, change your onDestroy
code to this :
您应该检查套接字是否已经关闭(使用serverSocket.isClosed(),然后关闭它。所以,将你的onDestroy代码改为:
@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 Socket
inside onDestory
method and then it tries to socket.accept()
from the Thread
as it's still running.
此线程正在片段中运行,因此请确保在调用socket.close()之前关闭该线程;在此线程中使用socket.accept(),因此当您关闭socketin onDestory方法时,它会导致错误,然后它会尝试socket.accept(),因为它仍在运行。
#2
1
getActivity()
returns null because the reference to the activity object is available when the onActivityCreated()
callback is executed, not on the onCreateView()
.
getActivity()返回null,因为当onActivityCreated()回调被执行时,对活动对象的引用是可用的,而不是onCreateView()。
I would suggest you to start the thread in the onActivityCreated()
callback.
我建议您在onActivityCreated()回调中启动线程。