gpt4 book ai didi

java - W/System.err : java.net.SocketException:套接字已关闭

转载 作者:行者123 更新时间:2023-12-02 03:30:30 25 4
gpt4 key购买 nike

我有一个问题。我关闭此应用程序收到错误。虽然socket服务器还没有被访问过。能怎么解决呢?套接字服务器是 fragment 。能影响这个吗?

    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

代码:

    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;
}
}

并不总是出现此错误。谢谢

最佳答案

您应该在关闭套接字之前检查套接字是否已关闭(使用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();
}
}

}

更新

看起来问题出在这个线程上:

SocketServerThread

此线程在 fragment 内运行,因此请确保在使用 socket 时调用 socket.close(); 之前关闭此线程。在此Thread内接受(),因此当您关闭onDestory方法内的Socket时会导致错误,然后它尝试来自 Threadsocket.accept(),因为它仍在运行。

关于java - W/System.err : java.net.SocketException:套接字已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38163496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com