Hallo Leute,
ich hab da ein Problem und zwar möchte ich von meiner Klasse TimeServer gerne den Socket zurückgeben (IP-Adresse des jeweiligen geöffneten Clienten). Jedoch hab ich keine Ahnung wie ich das anstellen soll denn mein Socket bleibt ja immer null bis sich ein Client beim Server anmeldet und dadurch bekomme ich eine NullPointer EXCP.
TimeServer
mfG
TimerServerThread
TimeServerGUI
ich hab da ein Problem und zwar möchte ich von meiner Klasse TimeServer gerne den Socket zurückgeben (IP-Adresse des jeweiligen geöffneten Clienten). Jedoch hab ich keine Ahnung wie ich das anstellen soll denn mein Socket bleibt ja immer null bis sich ein Client beim Server anmeldet und dadurch bekomme ich eine NullPointer EXCP.
TimeServer
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* Server Klasse
*
*/
public class TimeServer implements Runnable {
ServerSocket serverSocket;
Socket clientSocket;
OutputStreamWriter Osr;
private int i;
BufferedReader in;
public boolean running = false;
public TimeServer() throws IOException, InterruptedException {
}
/**
* ServerSocket wird angelegt (mit Port 12345) Danach wird eingstellt das
* der ServerSocket Verbindungen annimmt. Thread wird gestartet (mit
* Referenz auf ServerThread)
*
* @throws InterruptedException
* @throws IOException
*/
public void sendTime() throws InterruptedException, IOException {
while (running=true){
serverSocket = new ServerSocket(12345);
clientSocket = serverSocket.accept();
Thread t1 = new Thread(new TimerServerThread(clientSocket));
t1.start();
serverSocket.close();
}
}
public void startServer() {
running = true;
}
public void stopServer() {
running = false;
}
public boolean getRunstatus()
{
return this.running;
}
@Override
public void run() {
try {
sendTime();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
TimerServerThread
Java:
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimerServerThread extends Thread {
int i;
Socket clientSocket;
public TimerServerThread(Socket clientSocket) {
this.clientSocket = clientSocket;
}
/**
* In der run-Methoden werden nun die Lottozahlen erzeugt und
* rausgeschrieben.
*/
public void run() {
sendTime();
}
public void sendTime(){
System.out.println("Verbunden mit " + clientSocket);
OutputStreamWriter Osr = null;
BufferedWriter bw =null;
try {
Osr = new OutputStreamWriter(clientSocket.getOutputStream());
bw= new BufferedWriter(Osr);
for (i = 0; i < 6; i++) {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy.MM.dd 'at' HH:mm:ss ");
Date currentTime = new Date();
System.out.println("Zeit und Datum : "
+ formatter.format(currentTime));
bw.write("Zeit und Datum " + formatter.format(currentTime));
bw.newLine();
bw.flush();
Thread.sleep(1000);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Socket getStatus() {
return clientSocket;
}
}
Java:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class TimeServerGUI extends JFrame implements ActionListener, Runnable {
JButton bstartServer = new JButton("Run Server");
JButton bstopServer = new JButton("Stop Server");
JTextArea tAoutPutTa = new JTextArea();
TimeServer srv;
JPanel pControls = new JPanel();
public boolean running = false;
public TimeServerGUI() throws IOException, InterruptedException {
setLayout(new BorderLayout());
pControls.setLayout(new GridLayout(2, 2));
pControls.add(bstartServer);
pControls.add(bstopServer);
add(pControls, BorderLayout.WEST);
add(tAoutPutTa, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
srv = new TimeServer();
bstartServer.addActionListener(this);
bstopServer.addActionListener(this);
setVisible(true);
setSize(400, 400);
}
@Override
public void actionPerformed(ActionEvent arg0) {
Thread th2 = new Thread(srv);
if (arg0.getSource().equals(this.bstartServer)) {
System.out.println("Server started\n");
if (srv.getRunstatus() == false) {
this.tAoutPutTa.append("Server started\n");
running = true;
srv.startServer();
th2.start();
}
}
if (arg0.getSource().equals(this.bstopServer)) {
if (srv.getRunstatus() == true) {
running = false;
srv.stopServer();
this.tAoutPutTa.append("Server stopped\n");
}
}
}
public static void main(String[] args0) throws IOException,
InterruptedException {
TimeServerGUI gui = new TimeServerGUI();
Thread th1 = new Thread(gui);
th1.start();
}
public void run() {
}
}