A Preliminary Study on the Relationship Between mongorampacket and mongoramsocket in Java

Source: Internet
Author: User

1. Basic concepts:

A. zoorampacket and initramsocket are located in the java.net package.

B. datagrampacket indicates the data datagram that stores the data, and datagramsocket indicates the socket that accepts or sends the data datagram.

C.The network link composed of the two classes is based on the UDP protocol and is an unreliable protocol.

The reason for the reliability is that the sender is not responsible for whether the data is successfully sent. When the receiver receives the data, it does not report the successful message to the sender, which may lead to information loss.

However, this protocol is quick. For example, a CS (half-lived) game is based on the UDP protocol. Otherwise, it is estimated that the server will be crashed after a game is played, so we can see that the game sometimes loses frames.

2. Usage:

To establish a UDP-based link, we should first establish a socket <datagramsocket> (that is, the sending or receiving station), and then send or accept data through the socket <datagrampacket>.

Let's take a look at the initramsocket and initrampacket classes.

Datagramsocket class:

Constructor:

Datagramsocket ():

Creates a default socket and binds it to a local address and a random port number.

Datagramsocket (INT port ):

Different from the above, it is bound to a specific port number, and others remain unchanged.

Datagramsocket (INT port, inetaddress IAD ):

Creates a socket and binds it to a specified port number and address.

Datagramsocket (socketaddress sad );

Creates a socket and binds it to a specific socket address.

Tip: we can understand the so-called local address in this way. We can see an IP address through "Control Panel-local connection-support". This is the local IP address, it is also understood as a local address.

Basic method:

Close ():

Disable socket

Recevie (datagrampacket DP ):

Accept Datagram

Send (datagrampacket DP ):

Send Datagram

The datagram is involved here. The following describes the datagram:

Datagrampacket class:

Constructor: (only list the main ones)

Accept type:

Datagrampacket (byte [] Buf, int length ):

It is used to accept the Buf data with the length (that is, the data is stored in the byte array BUF)

Sending type:

Datagrampacket (byte [] Buf, int length, inetaddress address, int port );

Send the long-length Buf data to the port number of the specified address

Datagrampacket (byte [] Buf, int length, socketaddress address );

Send the long-length Buf data to the specified socket address

As we can see above, the datagram constructed when we send data should be of the sending type, while the data report should be accepted.

Start writing code

Code intent:

1. One receiver and one sender

2. After receiving the message from the sender, the receiver prints the message sent, the address and port number of the sender, and then sends a message to the sender, "Recipient: I have received it !"

3. The sender prints the message from the receiver.

For details, refer to the Code:

Import java.net. export rampacket; <br/> Import java.net. datagramsocket; <br/> Import java.net. inetaddress; </P> <p> public class sender {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> // create the sender's socket. The default IP address is local and the port number is random. <br/> random ramsocket sendsocket = new random ramsocket (); </P> <p> // confirm the message to be sent: <br/> string MEs = "Hello! Recipient! "; </P> <p> // because the data in the datagram is stored in the form of a character array, the data is transferred to the data <br/> byte [] Buf = mes. getbytes (); </P> <p> // determine the sender's IP address and port number. The address is the address of the Local Machine. <br/> int Port = 8888; <br/> inetaddress IP = inetaddress. getlocalhost (); </P> <p> // create a send-type datagram: <br/> datagrampacket sendpacket = new datagrampacket (BUF, Buf. length, IP, <br/> port); </P> <p> // send data through a socket: <br/> sendsocket. send (sendpacket); </P> <p> // determine the buffer storage for receiving feedback data, that is, the byte array for storing data <br/> byte [] getbuf = new Byte [1024]; </P> <p> // create a accept-type datagram <br/> datagrampacket getpacket = new datagrampacket (getbuf, getbuf. length); </P> <p> // receives data through a socket <br/> sendsocket. receive (getpacket); </P> <p> // parse the feedback message and print <br/> string backmes = new string (getbuf, 0, getpacket. getlength (); <br/> system. out. println ("message returned by the receiver:" + backmes); </P> <p> // close the socket <br/> sendsocket. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>} </P> <p >}< br/>}</P> <p> Import java.net. export rampacket; <br/> Import java.net. datagramsocket; <br/> Import java.net. inetaddress; <br/> Import java.net. socketaddress; </P> <p> public class receive {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> // confirm the IP address and port number of the receiver. The IP address is the address of the Local Machine. <br/> inetaddress IP = inetaddress. getlocalhost (); <br/> int Port = 8888; </P> <p> // create the socket of the receiver and specify the port number and IP address <br/> datagramsock Et getsocket = new datagramsocket (port, ip ); </P> <p> // determine the array size of the data received by the datagram <br/> byte [] Buf = new byte [1024]; </P> <p> // create a accept-type datagram. The data is stored in Buf. <br/> datagrampacket getpacket = new datagrampacket (BUF, Buf. length); </P> <p> // receives data through a socket <br/> getsocket. receive (getpacket); </P> <p> // parse the message transmitted by the sender and print <br/> string getmes = new string (BUF, 0, getpacket. getlength (); <br/> system. out. println ("message sent by the other Party:" + getmes); </P> <p> // Obtain the sender's IP address and port number through the datagram, and print <br/> inetaddress sendip = getpacket. getaddress (); <br/> int sendport = getpacket. getport (); <br/> system. out. println ("the peer IP address is:" + sendip. gethostaddress (); <br/> system. out. println ("the recipient's port number is:" + sendport); </P> <p> // obtain the sender's socket address through the datagram <br/> socketaddress sendaddress = getpacket. getsocketaddress (); </P> <p> // determine the message content to be fed back to the sender and convert it to a byte array <br/> string feedback = "the receiver says: I have received it! "; <Br/> byte [] backbuf = feedback. getbytes (); </P> <p> // create a send-type datagram <br/> datagrampacket sendpacket = new datagrampacket (backbuf, <br/> backbuf. length, sendaddress); </P> <p> // send data through a socket <br/> getsocket. send (sendpacket); </P> <p> // close the socket <br/> getsocket. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/>

 

Test passed

Start the receiver first, and then start the sender.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.