Simple UDP Network Programming

Source: Internet
Author: User

It is original, but it is indeed because I have read the video from Teacher Zhang Xiaoxiang. But it is indeed made by me independently. It is shameful to steal others' code, however, I have benefited a lot from the help of others. Well, let's get started.


There are two types of Socket network programming: tcp, udp, and most people may listen to a lot of tcp and use a lot, but they do not know much about udp. So let me explain, udpuser datasync protocol) is translated as the User datagram protocol.

What I want to talk about is the difference between tcp and udp. tcp needs to write a server class first, and then a client class. In the server class, a new ServerSocket is written, and then accept () is used () method to establish a connection with the connected Socket. However, udp does not establish a connection. udp forgets to send a mongorampacket to its destination. No matter whether you receive it or not, it completes the task after it is sent. For udp programming, you need to write a sending class, write a receiving class, and run the receiving class before running. Otherwise, the sending class will end after sending, you have been receiving it, so it is unnecessary.


After talking so much about it, everyone must be bored. I don't know how to write Nima code. I 've talked a bunch of nonsense. Well, let's say how to write code directly, thank you for your attention .)


UdpSend:

Public class UdpSend {

Public static void main (String [] args ){

Try {

DatagramSocket ds = new DatagramSocket ();

/* Why not add a port number to the above line? This is a big header.


When you create a new initramsocket object without a parameter, the system will default a unused port number.

In this way, you don't have to worry about using the system's port number. Of course, it's not your fault if you give it to him.


Here is a classic example: Do you need to know your phone number when you call someone else?

Maybe someone is not very clear, it doesn't matter, because I didn't understand it at the time. Let's take a look at the following code.

*/

DatagramPacket dp = new DatagramPacket ("ni hao! ". GetBytes ()," ni hao! ". Length (), InetAddress. getByName (" localhost "), 3333 );

/*

Here is another example: Do you want to know someone else's phone number when you call someone else?

Here, the phone number is represented in the code by the IP address and port number.

*/

Ds. send (dp );

Ds. close ();

} Catch (SocketException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (UnknownHostException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

}


UdpReceive:


Public class UdpReceive {

Public static void main (String [] args ){

Try {

DatagramSocket ds = new DatagramSocket (3333 );

/*

Another example: If you want someone to call you, do you want to tell someone about your phone number? Otherwise, how can someone call you?

*/

Byte [] buf = new byte [1, 1024];

DatagramPacket dp = new DatagramPacket (buf, buf. length );

/*

In udpSend, The initrampacket contains four parameters. Why is there only two parameters?

For example, when you answer the phone, you need to know the phone number of the other party? No, you don't have to know. You can answer this question. If you don't answer this question, let's just stop talking about it. What we're talking about now is, can you get it? Yes.


In the code, it means that no matter where you send the message, I don't need to know your IP address, your port, and I can connect to it, as for the programmer, it is up to him to use this message.

*/

Ds. receive (dp );

Ds. close ();

System. out. println (new String (buf, 0, dp. getLength ()));

} Catch (SocketException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

}


Explained the four lines of the most critical code. As long as you can understand these four lines, you will be able to understand udp programming. Of course, this is just an introduction, find more examples online if you want to be proficient.


I hope to help you.


Oh, by the way, is the English string transmitted above? What if the Chinese character is transmitted?


Yes, no error will be reported, but it cannot be transmitted correctly. Why ??


Because the Chinese character is two bytes, you cannot use the string length to represent the length of the array ,.. Do you understand Mu you? If you don't have one, you can ask me. I am very happy to answer this question. Although I am a cainiao, don't abandon me...


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.