aufgabe 3

This commit is contained in:
2025-11-12 22:31:09 +01:00
commit 179f16d993
7 changed files with 195 additions and 0 deletions

2
praktikum-5/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
build
result

View 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" );
}
}
}

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

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

View 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
View 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
View 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"
'';
}