Link to home
Start Free TrialLog in
Avatar of gplana
gplanaFlag for Spain

asked on

Problem using datagramSocket in Java

Dear Expert:

I'm developing a little application that use DatagramSocket and DatagramPacket classes in Java. The program takes 3 command line parameters: host, port and message, and sends the message to a server (host) on the port specified. Server reads the message, transform it to uppercase and sends back message to client.

When I execute client (with server running on other session) it raises a run-time error telling length or index is wrong. There are two println: only first println is executed so I know the line is failing but not why. Could you help me ?

I attach the code of the client. I could attach also the code of the server if you need it.

Thank you.
import java.net.*;

public class Client {
	public static void main(String args[]){
		DatagramSocket dg;
		DatagramPacket p;
		InetAddress iaddr;
		int portRemot;
		String host, missatge;
		byte buf [];
		
		buf = new byte[100];
		host = args[0];
		portRemot = Integer.parseInt(args[1]);
		missatge = args[2];
		try {
			dg = new DatagramSocket();
			iaddr =  InetAddress.getByName(null);
			buf = missatge.getBytes();
System.out.println("point 1");
			p = new DatagramPacket(buf, 100, iaddr, portRemot);
System.out.println("point 2");
			dg.send(p);
			dg.receive(p);
			buf = p.getData();
			missatge = new String(buf);
			System.out.println(missatge);
			dg.close();		
		} catch(Exception e) { System.out.println(e.getMessage()); }
	}
}

Open in new window

Avatar of HalfAsleep
HalfAsleep

Could you provide the runtime error message?

Also, your code will be more robust if you check the size of args, maybe not assume that all arguments will be there.

If size of args is less than the minimun required, you could print a "usage" text before exiting.

Also, according to the Java documentation, the length argument must be less than or equal to buf.length.  It seems you are in the clear, but for fun, try to create the buffer with one more byte and see if that was your problem.
Avatar of gplana

ASKER

I call by>
java Client localhost 23000 Hi
 
This is the server code. If you compile both and call Server by
java Servidor 23000
you will see Client print "point 1" but not "point 2".
The runtime error message is> "illegal length or offset"
 
Avatar of gplana

ASKER

Sorry, I forget the server code:

import java.net.*;

public class Client {
    public static void main(String args[]){
        DatagramSocket dg;
        DatagramPacket p;
        InetAddress iaddr;
        int portRemot;
        String host, missatge;
        byte buf [];
       
        buf = new byte[100];
        host = args[0];
        portRemot = Integer.parseInt(args[1]);
        missatge = args[2];
        try {
            dg = new DatagramSocket();
            iaddr =  InetAddress.getByName(null);
            buf = missatge.getBytes();
System.out.println("point 1");
            p = new DatagramPacket(buf, 100, iaddr, portRemot);
System.out.println("point 2");
            dg.send(p);
            dg.receive(p);
            buf = p.getData();
            missatge = new String(buf);
            System.out.println(missatge);
            dg.close();        
        } catch(Exception e) { System.out.println(e.getMessage()); }
    }
}
ASKER CERTIFIED SOLUTION
Avatar of HalfAsleep
HalfAsleep

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gplana

ASKER

Excellent. This was the problem !!!
I tried to put mssatge.length() instead of 100 and it works now. But what I don't understand is why it oesn't work if I put missatge.length()+1. I haven't to send the mark of the end of the string (0 character) ?
And if I put some non-english characters the program doesn't finalize. I suppose because strings in java would be UTF-8 encoded, which encodes non-english characters in 2 bytes. So how can I get the number of bytes instead of the number ofcharacters ?
If you want to know how many bytes the string turns into, you can try this>

int numBytes = myString.getBytes().length;
Avatar of gplana

ASKER

Excellent. Thank you.