forked from srieger/verteilte-systeme-bsc-ai-examples
Sebastian Rieger
7 years ago
10 changed files with 226 additions and 0 deletions
-
6VerteilteSysteme-Examples/.classpath
-
1VerteilteSysteme-Examples/.gitignore
-
17VerteilteSysteme-Examples/.project
-
11VerteilteSysteme-Examples/.settings/org.eclipse.jdt.core.prefs
-
27VerteilteSysteme-Examples/src/verteiltesysteme/socket/simple/TCPClient.java
-
28VerteilteSysteme-Examples/src/verteiltesysteme/socket/simple/TCPServer.java
-
30VerteilteSysteme-Examples/src/verteiltesysteme/socket/simple/UDPClient.java
-
29VerteilteSysteme-Examples/src/verteiltesysteme/socket/simple/UDPServer.java
-
42VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterClient.java
-
35VerteilteSysteme-Examples/src/verteiltesysteme/socket/timecounter/UDPTimeCounterServer.java
@ -0,0 +1,6 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<classpath> |
|||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> |
|||
<classpathentry kind="src" path="src"/> |
|||
<classpathentry kind="output" path="bin"/> |
|||
</classpath> |
@ -0,0 +1 @@ |
|||
/bin/ |
@ -0,0 +1,17 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<projectDescription> |
|||
<name>VerteilteSysteme-Examples</name> |
|||
<comment></comment> |
|||
<projects> |
|||
</projects> |
|||
<buildSpec> |
|||
<buildCommand> |
|||
<name>org.eclipse.jdt.core.javabuilder</name> |
|||
<arguments> |
|||
</arguments> |
|||
</buildCommand> |
|||
</buildSpec> |
|||
<natures> |
|||
<nature>org.eclipse.jdt.core.javanature</nature> |
|||
</natures> |
|||
</projectDescription> |
@ -0,0 +1,11 @@ |
|||
eclipse.preferences.version=1 |
|||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled |
|||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 |
|||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve |
|||
org.eclipse.jdt.core.compiler.compliance=1.8 |
|||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate |
|||
org.eclipse.jdt.core.compiler.debug.localVariable=generate |
|||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate |
|||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error |
|||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error |
|||
org.eclipse.jdt.core.compiler.source=1.8 |
@ -0,0 +1,27 @@ |
|||
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */ |
|||
|
|||
package verteiltesysteme.socket.simple; |
|||
|
|||
import java.io.*; |
|||
import java.net.*; |
|||
|
|||
class TCPClient { |
|||
public static void main(String argv[]) throws Exception { |
|||
String hostname = "localhost"; |
|||
int tcpPort = 36037; |
|||
String sentence; |
|||
String modifiedSentence; |
|||
|
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
Socket clientSocket = new Socket(hostname, tcpPort); |
|||
|
|||
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); |
|||
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); |
|||
|
|||
sentence = inFromUser.readLine(); |
|||
outToServer.writeBytes(sentence + '\n'); |
|||
modifiedSentence = inFromServer.readLine(); |
|||
System.out.println("FROM SERVER: " + modifiedSentence); |
|||
clientSocket.close(); |
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */ |
|||
|
|||
package verteiltesysteme.socket.simple; |
|||
|
|||
import java.io.*; |
|||
import java.net.*; |
|||
|
|||
class TCPServer { |
|||
public static void main(String argv[]) throws Exception { |
|||
int tcpPort = 36037; |
|||
String clientSentence; |
|||
String capitalizedSentence; |
|||
|
|||
// Server-Socket erzeugen |
|||
ServerSocket welcomeSocket = new ServerSocket(tcpPort); |
|||
|
|||
while (true) { |
|||
Socket connectionSocket = welcomeSocket.accept(); |
|||
|
|||
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); |
|||
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); |
|||
|
|||
clientSentence = inFromClient.readLine(); |
|||
capitalizedSentence = clientSentence.toUpperCase() + '\n'; |
|||
outToClient.writeBytes(capitalizedSentence); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */ |
|||
|
|||
package verteiltesysteme.socket.simple; |
|||
|
|||
import java.io.*; |
|||
import java.net.*; |
|||
|
|||
class UDPClient { |
|||
public static void main(String args[]) throws Exception { |
|||
String hostname = "localhost"; |
|||
int udpPort = 36037; |
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
DatagramSocket clientSocket = new DatagramSocket(); |
|||
InetAddress IPAddress = InetAddress.getByName(hostname); |
|||
byte[] sendData = new byte[1024]; |
|||
byte[] receiveData = new byte[1024]; |
|||
|
|||
String sentence = inFromUser.readLine(); |
|||
sendData = sentence.getBytes(); |
|||
|
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|||
clientSocket.send(sendPacket); |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
clientSocket.receive(receivePacket); |
|||
|
|||
String modifiedSentence = new String(receivePacket.getData()); |
|||
System.out.println("FROM UDP SERVER:" + modifiedSentence); |
|||
clientSocket.close(); |
|||
} |
|||
} |
@ -0,0 +1,29 @@ |
|||
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */ |
|||
|
|||
package verteiltesysteme.socket.simple; |
|||
|
|||
import java.io.*; |
|||
import java.net.*; |
|||
|
|||
class UDPServer { |
|||
public static void main(String args[]) throws Exception { |
|||
int udpPort = 36037; |
|||
DatagramSocket serverSocket = new DatagramSocket(udpPort); |
|||
byte[] receiveData = new byte[1024]; |
|||
byte[] sendData = new byte[1024]; |
|||
|
|||
while (true) { |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
serverSocket.receive(receivePacket); |
|||
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength()); |
|||
|
|||
InetAddress IPAddress = receivePacket.getAddress(); |
|||
int clientSrcPort = receivePacket.getPort(); |
|||
|
|||
String capitalizedSentence = sentence.toUpperCase(); |
|||
sendData = capitalizedSentence.getBytes(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort); |
|||
serverSocket.send(sendPacket); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,42 @@ |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
import java.text.DateFormat; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
class UDPTimeCounterClient { |
|||
public static void main(String args[]) throws Exception { |
|||
String hostname = "192.168.73.158"; |
|||
int udpPort = 36038; |
|||
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); |
|||
DatagramSocket clientSocket = new DatagramSocket(); |
|||
InetAddress IPAddress = InetAddress.getByName(hostname); |
|||
byte[] sendData = new byte[1024]; |
|||
byte[] receiveData = new byte[1024]; |
|||
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|||
|
|||
System.out.println("Bitte geben Sie eine Zahl ein: "); |
|||
Long number = new Long(inFromUser.readLine()); |
|||
sendData = number.toString().getBytes(); |
|||
|
|||
System.out.println(System.lineSeparator()); |
|||
System.out.println("Zeit vor Versand = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort); |
|||
clientSocket.send(sendPacket); |
|||
System.out.println("Zeit nach Versand = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))); |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
clientSocket.receive(receivePacket); |
|||
System.out.println("Zeit nach Empfang = " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))); |
|||
System.out.println(System.lineSeparator()); |
|||
|
|||
String result = new String(receivePacket.getData()); |
|||
System.out.println("Ausgabe vom Server:" + System.lineSeparator() + result); |
|||
System.out.println(System.lineSeparator()); |
|||
clientSocket.close(); |
|||
} |
|||
} |
@ -0,0 +1,35 @@ |
|||
package verteiltesysteme.socket.timecounter; |
|||
|
|||
import java.net.DatagramPacket; |
|||
import java.net.DatagramSocket; |
|||
import java.net.InetAddress; |
|||
import java.text.DateFormat; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
|
|||
class UDPTimeCounterServer { |
|||
private static long counter = 0; |
|||
|
|||
public static void main(String args[]) throws Exception { |
|||
int udpPort = 36038; |
|||
@SuppressWarnings("resource") |
|||
DatagramSocket serverSocket = new DatagramSocket(udpPort); |
|||
byte[] receiveData = new byte[1024]; |
|||
byte[] sendData = new byte[1024]; |
|||
DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); |
|||
|
|||
while (true) { |
|||
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); |
|||
serverSocket.receive(receivePacket); |
|||
long number = new Long(new String(receivePacket.getData(), 0, receivePacket.getLength())); |
|||
|
|||
InetAddress IPAddress = receivePacket.getAddress(); |
|||
int clientSrcPort = receivePacket.getPort(); |
|||
|
|||
counter += number; |
|||
sendData = (Long.toString(counter) + " Zeit: " + System.currentTimeMillis() + " " + df.format(new Date(System.currentTimeMillis()))).getBytes(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort); |
|||
serverSocket.send(sendPacket); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue