Results 1 to 3 of 3
  1. #1
    Koren3 is offline Member
    Join Date
    Apr 2009
    Posts
    50
    Rep Power
    0

    Post [SOLVED] UDP chat client server

    Hallo, I have a problem, I donīt know how can i do thread "PreposilaciVlakno". This thread should be send data for another client. My thread sent this data but it is very ineffective becouse Thread is sending same data while server recive another data. How can i solve this stuff? I am sory for my English. Here is my code:
    Java Code:
    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;
        }
    }

  2. #2
    rexicon is offline Member
    Join Date
    Apr 2009
    Posts
    10
    Rep Power
    0

    Default

    Worked for me

  3. #3
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Not really helpful rexicon
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. Chat Client
    By Jessi34 in forum New To Java
    Replies: 5
    Last Post: 12-09-2009, 04:49 PM
  2. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 01:22 AM
  3. Multi-user chat server and client
    By 435.mahesh in forum Java Software
    Replies: 6
    Last Post: 04-25-2009, 01:45 AM
  4. Multi Client/Server Chat Question
    By Kodak07 in forum Networking
    Replies: 3
    Last Post: 03-29-2009, 11:50 PM
  5. Replies: 2
    Last Post: 12-27-2008, 09:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •