The Java Learning Network programming UDP Chapter

Source: Internet
Author: User

Datagramsocket
A summary of the construction methods, with a total of 5 construction methods
Datagramsocket ()
Constructs a datagram socket and binds it to any available port on the local host.

Datagramsocket (Datagramsocketimpl impl)
Creates an unbound datagram socket with the specified Datagramsocketimpl.

Datagramsocket (int port)
Creates a datagram socket and binds it to the specified port on the local host.

Datagramsocket (int port, inetaddress laddr)
Creates a datagram socket that binds it to the specified local address.

Datagramsocket (socketaddress bindaddr)
Creates a datagram socket that binds it to the specified local socket address.

The usual method is
Close ()
Close this datagram socket.

Receive (Datagrampacket p)
Receives a datagram packet from this socket.

Send (Datagrampacket p)
Sends a datagram packet from this socket.

When receiving and sending, it involves class datagrampacket,
Datagrampacket has 6 construction methods, of which two are commonly used to construct a

Datagrampacket (byte[] buf, int length)
Constructs a datagrampacket to receive packets of length.

Datagrampacket (byte[] buf, int length, inetaddress address, int port)
Constructs a datagram package that is used to send a package length to a specified port number on a specified host.

Based on the basic mode of UDP communication, the simplest mode, using the following steps
1. Package the data (Datagrampacket) and send it out
2. Receive the sent packets (Datagrampacket) from someone else and view the packets

Steps when sending a packet
(1). Create a socket using the Datagramsocket () method
(2). Use Datagrampacket (byte[] buf, int length, inetaddress address, int port) to package the data to be sent
(3). Send a packet using the Datagramsocket class's Send (Datagrampacket p)

Steps when a packet is received
(1). Use the datagramsocket (int port) method to create a socket, where port indicates that the socket is bound directly to the specified port
(2). Use Datagrampacket (byte[] buf, int length) method to create a packet to be used to receive the packet
(3). Receive data for a packet using the receive (Datagrampacket p) method of the Datagrampacket class

Note: Datagrampacket and Datagramsocket are located in the java.net package, so remember to use it first
Import this package, and in the process of connection creation, there will be a variety of exceptions, remember to do exception handling

Here is a practice code where these two classes receive and send messages to each other

The code for one of the classes

//Import java.net.*; Public classUDPA { Public Static voidMain (string[] args) throws Exception {//steps when sending a packet//(1). Create a socket using the Datagramsocket () method//(2). Using Datagrampacket (byte[] buf, int length, inetaddress address, int//port) to package the data to be sent//(3). Send a packet using the Datagramsocket class's Send (Datagrampacket p)Datagramsocket socket =NewDatagramsocket ();//This can be automatically assigned by the system without the binding port        byte[] buf ="Hello, I'm UDPA, nice to meet you.". GetBytes (); //We send the packets to the IP and port of the IP and 10000 ports, respectively.Datagrampacket Packetsend =NewDatagrampacket (buf, Buf.length, Inetaddress.getbyname ("127.0.0.1"),10000);        Socket.send (Packetsend); //Receive DataDatagrampacket Packetback =NewDatagrampacket (New byte[1024x768],1024x768);        Socket.receive (Packetback); System. out. println ("the information sent by the other party is:"+ (NewString (Packetback.getdata (),0, Packetback.getlength ())));    Socket.close (); }}

This is the code for another class.

//Import java.net.*; Public classUDPB {/** * UDPB receive data first, and then send back the data connection creates a variety of exceptions, remember to do exception handling * * @param args * @throws Exception*/     Public Static voidMain (string[] args) throws Exception {//steps when receiving a packet//(1). Use the datagramsocket (int port) method to create a socket, where port indicates that the socket is bound directly to the specified port//(2). Use Datagrampacket (byte[] buf, int length) method to create a packet to be used to receive the packet//(3). Receive data for a packet using the receive (Datagrampacket p) method of the Datagrampacket classDatagramsocket socket =NewDatagramsocket (10000);//the port bound UDPB is 10000System. out. println ("UDPB's Open."); Datagrampacket Packetrecevice=NewDatagrampacket (New byte[1024x768],1024x768);        Socket.receive (Packetrecevice); String IP= Packetrecevice.getaddress (). gethostaddress ();//get the sender's IP        intPort = Packetrecevice.getport ();//get the port of the senderString data =NewString (Packetrecevice.getdata (),0, Packetrecevice.getlength ());//get the data sent overSystem. out. println ("the data received are:"+ Data +"where the sender's IP is:"+ IP +"ports are:"+ port);//print the Received data//and then send a message to each other .        byte[] msg ="Hello, I am UDPB oh". GetBytes (); //based on the IP and port of each other, we return the information .Datagrampacket Packetback =Newdatagrampacket (msg, msg.length, Inetaddress.getbyname (IP), port);        Socket.send (Packetback); //Last Close socketSocket.close (); }}

Well, while reading a book while watching the video, but also to practice the code, the progress is very slow AH.

The Java Learning Network programming UDP Chapter

Related Article

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.