A
aamircheema
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
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