Browse Source

Fast fertig IP-Einstellung fehlt noch

master
nobody 5 years ago
parent
commit
78af656f78

+ 13
- 7
src/chatprogramm/Start.java View File

@@ -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);
sendcontroller.registerEvents();
ConnectController conncontroller = new ConnectController(view,model);
ReceiveAdapter adapter = new ReceiveAdapter(model);
Thread chatter = new Thread(model);
chatter.start();
view.setVisible(true);
SendController sendcontroller = new SendController(view,model);
sendcontroller.registerEvents();
model.run();
ReceiveAdapter adapter = new ReceiveAdapter(view,model);
adapter.registerEvents();
view.setVisible(true);
}
/**
* @param args the command line arguments

+ 9
- 2
src/controller/ConnectController.java View File

@@ -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;
}
}

+ 14
- 3
src/controller/ReceiveAdapter.java View File

@@ -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
- 3
src/controller/SendController.java View File

@@ -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);
}

+ 90
- 67
src/model/Transmitter.java View File

@@ -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;
@@ -29,103 +28,127 @@ public class Transmitter extends Observable implements Runnable
private static final Logger lg = OhmLogger.getLogger();
private static final int PORT = 35000;
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);
}
}
}
public void client() throws IOException
{
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.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);
try
{
iStream = s.getInputStream();
oStream = s.getOutputStream();

out.close();
in.close();
}
isr = new InputStreamReader(iStream, "UTF-8");
osr = new OutputStreamWriter(oStream, "UTF-8");

public void server() throws IOException
{
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");
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);
}

System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht);
setChanged();
notifyObservers();

lg.info("Server: Nachricht empfangen");
out.flush(); // wirklich absenden!!

if (nachricht.equals("beenden"))
{
break;
}
}

out.println("Server -> ich habe die Nachricht erhalten");
lg.info("Server: Quittung versendet");
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!!
out.close();
in.close();
}
/**
* @return the nachricht
*/
public String getNachricht()
{
System.out.println("Server: NACHRICHT EMPFANGEN - " + nachricht);
return nachricht;
}

}

+ 0
- 2
src/netz/Client.java View File

@@ -53,8 +53,6 @@ public class Client

System.out.println("Client: Quittung EMPFANGEN - " + quittung);

out.close();
in.close();
}

Loading…
Cancel
Save