3

i just designed an application in Java to enable chat between multiple clients using one server. I used UDP sockets and multithreading. I had some questions about that:

Client side code:

 private void sendMessage(String s) throws Exception  
    {
        byte b[] = s.getBytes();
        InetAddress address = InetAddress.getLocalHost();
        DatagramPacket packet = new DatagramPacket(b, b.length, address, PORT);
        socket.send(packet);
    }

according to answer over here : (I hope i didnt misunderstand it) the getLocalHost() method should return the localhost loopback address. It however returns 192.xxx.xx.xx because I am connected to a network. Am I correct ?

My server side code to handle the sent packet is:

 byte[] b = new byte[1024];
    while (true) 
    {
        try 
        {
            Arrays.fill(b, (byte)0);
            DatagramPacket packet = new DatagramPacket(b, b.length);
            socket.receive(packet);

            String content = new String(b, 0 , b.length);

            InetAddress clientAddress = packet.getAddress();
            int clientPort = packet.getPort();
            String id = clientAddress.toString() + ":" + clientPort;

when I print id, it gives me : /192.168.56.1:64372 : GREETINGS. But the port should be the one i sent the packet from the client side right ? If not, what am I doing wrong ? And finally, after some packets are sent, I get an exception : java.lang.StringIndexOutOfBoundsException: String index out of range: 1046 Is it because of the 1024 size byte array ? Thanks in advance :]

Community
  • 1
  • 1
previouslyactualname
  • 673
  • 1
  • 10
  • 23
  • What Address did you expect for localhost? A 192.168... address sounds reasonable for a machine in a home network. You can ping localhost on a console to see which address it is resolved to. Should be the same as the one returned by `getLocalHost()` – Fildor Sep 17 '13 at 06:41
  • I expected 192.xx as well, but I got confused when I got off a network and i still got a 192.xx address. Later used ipconfig and found my ipv4 addr to be the same. So I think that clears that a bit, but I still did not get how people get /127.0.0.1. – previouslyactualname Sep 17 '13 at 06:49
  • Can you show us the complete stacktrace? And what was the payload? Is it in fact >1024 bytes? DO you get the same error with different payload of same and/or different lengths? – Fildor Sep 17 '13 at 07:49
  • That's precisely the thing, the error shows up unannounced. I cannot post the stack-trace now, will do when I am free. Sometimes I'll get it after I send one packet and sometimes after 6 or 7 packets. And I only used s.getBytes() where s.length() was below 10. – previouslyactualname Sep 17 '13 at 08:05
  • I just create a simple client/server udp example: http://stackoverflow.com/a/42450123/2619908 – nono Feb 25 '17 at 03:44

1 Answers1

0

Ok you've got the Following Problems 1. The Receive Port is different 2. You get an IndexOutOfBounds Exception

To the first, I guess java is just reasigning that or your os, but anyway you receive it correctly!

To the second. You don't really know where the DatagramSocket stores your data! It might be anywere in your array, for that the packet safes the offset of your data and the length:

DatagramPacket p;
p.getLength()
p.getOffset()
p.getData()

So do it like this.

byte[] b = new byte[1024];
while (true) 
{
   try 
   {
      // This is not needed! Just check how much data you get
      // Arrays.fill(b, (byte)0);
      DatagramPacket packet = new DatagramPacket(b, b.length);
      socket.receive(packet);

      // This is the easiest
      String content = new String(packet.getData());

Still this is a bit strange because you already set all data to 0 and your not reading in more than 1024 = b.length bytes. But for data integrity defenitly use getData()!!!

mjb4
  • 939
  • 1
  • 7
  • 14