Browse Source

changed serverSocket var name to datagramSocket to avoid confusion

master
Sebastian Rieger 6 years ago
parent
commit
7aed603c56
  1. 12
      VerteilteSysteme-Examples/src/verteiltesysteme/socket/reuse/UDPServerReuse.java

12
VerteilteSysteme-Examples/src/verteiltesysteme/socket/reuse/UDPServerReuse.java

@ -8,18 +8,18 @@ class UDPServerReuse {
public static void main(String args[]) throws Exception {
int udpPort = 36037;
@SuppressWarnings("resource")
DatagramSocket serverSocket = new DatagramSocket(null);
DatagramSocket datagramSocket = new DatagramSocket(null);
// erlaubt das Wiederverwenden des Ports, mehrere Server-Prozesse können gleichen Port nutzen
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(udpPort));
datagramSocket.setReuseAddress(true);
datagramSocket.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()+")");
System.out.println("UDP Server started. Waiting for incoming requests... (reuseAddress="+datagramSocket.getReuseAddress()+", timeout="+datagramSocket.getSoTimeout() +", buffersize="+datagramSocket.getReceiveBufferSize()+")");
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
datagramSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());
InetAddress IPAddress = receivePacket.getAddress();
@ -30,7 +30,7 @@ class UDPServerReuse {
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, clientSrcPort);
serverSocket.send(sendPacket);
datagramSocket.send(sendPacket);
}
}
}
Loading…
Cancel
Save