Browse Source

Fast fertig IP-Einstellung fehlt noch

master
nobody 5 years ago
parent
commit
78af656f78

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

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;
//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);
// //
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);
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 * @param args the command line arguments

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



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

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

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;


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



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;


/** /**
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()
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);
} }

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

* 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;
private static final Logger lg = OhmLogger.getLogger(); private static final Logger lg = OhmLogger.getLogger();
private static final int PORT = 35000; private static final int PORT = 35000;
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)
{ {
lg.info("Client: verbinde ...");
try try
{ {
client();
s = new Socket(IP_ADRESSE, PORT); // Achtung: blockiert!
lg.info("Client: Verbindung hergestellt");
} }
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);
} }
} }
}
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.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



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


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

Loading…
Cancel
Save