byte[] ip4 = {195, 113, 194, 135}; // adresa IPv4 try { InetAddress ia = InetAddress.getByAddress(ip4); InetAddress ia2 = InetAddress.getByName("195.113.194.135"); InetAddress ia3 = InetAddress.getByName("akela.mendelu.cz"); } catch (UnknownHostException e) { e.printStackTrace(); }
try { InetAddress ia = InetAddress.getByName("akela.mendelu.cz"); InetSocketAddress isa = new InetSocketAddress(ia, 80); InetSocketAddress isa2 = new InetSocketAddress("akela.mendelu.cz", 80); } catch (UnknownHostException e) { e.printStackTrace(); }
byte[] data = {0, 1, 2, 5, 19, 28}; // data, která chceme přenést DatagramPacket dgp1 = new DatagramPacket(data, data.length); DatagramPacket dgp2 = null; DatagramPacket dgp3 = null; try { dgp2 = new DatagramPacket(data, data.length, InetAddress.getByName("pocitac.domena.cz"), 12345); dgp3 = new DatagramPacket(data, data.length, new InetSocketAddress("pocitac.domena.cz", 12345)); } catch (SocketException ex) { ex.printStackTrace(); } catch (UnknownHostException ex) { ex.printStackTrace(); }
try { DatagramSocket sock1 = new DatagramSocket(); sock1.send(dgp1); // chyba (NullPointerException) – datagram ani socket nemá cíl. adresu a port sock1.send(dgp2); // funguje DatagramSocket sock2 = new DatagramSocket(55555); // 55555 je číslo portu na lokálním stroji sock2.connect(InetAddress.getByName("pocitac.domena.org"), 44444); sock2.send(dgp1); // funguje sock2.send(dgp2); // chyba – výjimka kvůli neshodě adres } catch (IOException ex) { ex.printStackTrace(); }
try { DatagramSocket sock = new DatagramSocket(20000); DatagramPacket dgp = new DatagramPacket(new byte[1000], 1000); sock.setSoTimeout(600000); // timeout 10 minut while (true) { sock.receive(dgp); sock.send(dgp); } } catch (Exception e) { ... }
import java.io.*; class Osoba implements Serializable { private String jmeno; private int vek; public Osoba(String jmeno, int vek) { this.jmeno = jmeno; this.vek = vek; } public String getJmeno() { return jmeno; } public int getVek() { return vek; } } public class Main { public static void main(String[] args) { Osoba o = new Osoba("Jana", 40); ByteArrayOutputStream baou = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baou); oos.writeObject(o); oos.flush(); oos.close(); byte[] pole = baou.toByteArray(); // V tomto poli bajtů je uložen objekt třídy Osoba. // Pole "pole" odešleme po síti. Příjemce jej konvertuje zpět na objekt následujícím způsobem: ByteArrayInputStream bais = new ByteArrayInputStream(pole); ObjectInputStream ois = new ObjectInputStream(bais); Object obj = ois.readObject(); if (obj instanceof Osoba) { Osoba o2 = (Osoba) obj; System.out.println(o2.getJmeno() + ", " + o2.getVek() + " let"); } } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } } }
try { Socket sock = new Socket("akela.mendelu.cz", 80); // připojení BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())); bw.write(request); // zapíšeme předem připravený požadavek (jeho příprava není součástí tohoto příkladu) bw.flush(); // vyprázdnění (tzn. odeslání) bufferu String line = br.readLine(); // dokud jsou data, opakuj while (line != null) { System.out.println(line); // platná data vypisuj line = br.readLine(); } sock.close(); // zavření socketu } catch (UnknownHostException ex) { ... } catch (IOException ex) { ... }
boolean quit = false; try { ServerSocket ss = new ServerSocket(22222); while (!quit) { final Socket sock = ss.accept(); Thread t = new Thread() { public void run() { try { InputStream is = sock.getInputStream(); OutputStream os = sock.getOutputStream(); ... sock.close(); } catch (IOException e) { ... } } }; t.setDaemon(true); t.start(); } } catch (Exception e) { ... }
public enum Barva { CERVENA, ZELENA, MODRA, ZLUTA } import java.io.Serializable; public class Figurka implements Serializable { final Barva barva; int x, y; public Figurka(Barva barva, int x, int y) { this.barva = barva; this.x = x; this.y = y; } @Override public String toString() { return "Figurka{" + "barva=" + barva + ", x=" + x + ", y=" + y + '}'; } } import java.io.*; import java.net.*; public class Receiver implements Runnable { int localPort; public Receiver(int localPort) { this.localPort = localPort; } @Override public void run() { ObjectInputStream ois; ServerSocket servSock = null; try { servSock = new ServerSocket(localPort); Socket sock = servSock.accept(); ois = new ObjectInputStream(sock.getInputStream()); Object o = ois.readObject(); System.out.println(o); ois.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } import java.io.*; import java.net.*; public class Sender implements Runnable { Object o; String hostName; int destPort; public Sender(Object o, String hostName, int destPort) { this.o = o; this.hostName = hostName; this.destPort = destPort; } @Override public void run() { ObjectOutputStream oos; try { Socket sock = new Socket(hostName, destPort); oos = new ObjectOutputStream(sock.getOutputStream()); oos.writeObject(o); oos.flush(); oos.close(); } catch (IOException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { final int port = 55555; Figurka figurka = new Figurka(Barva.ZELENA, 1, 5); Thread prijemce = new Thread(new Receiver(port)); prijemce.start(); Thread odesilatel = new Thread(new Sender(figurka, "localhost", port)); odesilatel.start(); } }
try { URL url = new URL("http://www.pef.mendelu.cz/"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); System.out.println("Response code: " + con.getResponseCode()); System.out.println("Content type: " + con.getContentType()); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); String s = br.readLine(); while (s != null) { System.out.println(s); s = br.readLine(); } con.disconnect(); } catch (Exception e) { ... }
double x = Math.cos(Math.PI * Math.pow(r, 2));
import static java.lang.Math.*; ... double x = cos(PI * pow(r, 2));