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