Init
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||
*/
|
||||
|
||||
package Aufgabe10_ChatProgramm;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* Builder Class
|
||||
* @author le
|
||||
*/
|
||||
public class Client
|
||||
{
|
||||
private static Logger lg = Logger.getLogger("netz");
|
||||
private static final int PORT = 35000; //lt. iana port > 2¹⁵
|
||||
private static final String IP = "127.0.0.1";
|
||||
|
||||
|
||||
public Client() throws IOException
|
||||
{
|
||||
lg.info("Client: versuche zu verbinden");
|
||||
Socket s = new Socket(IP, PORT); // ACHTUNG blockiert!
|
||||
lg.info("Client: Verbindung akzeptiert");
|
||||
|
||||
InputStream is = s.getInputStream();
|
||||
OutputStream os = s.getOutputStream();
|
||||
|
||||
//Umwandlung der Byteströme
|
||||
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
|
||||
OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8");
|
||||
|
||||
//Puffer
|
||||
BufferedReader in = new BufferedReader(isr);
|
||||
//BufferedWriter out = new BufferedWriter(osr);
|
||||
//besser
|
||||
PrintWriter out = new PrintWriter(osr);
|
||||
|
||||
lg.info("Client: Streams erfolgreich initialisiert");
|
||||
lg.info("Client: sende Textnachricht");
|
||||
|
||||
out.println("Hallo Du Server - ich bin ein Client");
|
||||
out.flush();
|
||||
|
||||
lg.info("Client: warte auf Serverantwort");
|
||||
|
||||
String nachricht = in.readLine(); // ACHTUNG blockiert
|
||||
lg.info("Client: Serverbestätigung erhalten");
|
||||
|
||||
lg.info("Client: fertig");
|
||||
in.close();
|
||||
out.close();
|
||||
s.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
new Client();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||
*/
|
||||
|
||||
package Aufgabe10_ChatProgramm;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
* Builder Class
|
||||
* @author le
|
||||
*/
|
||||
public class Server
|
||||
{
|
||||
private static Logger lg = Logger.getLogger("netz");
|
||||
private static final int PORT = 35000; //lt. iana port > 2¹⁵
|
||||
|
||||
|
||||
public 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 is = s.getInputStream();
|
||||
OutputStream os = s.getOutputStream();
|
||||
|
||||
//Umwandlung der Byteströme
|
||||
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
|
||||
OutputStreamWriter osr = new OutputStreamWriter(os, "UTF-8");
|
||||
|
||||
//Puffer
|
||||
BufferedReader in = new BufferedReader(isr);
|
||||
//BufferedWriter out = new BufferedWriter(osr);
|
||||
//besser
|
||||
PrintWriter out = new PrintWriter(osr);
|
||||
|
||||
lg.info("Server: Streams erfolgreich initialisiert");
|
||||
lg.info("Server: warte auf Textnachricht");
|
||||
|
||||
String nachricht = in.readLine(); // ACHTUNG blockiert
|
||||
lg.info("Server: Nachricht erhalten");
|
||||
System.out.println("Server: NACHRICHT = " + nachricht);
|
||||
|
||||
// ACHTUNG: blockiert NICHT!!!!
|
||||
out.println("Server an Client: Nachricht erhalten");
|
||||
out.flush(); // wichtig
|
||||
|
||||
lg.info("Server: fertig");
|
||||
in.close();
|
||||
out.close();
|
||||
s.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
new Server();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||
*/
|
||||
|
||||
package Aufgabe10_ChatProgramm;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Builder Class
|
||||
* @author le
|
||||
*/
|
||||
public class Start
|
||||
{
|
||||
public Start(String host, String dateiname) throws MalformedURLException, IOException
|
||||
{
|
||||
URL oURL = new URL(host + "/" + dateiname);
|
||||
InputStream inS = oURL.openStream();
|
||||
BufferedInputStream in = new BufferedInputStream(inS);
|
||||
|
||||
String tmpVerzeichnis = System.getProperty("java.io.tmpdir");
|
||||
String ausgabeDatei = tmpVerzeichnis + File.separator + dateiname;
|
||||
|
||||
FileOutputStream fos = new FileOutputStream(ausgabeDatei);
|
||||
BufferedOutputStream out = new BufferedOutputStream(fos);
|
||||
|
||||
int wert = 0;
|
||||
|
||||
while ( (wert = in.read()) >= 0)
|
||||
{
|
||||
out.write(wert);
|
||||
}
|
||||
out.flush();
|
||||
out.close();
|
||||
in.close();
|
||||
System.out.println("Datei " + dateiname +
|
||||
" im Verzeichnis " + tmpVerzeichnis +
|
||||
" gespeichert");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
if (args.length != 2)
|
||||
{
|
||||
System.err.println("2 Aufrufparameter nötig: Host und Dateiname");
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
new Start(args[0], args[1]);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.err.println(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user