Browse Source

init

master
Hannes Dürr 3 years ago
parent
commit
9914171269
4 changed files with 231 additions and 0 deletions
  1. 9
    0
      .gitignore
  2. 76
    0
      src/netz/Client.java
  3. 76
    0
      src/netz/Server.java
  4. 70
    0
      src/netz/Start.java

+ 9
- 0
.gitignore View File

@@ -0,0 +1,9 @@
/build/
/nbproject/private/
*.xml
*.properties
*.mf
/dist/
/wurfel/nbproject/private/
/wurfel/dist/
/wurfel/build/

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

@@ -0,0 +1,76 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package netz;

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.Logger;

/**
* Builder Class
* @author le
*/
public class Client
{
private static final Logger lg = Logger.getLogger("netz");
private static final int PORT = 35000;
private static final String IP_ADRESSE = "127.0.0.1";
public 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);

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

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
new Client();
}
catch (IOException ex)
{
lg.severe(ex.toString());
}
}
}

+ 76
- 0
src/netz/Server.java View File

@@ -0,0 +1,76 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package netz;

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.Logger;

/**
* Builder Class
* @author le
*/
public class Server
{
private static final Logger lg = Logger.getLogger("netz");
private static final int PORT = 35000;
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 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();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
try
{
new Server();
}
catch (IOException ex)
{
lg.severe(ex.toString());
}
}
}

+ 70
- 0
src/netz/Start.java View File

@@ -0,0 +1,70 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package netz;

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 urlString, String dateiname) throws MalformedURLException, IOException
{
URL oUrl = new URL(urlString + "/" + dateiname);
InputStream iStream = oUrl.openStream();
BufferedInputStream in = new BufferedInputStream(iStream);
String tmpVerzeichnis = System.getProperty("java.io.tmpdir");
String ausgabeDateiname = tmpVerzeichnis + File.separator + dateiname;
FileOutputStream fos = new FileOutputStream(ausgabeDateiname);
BufferedOutputStream out = new BufferedOutputStream(fos);
int wert = 0;
while ( (wert = in.read()) >= 0)
{
out.write(wert);
}
in.close();
out.close(); // flush!
System.out.println("Datei " + ausgabeDateiname + " erfolgreich erstellt");
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("2 Aufrufparameter nötig: URL-String Dateiname");
}
else
{
try
{
new Start(args[0], args[1]);
}
catch (Exception ex)
{
System.err.println(ex);
ex.printStackTrace();
}
}
}
}

Loading…
Cancel
Save