-
Notifications
You must be signed in to change notification settings - Fork 0
/
c5p1b.java
50 lines (40 loc) · 1.69 KB
/
c5p1b.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.*;
import java.net.*;
public class c5p1b {
// args[0] - nazwa hosta (lub IP) (hostname)
// args[1] - numer portu (port number)
public static void main(String[] args) {
if (args.length < 2) {
System.out.println(
"Podaj nazwe hosta i numer portu"
);
return;
}
try {
// utworzenie gniazda - lokalny port niewazny (socket creation on "any" port)
DatagramSocket socket = new DatagramSocket();
// ustalenie adresu na podstawie args[0] (address from args[0])
InetAddress addr = InetAddress.getByName(args[0]);
// utworzenie datagramu z wiadomoscia (creation of datagram with message)
String s = "SIK 420";
DatagramPacket p = new DatagramPacket(
s.getBytes(), s.length(),
addr, Integer.parseInt(args[1]));
// wyslanie wiadomosci (message sending)
socket.send(p);
// utworzenie "pustego" datagramu (creation of empty datagram)
byte[] bufor = new byte[256];
DatagramPacket p2 = new DatagramPacket(bufor, 256);
// czekaj na wiadomosc zwrotna (wait for reply message)
socket.receive(p2);
// utworz lancuch z tablicy bajtow (create string from array of bytes)
String response = new String(p2.getData());
// wyswietl wiadomosc zwrotna (print reply message)
System.out.println("Serwer powiedzial (server said): "+response);
// koniec protokolu (end of protocol)
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}