Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

DatagramSocket! I am getting  IllegalArgumentException but can't figure out why.

Thread view: 
aamircheema@gmail.com - 09 Aug 2006 12:34 GMT
Dear All,

I am new to Socket Programming. I am trying to send data using UDP but
I get the IllegalArgument Exception but I can't find why I am getting
this exception.
The program below is implemented on server.Which recieves the packet
from the Client and then reads its port number and address and sends
the DATE back to the Client.I am using getBytes() method to convert
data to bytes[] and this line gives the error. Whenever I comment this
line there is no Exception and Client recieves an empty message. Can
anybody figure out where is the problem.

            int length=1024;
        byte[] buf=new byte[length];
        byte[] data= new byte[length];
        int visit=1;
        try {
                DatagramSocket listener = new DatagramSocket(port);
                //Socket serverSocketAccepted;
                DatagramPacket packet= new DatagramPacket(buf,length);
                while(true)
                {
                    listener.receive(packet);
                    InetAddress clientAddr = packet.getAddress();
                    int clientPort = packet.getPort();

// Here I will read the data

                                        long time =
System.currentTimeMillis();
                    Date d= new Date(time);
                    String date=d.toString();
-------------->             data=date.getBytes();     //  When this line is
absent there is no Exception

                    DatagramPacket packetSend= new
DatagramPacket(data,length,clientAddr,clientPort);
                   
       
                    listener.send(packetSend);
        }

Thanks,
Aamir
Gordon Beaton - 09 Aug 2006 12:48 GMT
> I am new to Socket Programming. I am trying to send data using UDP
> but I get the IllegalArgument Exception but I can't find why I am
> getting this exception.

Always post the exact text of the exception, and mark the line number
it indicates!

I believe that your exception is because you attempt to send "length"
bytes of data, but the data array is shorter than you expect.

Realize that "data = date.getBytes();" does not initialize the array
contents, it *replaces* the array altogether. The new array is likely
much shorter than the original one in this case.

The solution here is to specify data.length() when you create the
DatagramPacket from data.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

aamircheema@gmail.com - 09 Aug 2006 23:53 GMT
It works! Thanks a lot Gordon

> > I am new to Socket Programming. I am trying to send data using UDP
> > but I get the IllegalArgument Exception but I can't find why I am
[quoted text clipped - 18 lines]
> [ don't email me support questions or followups ]
> g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.