gpt4 book ai didi

java - 类的构造函数不创建类

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

我是一名正在度假的学生,我决定制作一个服务器应用程序来娱乐。所以问题是:我的类(class) - Server.java - 是使用 ServerSocket 创建的服务器,但是当我尝试构造 Thread 类时 - ServerThread.java - 我以前从未见过的事情发生了。我调试了该类,当我进入 Server.java 中调用的 ServerThread 构造函数时,它不会进入 ServerThread而是进入 ClassLoader.class 。我认为这通常也会在某个时候发生,但现在只有这个被调用,而不是 ServerThread 的构造函数。 .

在过去的三天里,我一直在为此苦苦挣扎,几乎不间断,但出于对皮特的爱,我无法让它发挥作用。

这是我的 Server.java 代码:

    public class Server
{
private ArrayList<Socket> sockets;
private ServerSocket ss;

// Constructor and while-accept loop all in one.
public Server( int port ) throws IOException
{
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

// All we have to do is listen
listen( port );
}

// Main routine
// Usage: java Server >port<
static public void main( String args[] ) throws Exception
{
// Get the port # from the command line
int port = 5003;
// Create a Server object, which will automatically begin
// accepting connections.
new Server( port );
}

private void listen( int port ) throws IOException
{
// Create the ServerSocket
ss = new ServerSocket(port, 0, InetAddress.getByName("10.0.0.6"));
// Tell the world we're ready to go
System.out.println( "Listening on " + ss );

sockets = new ArrayList<Socket>();

// Keep accepting connections forever
while (true)
{
// Grab the next incoming connection
Socket s = ss.accept();
// Tell the world we've got it
System.out.println( "Connection from " + s );

sockets.add(s);
// Create a new thread for this connection, and then forget
// about it
new ServerThread( this, s );
}
}

public void removeConnection(Socket socket) throws IOException
{
synchronized (sockets)
{
sockets.remove(socket);
System.out.println("Closing connection at " + socket);
socket.close();
}
}

public void sendToAll(Socket s, String msg) throws IOException
{
synchronized (sockets)
{
for (int i = 0; i < sockets.size(); i++)
{
if (!sockets.get(i).equals(s))
{
new BufferedWriter(new OutputStreamWriter(sockets.get(i).getOutputStream())).write(msg);
}
}
}
}
}

这是我的 ServerThread.java 代码:

    public class ServerThread extends Thread
{
private Server server;
private Socket socket;

public ServerThread( Server server, Socket socket )
{
// Save the parameters
this.server = server;
this.socket = socket;
// Start up the thread
start();
}

// This runs in a separate thread when start() is called in the
// constructor.
public void run()
{
try
{
// Create a DataInputStream for communication; the client
// is using a DataOutputStream to write to us
BufferedReader din = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// Over and over, forever ...
while (true)
{
String message = "";
try
{
// ... read the next message ...
message = din.readLine();
// ... tell the world ...
System.out.println( "Sending "+message );

server.sendToAll(socket, message);
}
catch (SocketException ex)
{
break;
}
System.out.println("GG");
}
}
catch( EOFException ie )
{
ie.printStackTrace();
System.out.println("GG1");
}
catch( IOException ie )
{
// This does; tell the world!
ie.printStackTrace();
System.out.println("GG2");
}
finally
{
// The connection is closed for one reason or another,
// so have the server dealing with it
try {
server.removeConnection( socket );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

我希望我已经说得足够清楚了......

最佳答案

如果您正在逐步调试,并且到达需要当前卸载的类的 new 表达式,您进入类加载器。

这是正常行为。您将不得不退出(可能需要多次)才能返回代码。

关于java - 类的构造函数不创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17076174/

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