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.SendController;
|
||||
import javax.swing.JOptionPane;
|
||||
import static javax.swing.JOptionPane.CLOSED_OPTION;
|
||||
import javax.swing.UIManager;
|
||||
import model.Transmitter;
|
||||
import view.ChatView;
|
||||
@ -25,7 +26,7 @@ public class Start
|
||||
//Auswahl für Server oder 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]);
|
||||
if(entscheidung > 1)
|
||||
if(entscheidung == CLOSED_OPTION)
|
||||
{
|
||||
JOptionPane.showMessageDialog(null, "Unbekannter Fehler");
|
||||
System.exit(0);
|
||||
@ -33,18 +34,23 @@ public class Start
|
||||
//
|
||||
|
||||
ChatView view = new ChatView();
|
||||
if(entscheidung==0) view.setTitle("Server");
|
||||
if(entscheidung==1) view.setTitle("Client");
|
||||
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();
|
||||
|
||||
ReceiveAdapter adapter = new ReceiveAdapter(model);
|
||||
ReceiveAdapter adapter = new ReceiveAdapter(view,model);
|
||||
adapter.registerEvents();
|
||||
|
||||
view.setVisible(true);
|
||||
|
||||
model.run();
|
||||
|
||||
}
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
|
@ -6,14 +6,21 @@
|
||||
|
||||
package controller;
|
||||
|
||||
import model.Transmitter;
|
||||
import view.ChatView;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Apollo
|
||||
*/
|
||||
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.Observer;
|
||||
import model.Transmitter;
|
||||
import view.ChatView;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -16,14 +17,24 @@ import model.Transmitter;
|
||||
*/
|
||||
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
|
||||
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.ActionListener;
|
||||
import model.Transmitter;
|
||||
import view.ChatView;
|
||||
|
||||
/**
|
||||
@ -17,11 +18,13 @@ import view.ChatView;
|
||||
public class SendController implements ActionListener
|
||||
{
|
||||
ChatView view;
|
||||
Transmitter model;
|
||||
String input;
|
||||
|
||||
public SendController(ChatView view)
|
||||
public SendController(ChatView view,Transmitter model)
|
||||
{
|
||||
this.view = view;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public void registerEvents()
|
||||
@ -29,13 +32,15 @@ public class SendController implements ActionListener
|
||||
view.getInputField().addActionListener(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0)
|
||||
{
|
||||
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
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package model;
|
||||
|
||||
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";
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
this.modus = modus;
|
||||
nachricht = "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
if(modus == 0)
|
||||
if (modus == 0)
|
||||
{
|
||||
ServerSocket sSocket;
|
||||
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)
|
||||
{
|
||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
else if(modus == 1)
|
||||
else if (modus == 1)
|
||||
{
|
||||
lg.info("Client: verbinde ...");
|
||||
try
|
||||
{
|
||||
client();
|
||||
s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
|
||||
lg.info("Client: Verbindung hergestellt");
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
iStream = s.getInputStream();
|
||||
oStream = s.getOutputStream();
|
||||
|
||||
isr = new InputStreamReader(iStream, "UTF-8");
|
||||
osr = new OutputStreamWriter(oStream, "UTF-8");
|
||||
|
||||
in = new BufferedReader(isr);
|
||||
//BufferedWriter out = new BufferedWriter(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");
|
||||
}
|
||||
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();
|
||||
|
||||
lg.info("Server: Nachricht empfangen");
|
||||
out.flush(); // wirklich absenden!!
|
||||
|
||||
if (nachricht.equals("beenden"))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
in.close();
|
||||
out.close();
|
||||
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Logger.getLogger(Transmitter.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void client() throws IOException
|
||||
public void send(String input)
|
||||
{
|
||||
lg.info("Client: verbinde ...");
|
||||
Socket s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
|
||||
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.println(input);
|
||||
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
|
||||
/**
|
||||
* @return the nachricht
|
||||
*/
|
||||
public String getNachricht()
|
||||
{
|
||||
ServerSocket sSocket = new ServerSocket(PORT);
|
||||
lg.info("Server: Warte auf Verbindung ...");
|
||||
Socket s = sSocket.accept(); // Achtung: blockiert!
|
||||
lg.info("Server: Verbindung akzeptiert");
|
||||
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("Server: Stream initialisiert");
|
||||
lg.info("Server: warte auf Nachricht ...");
|
||||
|
||||
String nachricht = in.readLine(); // Achtung blockiert
|
||||
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.close();
|
||||
in.close();
|
||||
return nachricht;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -53,8 +53,6 @@ public class Client
|
||||
|
||||
System.out.println("Client: Quittung EMPFANGEN - " + quittung);
|
||||
|
||||
|
||||
|
||||
out.close();
|
||||
in.close();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user