Browse Source
Merge branch 'master' of git@gogs.informatik.hs-fulda.de:srieger/verteilte-systeme-bsc-ai-examples.git
master
Merge branch 'master' of git@gogs.informatik.hs-fulda.de:srieger/verteilte-systeme-bsc-ai-examples.git
master
Sebastian Rieger
6 years ago
3 changed files with 82 additions and 2 deletions
-
4VerteilteSysteme-Examples/src/verteiltesysteme/rmi/RMIEchoServer.java
-
44VerteilteSysteme-Examples/src/verteiltesysteme/socket/reuse/TCPServerReuse.java
-
36VerteilteSysteme-Examples/src/verteiltesysteme/socket/reuse/UDPServerReuse.java
@ -0,0 +1,44 @@ |
|||
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */ |
|||
|
|||
package verteiltesysteme.socket.reuse; |
|||
|
|||
import java.io.*; |
|||
import java.net.*; |
|||
|
|||
class TCPServerReuse { |
|||
public static void main(String argv[]) throws Exception { |
|||
int tcpPort = 36037; |
|||
String clientSentence; |
|||
String capitalizedSentence; |
|||
|
|||
// Server-Socket erzeugen |
|||
@SuppressWarnings("resource") |
|||
ServerSocket welcomeSocket = new ServerSocket(); |
|||
// erlaubt das Wiederverwenden von Sockets, die für die individuellen Clients genutzt werden, auch wenn der Port nocht im Status "TIME_WAIT" ist |
|||
welcomeSocket.setReuseAddress(true); |
|||
welcomeSocket.bind(new InetSocketAddress(tcpPort)); |
|||
|
|||
System.out.println("TCP Server started. Waiting for incoming requests... (reuseAddress="+welcomeSocket.getReuseAddress()+", timeout="+welcomeSocket.getSoTimeout() +", buffersize="+welcomeSocket.getReceiveBufferSize()+")"); |
|||
|
|||
while (true) { |
|||
Socket connectionSocket = welcomeSocket.accept(); |
|||
|
|||
System.out.println("Received request from client " + connectionSocket.getInetAddress() + ":" + connectionSocket.getPort() + " generating response..."); |
|||
|
|||
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); |
|||
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); |
|||
|
|||
// Try/Catch hinzugefügt, nachdem bei Einsatz Amazon AWS (Healthcheck des ELB) clientSentence null war |
|||
try { |
|||
clientSentence = inFromClient.readLine(); |
|||
if (clientSentence != null) { |
|||
capitalizedSentence = clientSentence.toUpperCase() + '\n'; |
|||
outToClient.writeBytes(capitalizedSentence); |
|||
} |
|||
} catch (IOException ioe) { |
|||
ioe.printStackTrace(); |
|||
} |
|||
connectionSocket.close(); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
/* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */ |
|||
|
|||
package verteiltesysteme.socket.reuse; |
|||
|
|||
import java.net.*; |
|||
|
|||
class UDPServerReuse { |
|||
public static void main(String args[]) throws Exception { |
|||
int udpPort = 36037; |
|||
@SuppressWarnings("resource") |
|||
DatagramSocket serverSocket = new DatagramSocket(null); |
|||
// erlaubt das Wiederverwenden des Ports, mehrere Server-Prozesse können gleichen Port nutzen |
|||
serverSocket.setReuseAddress(true); |
|||
serverSocket.bind(new InetSocketAddress(udpPort)); |
|||
byte[] receiveData = new byte[1024]; |
|||
byte[] sendData = new byte[1024]; |
|||
|
|||
System.out.println("UDP Server started. Waiting for incoming requests... (reuseAddress="+serverSocket.getReuseAddress()+", timeout="+serverSocket.getSoTimeout() +", buffersize="+serverSocket.getReceiveBufferSize()+")"); |
|||
|
|||
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(); |
|||
|
|||
System.out.println("Received request from client " + IPAddress + ":" + clientSrcPort + " generating response..."); |
|||
|
|||
String capitalizedSentence = sentence.toUpperCase(); |
|||
sendData = capitalizedSentence.getBytes(); |
|||
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort); |
|||
serverSocket.send(sendPacket); |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue