9 Replies Latest reply: Apr 13, 2008 8:49 PM by EJP RSS

    help me to write a UDP server

    807591
      Hi , can someone help me to solve the following problem:

      1) I wrote two different client and server program, (Using UDP)
      2) In the server side for testig I wrote some code like when a client sending message to server
      a) server find on the database only senders IP address then server return the message to sender
      b) Again if server find out other IP address too then it returns to them too
      3) but it working for (a) , but not for (b)
      4) Actually, its sending message but client not getting it

      So, can some tell me, how to modify server or client side

      I am giveing a pice of coding of server side

      }
                          else
                          {


                               IPAddressObject addressObjectt;

                               onlineUserList.reset();

                               for(int i=0; i<onlineUserList.lengthIs(); i++)
                               {
                                    addressObjectt=(IPAddressObject)onlineUserList.getNextItem();
                                    IPAddress=addressObjectt.getAddress();
                                    System.out.println("\n this is not same : "+IPAddress);


                                    if(addressObject.compareTo(addressObjectt)!=0)
                                    {
                                         System.out.println("\n this is not same : "+sentence);

                                         String cSentence=sentence.toUpperCase();

                                         sendData=cSentence.getBytes();

                                         DatagramPacket sendPacket= new DatagramPacket(sendData, sendData.length, IPAddress,port);
                                         System.out.println("\n aaaaaaa : "+IPAddress);

                                         client.send(sendPacket);

                                    }
                                    else
                                    {
                                         System.out.println("\n this is same : "+sentence);
                                         String cSentence= sentence.toUpperCase();

                                         sendData=cSentence.getBytes();

                                         DatagramPacket sendPacket= new DatagramPacket(sendData, sendData.length, IPAddress,port);
                                         client.send(sendPacket);
                                         System.out.println("\n this is same : "+cSentence);

                                    }

                               }//for
                          }//else

      Edited by: kidylimp on Apr 8, 2008 2:27 AM

      Edited by: kidylimp on Apr 8, 2008 3:10 AM
        • 1. Re: help me to write a UDP server
          EJP
          I know a fair bit about UDP programming but I can't understand a word of that.

          The incoming datagram contains the address of the sender. The simplest thing is to set the reply data in the same datagram and send it. Then you don't have to worry about the sender's address at all.

          If that's not your question, what is it?
          • 2. Re: help me to write a UDP server
            807591
            Actually, I m trying to create a messanger, thats's y server needs to keep IP address of all senders...

            Server wont send to the same datagram, server will send the message to all clients who are online that time ..

            I hope it is clear now, if its not, let me know, I will explain agagin.
            Thanks, Atleast, one person respond to my question.

            Edited by: kidylimp on Apr 8, 2008 5:27 AM
            • 3. Re: help me to write a UDP server
              EJP
              Server wont send to the same datagram
              This is meaningless. You can't send to a datagram.
              I hope it is clear now, if its not, let me know, I will explain again.
              Try again.

              We still don't know what exactly is your problem?
              • 4. Re: help me to write a UDP server
                807591
                Server wont reply the msg to the same IP address/ Sender who sent the message.

                Server will reply the message to other person..

                For example, Alace, Bob and Carrry are three clients. Alace, Bob, and Carry are online now. When they become online server save there IP address.

                Now, Bob sends a message to server , then server will send the message to Alace and Carry. Not to the sender Bob.

                Thanks

                Edited by: kidylimp on Apr 8, 2008 5:43 AM
                • 5. Re: help me to write a UDP server
                  EJP
                  Server wont reply the msg to the same IP address/ Sender who sent the message.
                  Is that the requirement or the bug?

                  If it's a bug, it means you're sending to the wrong address, which is just a simple data structure problem, nothing to do with UDP really. Check your data structures.
                  • 6. Re: help me to write a UDP server
                    807591
                    Its not a bug, its the requierement, I dont know how to write the code that the server will send a message which came from Bob(the example as I gave b4) to everybody who are online.

                    Please reply ass soon as possible.

                    Thanks ejp
                    • 7. Re: help me to write a UDP server
                      EJP
                      Your code loops over all the clients' IP address and checks whether the source address matches.

                      If it doesn't, you send the sentence to the client.

                      If it does, you also send the sentence to the client.

                      If you don't want to do that, don't.
                      • 8. Re: help me to write a UDP server
                        807591
                        Sorry ejp, I could not make you understand . Actually, you didnot understand..

                        When server sending the message to the source client, then source client is getting the message.

                        But, when server sending the message to other clients then other client not getting the message.

                        Thank you,
                        • 9. Re: help me to write a UDP server
                          EJP
                          I agree. You could not make me understand. That's because it took you five attempts to explain yourself clearly.

                          If your other clients aren't receiving the messages, either you are sending to the wrong addresses, which would be a problem with the data structure you're looping over & how it came into existence, or there is something wrong with the receiving code, or the UDP packets are being lost.