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

Thread: Problem in UDS client,server program

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

    Default Problem in UDS client,server program

    Hi,
    I have this problem: My program should be to do this, client send datagram to server and server send datagram to another client. But My program send datagram to server server recived but server doesn´t send to another client. My server code:
    try { 
    DatagramSocket server = new DatagramSocket(5000); 
    byte[] poslana = new byte[256]; 
    byte[] prijata = new byte[256]; 
    byte[] preposi = new byte[256]; 
    byte ip[] = { Network.ip(127), Network.ip(0), Network.ip(0), Network.ip(1)}; 
    InetAddress kolo = InetAddress.getByAddress(ip); 
    while (true) 
    { 
    DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length); 
    server.receive(prijatyPaket); 
    String prepos = new String(prijatyPaket.getData()); 
    preposi = prepos.getBytes(); 
    InetAddress adresa = InetAddress.getByAddress(ip); 
    DatagramPacket odeslat = new DatagramPacket(preposi,preposi.length,adresa,5000); 
    server.send(odeslat);
    This should be to do.Client send text and server send back. I am sory my English is not good. I hope you understand me.
    Thank you for your advice.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: UDS server

    I trust that 127.0.0.1 is the address of your router, in which you will need to set up port forwarding etc, so that when it recieves something on port 5000 it will forward it to the required PC on the specified port. I think you will find this is not a Java problem but a technical problem

    Chris

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

    Default Re: UDS server

    I don´t think so. Becouse I work with only localhost. 127.0.0.1=localhost . My router has got IP address 192.168.1.100.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: UDS server

    Good point, What is the code that is receiving the message?

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

    Default Re: UDS server

    This code on side server recive and send:
    DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length); 
    server.receive(prijatyPaket); //recive package
    String prepos = new String(prijatyPaket.getData()); //get data from package 
    preposi = prepos.getBytes(); //assign data from package to preposi
    InetAddress adresa = InetAddress.getByAddress(ip); //IP address client (localhost)
    DatagramPacket odeslat = new DatagramPacket(preposi,preposi.length,adresa,5000);//creating new datagram 
    server.send(odeslat);//send datagram to client
    And this is code client:
    package messenger;
     
    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.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) {
            while(true)
            {
                try {               
                    BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in)); 
                    DatagramSocket klient = new DatagramSocket();
                    try {
                        InetAddress lokalni = InetAddress.getByName("localhost");                 
                        byte[] poslana = new byte[256];
                        byte[] prijata = new byte[256];
                        String k = cteni.readLine();
                        poslana = k.getBytes();
                        DatagramPacket odesilanyPaket = new DatagramPacket(poslana, poslana.length,lokalni,5000);
                        klient.send(odesilanyPaket);
                        DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length);
                        klient.receive(prijatyPaket);
                        String prijataData = new String(prijatyPaket.getData());
                        System.out.println("Od serveru"+prijataData);
     
                    } catch (IOException ex) {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }                            
                } catch (SocketException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
     
     
     
     
     
     
            }
     
     
     
     
        }
     
    }

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: UDS server

    Hey Koren3,

    Could you please post your entire 'server' code for me to take a look at?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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

    Default Re: UDS server

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package msnserver;
     
     
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    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 {
     
                DatagramSocket server = new DatagramSocket(5000);//vytvoreni server soketu ktery nasloucha na portu 5000
                //BufferedReader cteni = new BufferedReader(new InputStreamReader(System.in));////cteni z klavesnice(vytvoreni proudu dat)
                byte[] poslana = new byte[256];//vytvoreni 256 bytoveho pole pro posilana data
                byte[] prijata = new byte[256];//vytvoreni 256 bytoveho pole pro prijata data
                byte[] preposi = new byte[256];            
                byte ip[] = { Network.ip(127), Network.ip(0), Network.ip(0), Network.ip(1)};
               InetAddress kolo = InetAddress.getByAddress(ip);
     
     
     
                //String ukonceni = "/q";
                //String kOdeslani;           
                while (true)                
                {
                DatagramPacket prijatyPaket = new DatagramPacket(prijata,prijata.length);// zaobaleni pole bajtu k prijmu
                server.receive(prijatyPaket);//prijmuti paketu  
                String prepos = new String(prijatyPaket.getData());              
                preposi = prepos.getBytes();            
                InetAddress adresa = InetAddress.getByAddress(ip);
                DatagramPacket odeslat = new DatagramPacket(preposi,preposi.length,adresa,5000);
                server.send(odeslat);                       
                //server.send(prijatyPaket);
                //String prijataData = new String(prijatyPaket.getData());//ziskani dat z prijmuteho paketu a prirazeni je do s          
                //System.out.println("Od klientu:"+prijataData);//vypis prijatych dat
                //InetAddress IPKlienta = prijatyPaket.getAddress();//Ziskani IP adresy klienta a prirazeni do IPKlient
                //int port = prijatyPaket.getPort();//Ziskani portu klienta a prirazeni do port
                //kOdeslani = cteni.readLine();//cteni dat po radcich
                //poslana = kOdeslani.getBytes();//prirazani dat do poslana
                //DatagramPacket odesilanyPaket = new DatagramPacket(poslana,poslana.length,IPKlienta,port);
                //server.send(odesilanyPaket);
                //if (kOdeslani.equals(ukonceni))
                //{
                //    System.exit(0);
                //}
     
                }            
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }    
     
        }
     
    }
    and class network
    package msnserver;
     
    /**
     *
     * @author Lolek
     */
    class Network {
     
    public static byte ip(int i) {
    return (byte) ((i > 127) ? i - 256 : i);
    }
    }

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

    Default Re: UDS server

    Ï solved it. Thank you. Bad client port.

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: UDS server

    Quote Originally Posted by Koren3 View Post
    Ï solved it. Thank you. Bad client port.
    Hey Koren, I was just this second looking at your code.

    Nice one for solving it
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.