私は休暇中の学生で、趣味でサーバー アプリケーションを作成することにしました。問題は次のとおりです。私のクラスServer.java
は ServerSocket で作成されたサーバーですが、Thread クラスを構築しようとすると、これServerThread.java
までに見たことのないことが起こります。私はクラスをデバッグし、で呼び出された ServerThread のコンストラクターにステップインするとServer.java
、それは入りませんが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();
}
}
}
}
私は自分自身を十分に明確にしたことを願っています...