Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////// Datagram sockets
- public class Server {
- public static void main(String []args){
- try{
- byte[] buffer = new byte[256];
- DatagramSocket socket = new DatagramSocket(1234);
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
- System.out.println("Listening for packets...");
- socket.receive(packet);
- System.out.println("Packet recieved! it contains: ");
- for(byte b: packet.getData()){
- System.out.print((char) b);
- }
- }catch(IOException e){
- System.err.println(e.getMessage());
- }
- }
- }
- public class Client {
- public static void main(String []args){
- try{
- System.out.println("Connecting to server...");
- Socket socket = new Socket(InetAddress.getByName("localhost"), 1234);
- System.out.println("Connected to server! Writing \"Hello!\" to socket's output stream...");
- // "hello".getBytes()
- socket.getOutputStream().write(new byte[]{'h','e','l','l','o','!'});
- System.out.println("Wrote to the stream! Closing...");
- socket.close();
- System.out.println("Closed! Goodbye :) ");
- }catch(IOException e){
- System.err.print(e.getMessage());
- }
- }
- }
- //////////// Stream sockets
- public class Server {
- public static void main(String []args){
- try{
- System.out.println("Creating server socket at port 1234...");
- ServerSocket serverSocket = new ServerSocket(1234);
- // System.out.println("Acceptiong client...");
- Socket socket = serverSocket.accept();
- // System.out.println("Client accepted, wating 5 seconds for buffer...");
- byte [] buffer = new byte[256];
- Thread.sleep(5000);
- // System.out.println("Reading buffer...");
- socket.getInputStream().read(buffer);
- for(byte b: buffer){
- System.out.print((char) b);
- }
- System.out.println("That is all, closing socket...");
- socket.close();
- // System.out.println("Aufviedersehn!");
- }catch(IOException e){
- System.err.print(e.getMessage());
- }catch(InterruptedException e){
- System.err.print(e.getMessage());
- }
- }
- }
- public class Client {
- public static void main(String []args){
- try{
- System.out.println("Connecting to server...");
- Socket socket = new Socket(InetAddress.getByName("localhost"), 1234);
- System.out.println("Connected to server! Writing \"Hello!\" to socket's output stream...");
- // "hello".getBytes()
- socket.getOutputStream().write(new byte[]{'h','e','l','l','o','!'});
- System.out.println("Wrote to the stream! Closing...");
- socket.close();
- System.out.println("Closed! Goodbye :) ");
- }catch(IOException e){
- System.err.print(e.getMessage());
- }
- }
- }
- //////////// NIO
- public class Client {
- public static void main(String[] args) throws IOException {
- SocketAddress address = new InetSocketAddress(InetAddress.getByName("localhost"), 9090);
- SocketChannel channel = SocketChannel.open(address);
- Thread nit = new Thread(new Runnable() {
- @Override
- public void run() {
- ByteBuffer podaciSaServera = ByteBuffer.allocate(256);
- podaciSaServera.clear();
- while (true) {
- try {
- channel.read(podaciSaServera);
- podaciSaServera.flip();
- System.out.println();
- while (podaciSaServera.hasRemaining()) {
- System.out.print((char)podaciSaServera.get());
- }
- System.out.println();
- podaciSaServera.clear();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- nit.start();
- /// slanje na server
- Scanner scanner = new Scanner(System.in);
- ByteBuffer buffer = ByteBuffer.allocate(256);
- while(true)
- {
- String line = scanner.nextLine();
- buffer.put(line.getBytes());
- buffer.flip();
- channel.write(buffer);
- buffer.clear();
- }
- }
- }
- public class Server {
- static int port = 9090;
- static ServerSocketChannel ssc = null;
- static Selector selector = null;
- public static void main(String[] args) throws IOException {
- ssc = ServerSocketChannel.open();
- ServerSocket socket = ssc.socket();
- InetSocketAddress isa = new InetSocketAddress(port);
- socket.bind(isa);
- ssc.configureBlocking(false); // vazno
- selector = Selector.open();
- ssc.register(selector, SelectionKey.OP_ACCEPT);
- System.out.println("Server is started at port: "+ port);
- while(true)
- {
- selector.select();
- Set<SelectionKey> selectionKeys = selector.selectedKeys();
- Iterator<SelectionKey> kljucevi = selectionKeys.iterator();
- while (kljucevi.hasNext()) {
- SelectionKey selectionKey = (SelectionKey) kljucevi.next();
- kljucevi.remove();
- if(selectionKey.isAcceptable())
- {
- ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
- SocketChannel klijentskiKanal = serverSocketChannel.accept();
- klijentskiKanal.configureBlocking(false);
- String ip_adresa = klijentskiKanal.socket().getInetAddress()
- + ":" + klijentskiKanal.socket().getPort();
- klijentskiKanal.register(selector, SelectionKey.OP_READ, ip_adresa);
- }
- else if (selectionKey.isReadable())
- {
- SocketChannel klijent = (SocketChannel) selectionKey.channel();
- ByteBuffer buffer = ByteBuffer.allocate(256);
- buffer.clear();
- // citanje iz kanala
- klijent.read(buffer);
- buffer.flip();
- String poruka = selectionKey.attachment() + " ";
- while (buffer.hasRemaining()) {
- poruka += (char)buffer.get();
- }
- posaljiSvima(poruka);
- }
- }
- }
- }
- private static void posaljiSvima(String poruka) throws IOException
- {
- ByteBuffer buffer = ByteBuffer.wrap(poruka.getBytes());
- for(SelectionKey kljuc: selector.keys())
- {
- if(kljuc.isValid() && kljuc.channel() instanceof SocketChannel)
- {
- SocketChannel klijent = (SocketChannel)kljuc.channel();
- klijent.write(buffer);
- buffer.rewind();
- }
- }
- }
- }
- //////////// RMI
- public class Klijent
- {
- public static void main(String[] args)
- {
- try
- {
- System.out.println("Konektovanje na server....");
- BrojacI server = (BrojacI)Naming.lookup("rmi://localhost/Server");
- System.out.println("Brojac: " + server.count());
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public class Server
- {
- public static void main(String[] args)
- {
- try
- {
- Brojac brojac = new Brojac();
- LocateRegistry.createRegistry(1099);
- Naming.rebind("//localhost:1099/Server", brojac);
- System.out.println("Server je startovan.");
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- }
- public class Brojac extends UnicastRemoteObject implements BrojacI
- {
- private static final long serialVersionUID = -117253489961899775L;
- private int count = 0;
- public Brojac() throws RemoteException
- {
- }
- public int count() throws RemoteException
- {
- return ++count;
- }
- }
- public interface BrojacI extends Remote
- {
- public int count() throws RemoteException;
- }
- //////////// Multicast UDP
- public class Server {
- public static void main(String[] args) {
- int socketPort = Common.SOCKET_PORT;
- try {
- InetAddress adress = InetAddress.getByName(Common.ADDRESS); // group
- MulticastSocket socket = new MulticastSocket(socketPort);
- socket.setTimeToLive(1);
- socket.joinGroup(adress);
- while (true) {
- String poruka = "Pozdrav svima";
- byte[] data = poruka.getBytes();
- DatagramPacket packet = new DatagramPacket(data, data.length, adress, socketPort);
- socket.send(packet);
- System.out.println("Poslao podatke");
- Thread.sleep(5000);
- }
- } catch (IOException e) {
- //TODO: handle exception
- }catch (Exception e) {
- //TODO: handle exception
- }
- }
- }
- public class Client {
- public static void main(String[] args) {
- int socketPort = Common.SOCKET_PORT;
- try {
- InetAddress address = InetAddress.getByName(Common.ADDRESS);
- MulticastSocket socket = new MulticastSocket(socketPort);
- socket.joinGroup(address);
- byte[] data = new byte[1024];
- while (true) {
- DatagramPacket dp = new DatagramPacket(data, data.length, address, socketPort);
- socket.receive(dp);
- String poruka = new String(dp.getData());
- System.out.println(poruka.trim());
- }
- } catch (Exception e) {
- //TODO: handle exception
- }
- }
- }
- public class Common {
- public static int SOCKET_PORT = 5776;
- public static String ADDRESS = "228.5.6.7";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement