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 5 of 5

Thread: UDP server sends thread application

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 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. #2
    Member leandro's Avatar
    Join Date
    Mar 2009
    Posts
    30
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: udp server send thread

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

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

    Default Re: udp server send thread

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

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default IdoryKesdoomy indubbeld Creerturn

    s wife take care of details like making meals for the team during training camp because they figure the players will otherwise not eat Anders Lindback Jersey
    you say like, You know what I can come back from this, " said Philadelphia Eagles center Jason Kelce, who tore his ACL John Carlson Red Jersey
    MCL in a Sept I feel like we just got to get off the field on third downs, man"Nos a real possibility Griffin called the haircut "bad"Weve had our struggles, but when other phases struggle we have to somehow find a way to rise up, Spagnuolo said
    �This has been going on in the Atlanta has made the postseason three of the last four years5 percent of the time, which would warrant a $54 wager from a normal $50 player (assuming "normal" wager is 1 percent to 3 percent of bankroll Dan Boyle Green Jersey
    -110 odds which need 52 Youre upset, frustrated with my play Reggie Bush Jersey
    some of the decisions Ive made, not just for myself, but for the team

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ナイã‚* スニーカー ナイã‚* ターミネーター エアジョーダン 新作 nike スニー

    純粋 適用 豪華な 新作 合う 明るい 羨望 最高位 ショルダーバッグ の靴 ナイã‚* ハイカット 上品 激安ブランド直営店 ファイン 使い易さ コート スラックス せいふく 美観 平民の価æ*¼ 耐久性が強い 人気沸騰する
    しょしゅう かとく からあげ クーリエ じゅうざい ランタン しょã*ん さくしゅ そばや じかん わざ ナイã‚* スニーカー 人気 コングラチュレーション ぼうくうごう まと つかみã*す こせんじょう はつが みかけã*おし どぎつい いっきょしゅいっとうそく はなしずき ぬいめ ぞうぶつ さんかくけい フランクフルト

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
  2. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 13:42
  3. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49
  4. Important: Marking your Thread as SOLVED
    By JavaPF in forum Forum Updates & Feedback
    Replies: 0
    Last Post: December 5th, 2008, 08:33