Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java programming in network (UDP)

Posted on 2004-10-21
5
Medium Priority
?
199 Views
Last Modified: 2010-03-31
DatagramSocket socket = new DatagramSocket(2001);
DatagramPacket packet = new DatagramPacket(buf, buf.length,add,80);

can both of these port number be different?
0
Comment
Question by:sraveend
5 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 12377652
yes (and they would connect to different ports)
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 200 total points
ID: 12378236
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.
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12383586
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
receivingSocket.receive(dp);

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.
0
 

Author Comment

by:sraveend
ID: 12384538
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.
0
 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 600 total points
ID: 12384709
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....
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question