Hallo liebe Java Community. Ich habe letzhin wieder damit angefangen, einen Socketserver ( der bis jetzt nur lesen kann ;-) ) zu programmieren, der eigentlich relativ gut funktioniert. Mein einziges Problem ist, dass sobald sich ein Client verbindet, "null" ausgegeben wird. Ich weiss, dass diese Ausgabe von der Exception bei server.accept() verursacht wird, aber ich weiss nicht wieso. Wisst ihr da vielleicht Rat? Und könntet ihr euch den Code des Server ansehen und eventuell sagen, was ihr verbessern würdet?
Vielen Dank im Voraus
Robert G.
Code:
Server.java
Connection.java
Vielen Dank im Voraus
Robert G.
Code:
Server.java
Java:
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;
// Main server class
public class Server extends Thread
{
// Server socket
private ServerSocket server;
// Vector containing the connections
private Vector<Connection> connections;
// Constructor
public Server ( )
{
try
{
this.server = new ServerSocket(8080);
}
catch ( Exception e )
{
System.out.println(e.getMessage());
}
this.connections = new Vector<Connection>();
this.start();
}
// Listen for incoming connections
public void run ( )
{
System.out.println("Server setup successful!");
while ( true )
{
try
{
Socket client = this.server.accept();
Connection connection = new Connection(this, this.connections.size(), client);
this.connections.add(connection);
connection.start();
}
catch ( Exception e )
{
System.out.println(e.getMessage()); // HIER WIRD "null" AUSGEGEBEN
}
}
}
// Removes a connection
public void removeConnection ( int id )
{
this.connections.remove(id);
}
// Returns the vector containing the connections
public Vector getConnections ( )
{
return this.connections;
}
}
Connection.java
Java:
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
// Handles the connection to a single client
public class Connection extends Thread
{
// Reference to the server
private Server server;
// ID of the connection
private int id;
// Reference to the client
private Socket client;
// Client inputstream
private InputStream in;
// Client outputstream
private OutputStream out;
// Constructor
public Connection ( Server server, int id, Socket client )
{
this.server = server;
this.id = id;
this.client = client;
try
{
this.in = client.getInputStream();
this.out = client.getOutputStream();
}
catch ( Exception e )
{
System.out.println(e.getMessage());
}
this.start();
}
// Listens for client input
public void run ( )
{
System.out.println("New client connected!");
byte[] buffer = new byte[1024];
int received = -1;
while ( true )
{
try
{
received = in.read(buffer);
}
catch ( Exception e )
{
System.out.println(e.getMessage());
}
if ( received == -1 )
{
break;
}
String content = new String(buffer);
content = content.trim();
System.out.println(content);
}
try
{
this.client.close();
}
catch ( Exception e )
{
System.out.println(e.getMessage());
}
this.server.removeConnection(this.id);
System.out.println("Client disconnected! " + this.server.getConnections().size() + " client(s) left");
}
}