Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: UDP server sends thread application

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default UDP server sends thread application

    Hi, How can I do thread VlaknoPreposilaci?
    package nserver;
     
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    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();
                Adresa adresa = new Adresa();
                Preposilani prepos = new Preposilani();
                PreposilaniData pd = new PreposilaniData();
     
                VlaknoPrijimaci prijem = new VlaknoPrijimaci(5000, port, adresa, prepos,pd);
                VlaknoPreposilaci odesli = new VlaknoPreposilaci(port, adresa, prepos,prijem.server,pd);
     
     
         }    
      }
     
    class VlaknoPrijimaci implements Runnable
    {   
     
        private Port p;
        private Adresa a;
        private int getPort;
        private InetAddress getAddress;
        private Preposilani prepos; 
        private byte odeslana[] = new byte[256];
        private byte odeslanaData[] = new byte[256];
        private byte prijata[] = new byte[256];
        private PreposilaniData pd;
     
        DatagramSocket server;
     
     
        int port;   
        VlaknoPrijimaci(int port,Port p,Adresa a,Preposilani prepos, PreposilaniData pd)
        {   
            try {
                this.pd = pd;
                this.prepos = prepos;
                this.a = a;
                this.p = p;
                this.server = new DatagramSocket(port);
                new Thread(this, "VlaknoPrijimaci").start();
            } catch (SocketException ex) {
                Logger.getLogger(VlaknoPrijimaci.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        public DatagramSocket getSocket()
        {
            return this.server;
        }
        @Override
        public void run ()
        {    
            try 
            {            
                while(true)
                {
     
                 DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length);
                 server.receive(prijatyPaket);
     
                 String prijaty = new String(prijatyPaket.getData(),0,prijatyPaket.getLength());
                 System.out.println("Od  klienta:"+prijaty);              
     
     
     
                 if (prijaty.equals("add"))
                 {
                    getPort = prijatyPaket.getPort();            
                    p.setData(getPort);               
     
     
                    getAddress = prijatyPaket.getAddress();
                    a.setData(getAddress);                                
                 }
                 else 
                 {  
     
                    odeslana = prijaty.getBytes();
                    prepos.setData(odeslana); 
                    pd.setData(prijatyPaket);
     
                 }
     
                }            
            } 
            catch (IOException ex) 
            {
                Logger.getLogger(VlaknoPrijimaci.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
    }
     
     
    class VlaknoPreposilaci implements Runnable
    {
     private Preposilani prepos;
     private InetAddress klient;
     private int port;
     private DatagramSocket server;
     private Port p;
     private Adresa a;
     private int olo=0;
     private PreposilaniData pd;
     private int getPort;
     private InetAddress getAddress;
     
     private Integer port1;
     private Integer port2;
     
     private InetAddress adresa1;
     private InetAddress adresa2;
     
     private boolean bool = true;
     
     Thread preposilaci;
     VlaknoPreposilaci(Port p,Adresa a,Preposilani prepos,DatagramSocket pomA,PreposilaniData pd)
     {
                this.pd = pd;
                this.prepos = prepos;
                this.p = p;
                this.a = a;
                this.server = pomA;         
                new Thread(this, "VlaknoPreposilaci").start();       
     }
     
        @Override
     public void run()
     {  
            try {
                //Thread.sleep(10000);
                System.out.println(p.getData().get(0));
                System.out.println(a.getData().get(0));
     
                System.out.println(p.getData().get(1));
                System.out.println(a.getData().get(1));
     
                port1 = (Integer) p.getData().get(0);
                port2 = (Integer) p.getData().get(1);
     
                adresa1 = (InetAddress) a.getData().get(0);
                adresa2 = (InetAddress) a.getData().get(1);
     
              //  while(true){
     
                getPort = pd.getData().getPort();
                getAddress = pd.getData().getAddress();
     
                    if (port1 == getPort) {
                        DatagramPacket odeslat1 = new DatagramPacket(prepos.getData(),prepos.getData().length,adresa1,port1);
                        server.send(odeslat1);
                        System.out.println("port1");
                    } else if (port2 == getPort) {
                        DatagramPacket odeslat2 = new DatagramPacket(prepos.getData(),prepos.getData().length,adresa2,port2);
                        server.send(odeslat2);
                        System.out.println("port2");
                    }
     
     
               // }
            } catch (IOException ex) {
                Logger.getLogger(VlaknoPreposilaci.class.getName()).log(Level.SEVERE, null, ex);
            }// catch (InterruptedException ex) {
              //  Logger.getLogger(VlaknoPreposilaci.class.getName()).log(Level.SEVERE, null, ex);
           // }
     
     
     }
     
    }
     
     
    class Port {
        List<Integer> listPort = Collections.synchronizedList(new ArrayList<Integer>(2)); 
     
        public synchronized void setData(int portt){
            listPort.add(portt);                                                     
        }
     
        public synchronized List getData(){        
          return this.listPort; 
        }
    }
    class Adresa {       
        private List<InetAddress> listAddress = Collections.synchronizedList(new ArrayList<InetAddress>(2));
        public synchronized void setData(InetAddress adresa){
                listAddress.add(adresa);
            }
     
        public synchronized List getData(){
            return this.listAddress;
        }
    }
     
    class Preposilani {
        private boolean obsazeno=false;
        byte[] odeslat = new byte[256];
        private int i;
        public synchronized void setData(byte odeslat[]){
            this.odeslat=odeslat;        
        }
     
        public synchronized byte[] getData(){
           return this.odeslat;
        }
    }
     
    class PreposilaniData {
        private byte[] odeslat = new byte[256];
     
        private DatagramPacket preposilanyPacket = new DatagramPacket(odeslat,odeslat.length);
        public synchronized void setData(DatagramPacket kOdeslani){
           this.preposilanyPacket=kOdeslani;        
        }
     
        public synchronized DatagramPacket getData(){
           return this.preposilanyPacket;
        }
    }
     
    class Shromazdovac
    {
     
    }
    Thanks a lot for your answer.

  2. The Following User Says Thank You to Koren3 For This Useful Post:



  3. #2

    Default Re: udp server send thread

    Did you get an exception? Can you give us something more specific?

  4. #3
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default Re: udp server send thread

    Never mind. I will solve another way. But thank for your interest.

Similar Threads

  1. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  2. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM
  3. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM
  4. Important: Marking your Thread as SOLVED
    By JavaPF in forum Forum Updates & Feedback
    Replies: 0
    Last Post: December 5th, 2008, 09:33 AM