Fast fertig IP-Einstellung fehlt noch
This commit is contained in:
parent
eddd07057b
commit
78af656f78
@ -10,6 +10,7 @@ import controller.ConnectController;
|
|||||||
import controller.ReceiveAdapter;
|
import controller.ReceiveAdapter;
|
||||||
import controller.SendController;
|
import controller.SendController;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
import static javax.swing.JOptionPane.CLOSED_OPTION;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
import model.Transmitter;
|
import model.Transmitter;
|
||||||
import view.ChatView;
|
import view.ChatView;
|
||||||
@ -25,7 +26,7 @@ public class Start
|
|||||||
//Auswahl für Server oder Client
|
//Auswahl für Server oder Client
|
||||||
String[] options = {"Server","Client"};
|
String[] options = {"Server","Client"};
|
||||||
int entscheidung = JOptionPane.showOptionDialog(null, "Server oder Client Modus?","Auswahl des Operationmoduses",JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
|
int entscheidung = JOptionPane.showOptionDialog(null, "Server oder Client Modus?","Auswahl des Operationmoduses",JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
|
||||||
if(entscheidung > 1)
|
if(entscheidung == CLOSED_OPTION)
|
||||||
{
|
{
|
||||||
JOptionPane.showMessageDialog(null, "Unbekannter Fehler");
|
JOptionPane.showMessageDialog(null, "Unbekannter Fehler");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
@ -33,18 +34,23 @@ public class Start
|
|||||||
//
|
//
|
||||||
|
|
||||||
ChatView view = new ChatView();
|
ChatView view = new ChatView();
|
||||||
|
if(entscheidung==0) view.setTitle("Server");
|
||||||
|
if(entscheidung==1) view.setTitle("Client");
|
||||||
Transmitter model = new Transmitter(entscheidung);
|
Transmitter model = new Transmitter(entscheidung);
|
||||||
ConnectController conncontroller = new ConnectController();
|
|
||||||
|
|
||||||
SendController sendcontroller = new SendController(view);
|
ConnectController conncontroller = new ConnectController(view,model);
|
||||||
|
|
||||||
|
Thread chatter = new Thread(model);
|
||||||
|
chatter.start();
|
||||||
|
|
||||||
|
SendController sendcontroller = new SendController(view,model);
|
||||||
sendcontroller.registerEvents();
|
sendcontroller.registerEvents();
|
||||||
|
|
||||||
ReceiveAdapter adapter = new ReceiveAdapter(model);
|
ReceiveAdapter adapter = new ReceiveAdapter(view,model);
|
||||||
|
adapter.registerEvents();
|
||||||
|
|
||||||
view.setVisible(true);
|
view.setVisible(true);
|
||||||
|
|
||||||
model.run();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param args the command line arguments
|
* @param args the command line arguments
|
||||||
|
@ -6,14 +6,21 @@
|
|||||||
|
|
||||||
package controller;
|
package controller;
|
||||||
|
|
||||||
|
import model.Transmitter;
|
||||||
|
import view.ChatView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Apollo
|
* @author Apollo
|
||||||
*/
|
*/
|
||||||
public class ConnectController
|
public class ConnectController
|
||||||
{
|
{
|
||||||
public ConnectController()
|
ChatView view;
|
||||||
{
|
Transmitter model;
|
||||||
|
|
||||||
|
public ConnectController(ChatView view,Transmitter model)
|
||||||
|
{
|
||||||
|
this.view = view;
|
||||||
|
this.model = model;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ package controller;
|
|||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
import model.Transmitter;
|
import model.Transmitter;
|
||||||
|
import view.ChatView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -16,14 +17,24 @@ import model.Transmitter;
|
|||||||
*/
|
*/
|
||||||
public class ReceiveAdapter implements Observer
|
public class ReceiveAdapter implements Observer
|
||||||
{
|
{
|
||||||
public ReceiveAdapter(Transmitter model)
|
ChatView view;
|
||||||
{
|
Transmitter model;
|
||||||
|
|
||||||
|
public ReceiveAdapter(ChatView view,Transmitter model)
|
||||||
|
{
|
||||||
|
this.view = view;
|
||||||
|
this.model = model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerEvents()
|
||||||
|
{
|
||||||
|
model.addObserver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable arg0, Object arg1)
|
public void update(Observable arg0, Object arg1)
|
||||||
{
|
{
|
||||||
|
String message = model.getNachricht();
|
||||||
|
view.getChatanzeige().append(message+"\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ package controller;
|
|||||||
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import model.Transmitter;
|
||||||
import view.ChatView;
|
import view.ChatView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -17,11 +18,13 @@ import view.ChatView;
|
|||||||
public class SendController implements ActionListener
|
public class SendController implements ActionListener
|
||||||
{
|
{
|
||||||
ChatView view;
|
ChatView view;
|
||||||
|
Transmitter model;
|
||||||
String input;
|
String input;
|
||||||
|
|
||||||
public SendController(ChatView view)
|
public SendController(ChatView view,Transmitter model)
|
||||||
{
|
{
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
this.model = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerEvents()
|
public void registerEvents()
|
||||||
@ -29,13 +32,15 @@ public class SendController implements ActionListener
|
|||||||
view.getInputField().addActionListener(this);
|
view.getInputField().addActionListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent arg0)
|
public void actionPerformed(ActionEvent arg0)
|
||||||
{
|
{
|
||||||
input = view.getInputField().getText();
|
input = view.getInputField().getText();
|
||||||
|
|
||||||
view.getChatanzeige().setText(input.toString());
|
view.getChatanzeige().append(input + "\n");
|
||||||
|
view.getInputField().setText("");
|
||||||
|
|
||||||
|
model.send(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
* To change this template file, choose Tools | Templates
|
* To change this template file, choose Tools | Templates
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package model;
|
package model;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
@ -31,101 +30,125 @@ public class Transmitter extends Observable implements Runnable
|
|||||||
private static final String IP_ADRESSE = "127.0.0.1";
|
private static final String IP_ADRESSE = "127.0.0.1";
|
||||||
|
|
||||||
int modus;
|
int modus;
|
||||||
|
private String nachricht;
|
||||||
|
String message;
|
||||||
|
OutputStream oStream;
|
||||||
|
InputStream iStream;
|
||||||
|
InputStreamReader isr;
|
||||||
|
OutputStreamWriter osr;
|
||||||
|
BufferedReader in;
|
||||||
|
PrintWriter out;
|
||||||
|
Socket s;
|
||||||
|
|
||||||
public Transmitter(int modus)
|
public Transmitter(int modus)
|
||||||
{
|
{
|
||||||
|
this.modus = modus;
|
||||||
|
nachricht = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
if(modus == 0)
|
if (modus == 0)
|
||||||
{
|
{
|
||||||
|
ServerSocket sSocket;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
server();
|
sSocket = new ServerSocket(PORT);
|
||||||
|
lg.info("Server: Warte auf Verbindung ...");
|
||||||
|
s = sSocket.accept(); // Achtung: blockiert!
|
||||||
|
lg.info("Server: Verbindung akzeptiert");
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(modus == 1)
|
else if (modus == 1)
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
client();
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void client() throws IOException
|
|
||||||
{
|
{
|
||||||
lg.info("Client: verbinde ...");
|
lg.info("Client: verbinde ...");
|
||||||
Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
|
try
|
||||||
lg.info("Client: Verbindung hergestellt");
|
|
||||||
InputStream iStream = s.getInputStream();
|
|
||||||
OutputStream oStream = s.getOutputStream();
|
|
||||||
|
|
||||||
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8");
|
|
||||||
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8");
|
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(isr);
|
|
||||||
//BufferedWriter out = new BufferedWriter(osr);
|
|
||||||
PrintWriter out = new PrintWriter(osr);
|
|
||||||
|
|
||||||
lg.info("Client: Stream initialisiert");
|
|
||||||
|
|
||||||
out.println("Hallo Du Server Du - ich bin der client");
|
|
||||||
out.flush(); // wirklich absenden!!
|
|
||||||
|
|
||||||
lg.info("Client: Nachricht versendet");
|
|
||||||
|
|
||||||
String quittung = in.readLine(); // Achtung blockiert
|
|
||||||
lg.info("Client: Quittung empfangen");
|
|
||||||
|
|
||||||
System.out.println("Client: Quittung EMPFANGEN - " + quittung);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
in.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void server() throws IOException
|
|
||||||
{
|
{
|
||||||
ServerSocket sSocket = new ServerSocket(PORT);
|
s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
|
||||||
lg.info("Server: Warte auf Verbindung ...");
|
lg.info("Client: Verbindung hergestellt");
|
||||||
Socket s = sSocket.accept(); // Achtung: blockiert!
|
}
|
||||||
lg.info("Server: Verbindung akzeptiert");
|
catch (IOException ex)
|
||||||
InputStream iStream = s.getInputStream();
|
{
|
||||||
OutputStream oStream = s.getOutputStream();
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
InputStreamReader isr = new InputStreamReader(iStream, "UTF-8");
|
try
|
||||||
OutputStreamWriter osr = new OutputStreamWriter(oStream, "UTF-8");
|
{
|
||||||
|
iStream = s.getInputStream();
|
||||||
|
oStream = s.getOutputStream();
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(isr);
|
isr = new InputStreamReader(iStream, "UTF-8");
|
||||||
|
osr = new OutputStreamWriter(oStream, "UTF-8");
|
||||||
|
|
||||||
|
in = new BufferedReader(isr);
|
||||||
//BufferedWriter out = new BufferedWriter(osr);
|
//BufferedWriter out = new BufferedWriter(osr);
|
||||||
PrintWriter out = new PrintWriter(osr);
|
out = new PrintWriter(osr);
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
if (modus == 0)
|
||||||
|
{
|
||||||
lg.info("Server: Stream initialisiert");
|
lg.info("Server: Stream initialisiert");
|
||||||
lg.info("Server: warte auf Nachricht ...");
|
}
|
||||||
|
else if (modus == 1)
|
||||||
|
{
|
||||||
|
lg.info("Client: Stream initialisiert");
|
||||||
|
}
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
nachricht = in.readLine(); // Achtung blockiert
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
|
||||||
String nachricht = in.readLine(); // Achtung blockiert
|
|
||||||
lg.info("Server: Nachricht empfangen");
|
lg.info("Server: Nachricht empfangen");
|
||||||
|
|
||||||
System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht);
|
|
||||||
|
|
||||||
out.println("Server -> ich habe die Nachricht erhalten");
|
|
||||||
lg.info("Server: Quittung versendet");
|
|
||||||
|
|
||||||
out.flush(); // wirklich absenden!!
|
out.flush(); // wirklich absenden!!
|
||||||
|
|
||||||
out.close();
|
if (nachricht.equals("beenden"))
|
||||||
in.close();
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(String input)
|
||||||
|
{
|
||||||
|
out.println(input);
|
||||||
|
out.flush(); // wirklich absenden!!
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the nachricht
|
||||||
|
*/
|
||||||
|
public String getNachricht()
|
||||||
|
{
|
||||||
|
System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht);
|
||||||
|
return nachricht;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -53,8 +53,6 @@ public class Client
|
|||||||
|
|
||||||
System.out.println("Client: Quittung EMPFANGEN - " + quittung);
|
System.out.println("Client: Quittung EMPFANGEN - " + quittung);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
out.close();
|
out.close();
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user