You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

6 years ago
  1. /* Angelehnt an Kurose/Ross Computernetzwerke (bis 5e Java, ab 6e Python) */
  2. package verteiltesysteme.socket.simple;
  3. import java.io.*;
  4. import java.net.*;
  5. class UDPClient {
  6. public static void main(String args[]) throws Exception {
  7. String hostname = "localhost";
  8. int udpPort = 36037;
  9. BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
  10. DatagramSocket clientSocket = new DatagramSocket();
  11. InetAddress IPAddress = InetAddress.getByName(hostname);
  12. byte[] sendData = new byte[1024];
  13. byte[] receiveData = new byte[1024];
  14. System.out.println("Enter message to sent to the server:");
  15. String sentence = inFromUser.readLine();
  16. sendData = sentence.getBytes();
  17. DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, udpPort);
  18. clientSocket.send(sendPacket);
  19. DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
  20. clientSocket.receive(receivePacket);
  21. String modifiedSentence = new String(receivePacket.getData());
  22. System.out.println("FROM UDP SERVER:" + modifiedSentence);
  23. clientSocket.close();
  24. }
  25. }