import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class ChatPrywatny implements Runnable { DatagramSocket udpSocket = null; @Override public void run() { try { byte[] buffer = new byte[8192]; DatagramPacket pakiet = new DatagramPacket(buffer, buffer.length); while(true) { udpSocket.receive(pakiet); String msg = new String(buffer, 0, pakiet.getLength()); Klient.obsluzPakiet(pakiet, msg); pakiet.setLength(buffer.length); } } catch (SocketException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } public void initilize() throws IOException { udpSocket = new DatagramSocket(Klient.port, Klient.adresWlasny); } public void sendPacket(String text, InetAddress target) throws IOException { byte[] wysylanyKomunikat = text.getBytes(); DatagramPacket wysylanyPakiet = new DatagramPacket(wysylanyKomunikat, wysylanyKomunikat.length, target, Klient.port); udpSocket.send(wysylanyPakiet); } }