我是度假的学生,我决定制作一个有趣的服务器应用程序。所以问题是:我的类 - Server.java
- 是用ServerSocket创建的服务器,但是当我尝试构造一个Thread类 - ServerThread.java
时 - 我以前从未见过的东西发生了。我调试了类,当我进入Server.java
中调用的ServerThread的构造函数时,它不会进入ServerThread
,而是进入ClassLoader.class
。我认为这通常也会在某些时候发生,但现在只调用它而不是ServerThread
的构造函数。
在过去的3天里,我一直在努力,几乎不停,但是因为皮特的爱,我无法让它发挥作用。
这是我的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();
}
}
}
}
我希望自己足够清楚......
答案 0 :(得分:0)
如果您正在逐步调试并且达到需要当前卸载的类的new
表达式,那么将进入类加载器。
这是正常行为。您可能需要多次退出才能返回代码。
答案 1 :(得分:-1)
确定。事实证明,构造函数被调用但程序在din.readLine()中被挂起。我不知道为什么会发生这种情况,因为我在消息的末尾添加了一个'\ n'字符,所以我将BufferedReader和BufferedWriter对象改为java.io.DataInputStream和java.io.DataOutputStream并使用了readUTF()和writeUTF()方法,这个工作。谢谢大家的答案和时间:)。