java programming in network (UDP)

DatagramSocket socket = new DatagramSocket(2001);
DatagramPacket packet = new DatagramPacket(buf, buf.length,add,80);

can both of these port number be different?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

yes (and they would connect to different ports)
DatagramSocket socket = new DatagramSocket(2001);
Constructs a datagram socket and binds it to the specified port on the local host machine.

DatagramPacket packet = new DatagramPacket(buf, buf.length,add,80);
Constructs a datagram packet for sending packets of length length to the specified port number on the specified host.

>can both of these port number be different?
Sure! If the host is the same and the port are the same, be carefull!

Bye, Giant.
Yes and at receiving end all U got to do is

Create a datagram socket bound to port 80 using
DatagramSocket receivingSocket = new DatagramSocket(80);

Then create a buffer of some length bigger than the lenth of the datagramPacket U r sending

byte[] buffer = new byte[2000];

Create a datagramPacket with this buffer using
DatagramPacket dp = new DatagramPacket(buffer,buffer.length);

then receive a packet using from DatagramSocket bound to port 80 using

This populate the datagramPacket created above with the data sent to port 80

Now to extract the data use

byte[] dataSent = dp.getData();

dataSent array contains the data sent to this port.
sraveendAuthor Commented:
am confused,why do we've to use port number in datagramsocket as well as in datagrampacket.In TCP/IP  we use only once port number in sockets.
Port number in DatagramSocket is the port on Ur machine using which U will be sending packets to... and Port number in DatagramPacket is the port number on the destination machine.

In your case port number on the sending machine is 2001 so packets sent from this socket will have a source port set to 2001

and Ur datagram packets are received at port 80 on the destination machine.

Here U can use the same datagram socket at 2001 and send packets to destination machines at various ports like 500, 1000(port mentioned in datagramPacket) what ever....

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.