aufgabe 3
This commit is contained in:
2
praktikum-5/.gitignore
vendored
Normal file
2
praktikum-5/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
build
|
||||
result
|
||||
32
praktikum-5/aufgabe1/InternetAdresse.java
Normal file
32
praktikum-5/aufgabe1/InternetAdresse.java
Normal file
@@ -0,0 +1,32 @@
|
||||
// DK, 18.11.2022
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class InternetAdresse {
|
||||
public static void main( String args[] ) {
|
||||
try {
|
||||
InetAddress iaddr1 = InetAddress.getByName("www.fh-dortmund.de");
|
||||
InetAddress iaddr2 = InetAddress.getLocalHost();
|
||||
InetAddress iaddr3 = InetAddress.getByName("localhost");
|
||||
System.out.println(iaddr1.getHostName() + ": " + iaddr1.getHostAddress());
|
||||
System.out.println(iaddr2.getHostName() + ": " + iaddr2.getHostAddress());
|
||||
System.out.println(iaddr3.getHostName() + ": " + iaddr3.getHostAddress());
|
||||
System.out.println();
|
||||
|
||||
byte[] ip = iaddr3.getAddress();
|
||||
for(int i = 0; i < ip.length; i++) {
|
||||
System.out.println(i + ": " + ip[i]);
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
InetAddress[] iaddr4 = InetAddress.getAllByName("www.adeso.de");
|
||||
for (int i = 0; i < iaddr4.length; i++) {
|
||||
System.out.println(i + ": " + iaddr4[i].getHostName() + ": " + iaddr4[i].getHostAddress());
|
||||
}
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
System.out.println( "Host nicht bekannt" );
|
||||
}
|
||||
}
|
||||
}
|
||||
43
praktikum-5/aufgabe2/MACAdresse.java
Normal file
43
praktikum-5/aufgabe2/MACAdresse.java
Normal file
@@ -0,0 +1,43 @@
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.SocketException;
|
||||
import java.net.NetworkInterface;
|
||||
|
||||
public class MACAdresse {
|
||||
public static void main( String args[] ) {
|
||||
try {
|
||||
InetAddress iaddr1 = InetAddress.getByName("www.fh-dortmund.de");
|
||||
InetAddress iaddr2 = InetAddress.getLocalHost();
|
||||
InetAddress iaddr3 = InetAddress.getByName("localhost");
|
||||
System.out.println(iaddr1.getHostName() + ": " + iaddr1.getHostAddress());
|
||||
System.out.println(iaddr2.getHostName() + ": " + iaddr2.getHostAddress());
|
||||
System.out.println(iaddr3.getHostName() + ": " + iaddr3.getHostAddress());
|
||||
System.out.println();
|
||||
|
||||
byte[] ip = iaddr3.getAddress();
|
||||
for(int i = 0; i < ip.length; i++) {
|
||||
System.out.println(i + ": " + ip[i]);
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
InetAddress[] iaddr4 = InetAddress.getAllByName("www.adeso.de");
|
||||
for (int i = 0; i < iaddr4.length; i++) {
|
||||
System.out.println(i + ": " + iaddr4[i].getHostName() + ": " + iaddr4[i].getHostAddress());
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
var net = NetworkInterface.getByName("eth0");
|
||||
byte[] macAdress = net.getHardwareAddress();
|
||||
for (int i = 0; i < macAdress.length; i++) {
|
||||
System.out.println(i + ": " + macAdress[i]);
|
||||
}
|
||||
}
|
||||
catch (UnknownHostException e) {
|
||||
System.out.println( "Host nicht bekannt: " + e );
|
||||
}
|
||||
catch (SocketException e) {
|
||||
System.out.println("Socket exception: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
38
praktikum-5/aufgabe3/Client.java
Normal file
38
praktikum-5/aufgabe3/Client.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package aufgabe3;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Scanner;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Client {
|
||||
private int port;
|
||||
|
||||
public Client(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void send(String msg) throws IOException {
|
||||
var socket = new Socket("localhost", port);
|
||||
var out = socket.getOutputStream();
|
||||
var writer = new OutputStreamWriter(out);
|
||||
writer.write(msg);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
int port = 4711;
|
||||
var client = new Client(port);
|
||||
var reader = new Scanner(System.in);
|
||||
while (true) {
|
||||
var msg = reader.nextLine();
|
||||
client.send(msg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
42
praktikum-5/aufgabe3/Server.java
Normal file
42
praktikum-5/aufgabe3/Server.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package aufgabe3;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Server {
|
||||
private ServerSocket socket;
|
||||
|
||||
public Server(ServerSocket socket) {
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public String receive() throws IOException {
|
||||
var client = socket.accept();
|
||||
var in = client.getInputStream();
|
||||
var reader = new InputStreamReader(in);
|
||||
var result = "";
|
||||
var curr = reader.read();
|
||||
while (curr != -1) {
|
||||
result += (char)curr;
|
||||
curr = reader.read();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
int port = 4711;
|
||||
var socket = new ServerSocket(port);
|
||||
var server = new Server(socket);
|
||||
System.out.println("Listening on port " + port);
|
||||
while (true) {
|
||||
var msg = server.receive();
|
||||
System.out.println("Received message: " + msg);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
praktikum-5/build.xml
Normal file
25
praktikum-5/build.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project name="Praktikum5" default="run" basedir=".">
|
||||
|
||||
<property name="src.dir" value="."/>
|
||||
<property name="build.dir" value="build"/>
|
||||
<property name="classes.dir" value="${build.dir}/classes"/>
|
||||
<property name="main-class" value=""/>
|
||||
|
||||
<target name="clean">
|
||||
<delete dir="${build.dir}"/>
|
||||
</target>
|
||||
|
||||
<target name="compile">
|
||||
<mkdir dir="${classes.dir}"/>
|
||||
<javac srcdir="${src.dir}" destdir="${classes.dir}"
|
||||
includeantruntime="false" debug="true">
|
||||
<compilerarg value="-Xlint:unchecked"/>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<target name="run" depends="compile">
|
||||
<java classname="${main-class}" classpath="${classes.dir}" fork="true"/>
|
||||
</target>
|
||||
|
||||
</project>
|
||||
13
praktikum-5/shell.nix
Normal file
13
praktikum-5/shell.nix
Normal file
@@ -0,0 +1,13 @@
|
||||
with import <nixpkgs> {};
|
||||
mkShell {
|
||||
packages = [
|
||||
openjdk
|
||||
ant
|
||||
astyle
|
||||
jdt-language-server
|
||||
];
|
||||
|
||||
shellHook = ''
|
||||
echo "Usage Example: ant run -Dmain-class=aufgabe3.Server"
|
||||
'';
|
||||
}
|
||||
Reference in New Issue
Block a user