Hallo,
Ich dachte ich eröffne für diese Frage einen neuen Thread, damit er für spätere „Problem-Haber“ auch noch zu finden ist
Ich hatte ein Problem mit dem Erreichen meines Java-Servers über meine Internet-IP, naja stellt sich heraus es war meine kaputte Firewall die einfach alles weggeblockt hatte =(
Ich bin jetzt also temporär auf meinen funktionsfähigeren Laptop umgestiegen und hab es dort ausprobiert!
Nun, ein kleiner Erfolg ist zu vermelden:
Beim Server kommt etwas an, allerdings wird sofort darauf eine Exception geworfen
Ich habe mal zwei KSKB’s gemacht und zeige euch anschließend den Output(einmal bei localhost und einmal bei meiner Public domain):
KSKB_CLIENT:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
public class Client_KSKB {
private Socket serverConnection = null;
private ObjectOutputStream streamOut = null;
private ObjectInputStream streamIn = null;
private MainClientThread mClT;
private boolean run_Thread_run = false;
String serverAdress = "localhost\Public IP";
int serverPort = 3443;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Client_KSKB().initConnection();
}
public void stopConnection() {
run_Thread_run = false;
try {
if (serverConnection != null && !serverConnection.isClosed()) {
serverConnection.close();
System.out.println("CLOSED");
}
} catch (IOException ex) {
System.out.println("stopConnection bug");
}
serverConnection = null;
}
public void initConnection() {
stopConnection();
try {
System.out.println("Tries to init new Connection(ip: "+serverAdress+" port:"+serverPort+")");
// SocketAddress addr = new InetSocketAddress( serverAdress/*+":"+serverPort*/,serverPort );
serverConnection = new Socket(serverAdress/*+":"+serverPort*/,serverPort);
// serverConnection.connect( addr, 10000 );
System.out.println("1");
streamOut = new ObjectOutputStream(serverConnection.getOutputStream());
System.out.println("2");
streamIn = new ObjectInputStream(serverConnection.getInputStream());
streamOut.writeInt(1234);
streamOut.flush();
System.out.println("3");
run_Thread_run = true;
mClT = new MainClientThread();
System.out.println("4");
mClT.start();
System.out.println("CONNECTED");
} catch (SocketTimeoutException ex) {
ex.printStackTrace();
System.out.println("Server not responding");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("initConnection bug");
}
}
public void sendToServer(Object o) {
try {
streamOut.writeObject(o);
streamOut.flush();
} catch(Exception ex) {
System.out.println("send bug");
}
}
public boolean isConnected() {
return run_Thread_run && serverConnection.isConnected();
}
private class MainClientThread extends Thread {
@Override
public void run() {
while (run_Thread_run) {
try {
if (! serverConnection.isClosed()) {
Object o = streamIn.readObject();
System.out.println(o);
}
} catch(Exception ex) {
if (serverConnection != null && !serverConnection.isClosed())
System.out.println("read bug");
stopConnection();
}
}
}
}
}
KSKB_SERVER:
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server_KSKB {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Server_KSKB().startServer();
}
private ServerSocket serverSocket = null;
MainServerThread checkForNewClients;
boolean run_Thread_run = false;
private ArrayList<ClientThread> clients = new ArrayList<>();
int serverPort = 3443;
public void startServer() {
try {
// InetAddress thisIp = InetAddress.getByName("0.0.0.0");
serverSocket = new ServerSocket(serverPort);
System.out.println("New Server Socket(port: "+serverPort+") initiated");
} catch (IOException ex) {
System.out.println("can't init new ServerSocket(with port: "+serverPort+"): " + ex.getMessage());
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {}
run_Thread_run = true;
checkForNewClients = new MainServerThread();
checkForNewClients.start();
}
private class MainServerThread extends Thread {
@Override
public void run() {
Socket newClientConnection = null;
while (run_Thread_run) {
try {
System.out.println("Server Waiting for new Client's...");
if (serverSocket != null && ! serverSocket.isClosed())
newClientConnection = serverSocket.accept();
System.out.println("New Client tries to connect: " + newClientConnection);
ObjectInputStream newClient_InputStream = new ObjectInputStream(newClientConnection.getInputStream());
ObjectOutputStream newClient_OutputStream = new ObjectOutputStream(newClientConnection.getOutputStream());
System.out.println(newClient_InputStream.readInt());
logInClient(newClientConnection, newClient_InputStream, newClient_OutputStream);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void logInClient(Socket newClient_Socket, ObjectInputStream newClient_InputStream, ObjectOutputStream newClient_OutputStream) throws IOException {
ClientThread ct = new ClientThread(newClient_Socket, newClient_InputStream, newClient_OutputStream);
ct.start();
System.out.println("Client logged in");
clients.add(ct);
}
}
private class ClientThread extends Thread {
private Socket clientConnection;
private ObjectInputStream inputStream;
private ObjectOutputStream outputStream;
private boolean run_Thread_run = true;
public ClientThread(Socket clientConn, ObjectInputStream inputStream, ObjectOutputStream outputStream) throws IOException {
clientConnection = clientConn;
this.inputStream = inputStream;
this.outputStream = outputStream;
// userName = inputStream.readUTF();
}
public void sendToClient(Object o) throws IOException {
if (! clientConnection.isClosed() && !clientConnection.isOutputShutdown()) {
outputStream.writeObject(o);
outputStream.flush();
}
}
@Override
public void run() {
while(run_Thread_run) {
try {
if (! clientConnection.isClosed()) {
Object o = inputStream.readObject();
System.out.println("recieved: "+o);
} else {
run_Thread_run = false;
}
} catch (IOException ex) {
run_Thread_run = false;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
run_Thread_run = false;
}
}
}
public void close() {
try {
clientConnection.close();
inputStream.close();
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}```
**OUTPUT::**
localhost:
Client:
Tries to init new Connection(ip: localhost port:3443)
1
2
3
4
CONNECTED
Server:
New Server Socket(port: 3443) initiated
Server Waiting for new Client’s…
New Client tries to connect: Socket[addr=/127.0.0.1,port=1182,localport=3443]
1234
Client logged in
Server Waiting for new Client’s…
public ip:
Client:
Tries to init new Connection(ip: publicIP port:3443)
Server:
New Server Socket(port: 3443) initiated
Server Waiting for new Client’s…
New Client tries to connect: Socket[addr=/192.168.1.111,port=1183,localport=3443]
java.net.SocketException: Connection reset
Server Waiting for new Client’s…
at java.net.SocketInputStream.read(SocketInputStream.java:196)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2310)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.(ObjectInputStream.java:299)
at server_kskb.Server_KSKB$MainServerThread.run(Server_KSKB.java:54)
New Client tries to connect: Socket[addr=/192.168.1.111,port=1183,localport=3443]
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:196)
Server Waiting for new Client’s…
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2310)
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2323)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.(ObjectInputStream.java:299)
at server_kskb.Server_KSKB$MainServerThread.run(Server_KSKB.java:54)