Hi, IŽam beginner and I try to do this. Client send datagram to server and server send datagram another client or back. I found some inspiration and i used it but I donŽt know how can i do in serve ForwardingThread. My code server:
Java Code:
package nserver;
 
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author Lolek
 */
 
public class Main {
 
    /**
     * @param args the command line arguments
     */
 
    public static void main(String[] args){       
 
            Port port = new Port();
            Address address = new Address();
            Forwarding forwarding = new Forwarding();
 
            ThreadForwarding odesli = new ThreadForwarding (port, address, forwarding);
            ThreadReciving prijem = new ThreadReciving(5000, port, address, forwarding);
 
     }    
  }
 
 
 
class ThreadReciving extends Thread
{   
 
 
    private Port p;
    private Address a;
    private int getPort;
    private InetAddress getAddress;
    private Forwarding forwarding; 
    private byte send[] = new byte[256];
    private byte sendData[] = new byte[256];
    private byte[] recived = new byte[256];
 
    DatagramSocket server;
 
    int port;   
    ThreadReciving(int port,Port p,Address a,Forwarding forwarding)
    {   
        try {
            this.forwarding = forwarding;
            this.a = a;
            this.p = p;
            this.server = new DatagramSocket(port);
            new Thread(this, "ThreadReciving").start();
        } catch (SocketException ex) {
            Logger.getLogger(ThreadReciving.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @Override
    public void run ()
    {    
 
        try 
        {            
            while(true)
            {
 
             DatagramPacket recivedPacket = new DatagramPacket(prijata,prijata.length);
             server.receive(recivedPacket);
 
             String recivedStr = new String(recivedPacket.getData(),0,recivedPacket.getLength());
 
             send = recived.getBytes();
             forwarding.setData(odeslana);
 
             if (recived.equals("add"))
             {
                getPort = recivedPacket.getPort();            
                p.setData(getPort);               
 
 
                getAddress = recivedPacket.getAddress();
                a.setData(getAddress);                                
             }
 
 
             System.out.println("client:"+recived);
            }            
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(ThreadReciving.class.getName()).log(Level.SEVERE, null, ex);
        }    
    }
}
 
 
class ThreadForwarding extends Thread
{
 private Forwarding forwarding;
 private InetAddress client;
 private int port;
 private DatagramSocket server;
 private Port p;
 private Address a;
 
 VlaknoPreposilaci(Port p,Address a,Forwarding forwarding)
 {
        try {
            this.forwarding = forwarding;
            this.p = p;
            this.a = a;
            this.server = new DatagramSocket();           
            new Thread(this, "ThreadForwarding").start();
        } catch (SocketException ex) {
            Logger.getLogger(ThreadForwarding.class.getName()).log(Level.SEVERE, null, ex);
        }
 }
 
    @Override
 public void run()
 {  p.getData();
    a.getData();
    prepos.getData();
 
 
   // while(true)
   // {   
            try {
 
 
 
 
                DatagramPacket ForwardingPacket = new DatagramPacket(forwarding.send, forwarding.send.length, a.addressArray[0], p.addressArray[0]);
                server.send(ForwardingPacket);
            } catch (IOException ex) {
                Logger.getLogger(ThreadForwarding.class.getName()).log(Level.SEVERE, null, ex);
            }
 
    //}
 }
 
}
 
 
class Port {
    private int portt;
    private int i;
    private int n = 0;
    private boolean bool = true;
    private boolean stop = false;
 
    int portArray[] = new int[10];
    public synchronized void setData(int portt){
        if(stop)
        {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Port.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        stop=true;
        Arrays.fill(portArray, n);
        for(int x=0;x<10;x++)
        {
            if((portArray[x]==n)&&(bool==true))
            {
                 this.portArray[x]=portt;
                 bool = false;             
            }
        }
        System.out.println(this.portArray[0]);
        notify();                                  
        }
 
    public synchronized int[] getData(){        
      if(!stop)
      {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Port.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
      stop=false;
      notify();
      return portArray; 
    }
}
class Adrress {
 
    private boolean stop=false;
    private boolean bool=true;
    private int i;
    byte[] ip={Network.ip(100),Network.ip(100),Network.ip(100),Network.ip(100)};    
 
    InetAddress addressAray[]=new InetAddress[10];
 
    public synchronized void setData(InetAddress address){
        try {
            InetAddress plnAddress = InetAddress.getByAddress(ip);
            if (stop) {
                try {
                    wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            stop = true;
            Arrays.fill(addressAray, plnAdres);
            for (i = 0; i < 10; i++) {
                if ((addressAray[i] == plnAddress) && (bool)) {
                    this.addressAray[i] = address;
                    bool = false;
                }
            }
            System.out.println(this.addressArray[0]);
            notify();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
 
 
    public synchronized InetAddress[] getData(){
        if(!stop)
        {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Address.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        stop=false;
        notify();
        return addressAray;
    }
}
 
class Forwarding {
    private boolean stop=false;
    byte[] send = new byte[256];
    private int i;
    public synchronized void setData(byte send[]){
        if(stop)
        {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Forwarding.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        stop=true;
        this.send=send;        
        notify();
        }
 
    public synchronized byte[] getData(){
       if(!stop)
       {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Forwarding.class.getName()).log(Level.SEVERE, null, ex);
            }
       }
       stop=false;
       notify();
       return send;
    }
}
 
 
 
 
class Network {
public static byte ip(int i) {
return (byte) ((i > 127) ? i - 256 : i);
}
}
and client:
Java Code:
package nklient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lolek
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            //byte[] ip = {Network.ip(172), Network.ip(0), Network.ip(0), Network.ip(1)};
            //InetAddress adresa = InetAddress.getByAddress(ip);
            
            String hostname = "localhost";
            InetAddress ia = InetAddress.getByName(hostname);
            
            SendThread send = new SendThread(ia, 5000);
            send.start();
           
            ReciveThread recive = new ReciveThread(send.getSocket());
            recive.start();
        } catch (UnknownHostException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
class SendThread extends Thread
{
    private InetAddress server;
    private DatagramSocket client;
    private int port;
    private byte addPacket[]=new byte[3];
    private String test="add";
    public SendThread(InetAddress address,int port)
    {
        try {
            this.server = address;
            this.port = port;
            this.client = new DatagramSocket();
            this.client.connect(address, port);
            
        } catch (SocketException ex) {
            Logger.getLogger(SendThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public DatagramSocket getSocket()
    {
        return this.client;
    }
    @Override
    public void run()
    {
        try {
            addPacket = test.getBytes();
            DatagramPacket testPacket = new DatagramPacket(addPacket, addPacket.length, server, port);
            client.send(testPacket);
            BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                try {

                    String reading = cteni.readLine();
                    if (reading.equals("/q")) {
                        break;
                    }
                    byte[] data = reading.getBytes();
                    DatagramPacket odesilanaData = new DatagramPacket(data, data.length, server, port);
                    client.send(odesilanaData);
                } catch (IOException ex) {
                    Logger.getLogger(SendThread.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(SendThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

class  ReciveThread extends Thread
{
    DatagramSocket client;
    
    
    public  ReciveThread (DatagramSocket pomA)
    {
        this.client=pomA;
    }
   
    @Override
    public void run()
    {
        byte[] reciveData = new byte[256];
        while (true)
        {
            try {

                DatagramPacket reciving = new DatagramPacket(reciveData, reciveData.length);
                client.receive(reciving);
                String here = new String(reciving.getData(), 0, reciving.getLength());
                System.out.println(here);
            } catch (IOException ex) {
                Logger.getLogger(ReciveThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


class Network {
public static byte ip(int i) {
return (byte) ((i > 127) ? i - 256 : i);
}
}