A UDP Network Programming1. For non-connected data transmission, unreliable, but high efficiency (audio, video, etc.).2.UDP data sent at once cannot exceed 64kb.Classes required for 3.UDP programming(1) Datagramsocket This class represents the socket used to send and receive datagram packets(2) Datagrampacket This class represents a datagram packetMethod: Datagra
I have heard that using UDP to penetrate NAT can solve the problem of direct communication between two clients that access the Internet through NAT in P2P software. Of course, an intermediary is needed to help find the other party. Finally, I did this experiment using Java.
Paste the code.
UDPAgent. java:
Import java
of the array as a token to determine if the file has been read or not (lenReceiving Party:Package Cn.hncu.url.udp;import Java.net.datagrampacket;import Java.net.datagramsocket;import java.net.InetAddress; Import Java.net.socketexception;public class Receivedemo {public static void main (string[] args) {try {//receive1 ();// Receive small Data receive2 ();//Receive large data} catch (Exception e) {e.printstacktrace ();}} private static void Receive2 () throws Exception {//for receiving larger Pa
Java encapsulates a large number of socket APIs, which provide great convenience for writing network communication programs.In the study of computer network, we have mastered the basic principle of tcp/udp, here is not to repeat. The source code for the receiver and the sender is given for discussion and criticism.The sending end code is as follows:You can send data as soon as you have a slight improvementI
This morning gives you a brief introduction of TCP network communication knowledge, now for everyone to complement the knowledge of complete network programming, about UDP communication knowledge.UDP is an unreliable network protocol, so what is the use value or necessity? In some cases, the UDP protocol may become very useful. Because UDP has the speed advantage
Using the UDP protocol, write a command-line-based chat software, and the client and the server can send data to each other after the command line is started, respectively.First, create Threads Sendthread and Receivethread 1 PackageCom.fhcq.chat;2 3 //Duplex Chat program4 //Can send data, but also can wait to receive data5 //need to use multithreading to implement6 7 Public classQicqdemo {8 9 Public Static voidMain (string[] args) {Ten
port number with a unique identity on the network can be combined to form a uniquely identifiable identifier socket. socket principle mechanism:? There are sockets on both ends of the communication. ? network communication is actually the communication between sockets. ? data is transmitted via IO between two sockets. UDP Transport
Datagramsocket and Datagrampacket
establish the sending end, the receiving end.
set up the data packag
Because to do the application of the UDP protocol interaction, so it is specifically learning the next, in fact, similar to the Java server and socket, the following is written a simple demoService side:Package Com.test1;import java.io.ioexception;import java.net.datagrampacket;import java.net.DatagramSocket; Import Java.net.inetsocketaddress;import java.net.SocketException; Public classUpdserver {Private b
(Dp.getdata (),0,dp.getlength ());
System.out.println (ip+":" +data);
}
} catch (Exception e) {
throw New RuntimeException ("receiver failed");
}
}
}
Public class Cudpchat {
public static void Main (string[] args) throws exception{
//Send side can not specify a port, the system randomly assigns a
Datagramsocket sendsocket = new Datagramsocket ();
Datagramsocket recesocket = new Datagramsocket (5656);
//Turn on multithreading
New Thread (new Send (Sendso
address int servport = 5500; Server Port//Convert The argument String to bytes using the default encoding byte[] Bytestosend = "Hi, worl
D ". GetBytes ();
Datagramsocket socket = new Datagramsocket (); Socket.setsotimeout (TIMEOUT); Maximum Receive blocking time (milliseconds)//sending packet Datagrampacket Sendpacket = new Datagrampacket (
Bytestosend, Bytestosend.length, serveraddress, Servport); Datagrampacket Receivepacket =//Receiving packet new Datagrampacket (new Byte[
[] args) throws IOException { //based on host name To obtain the corresponding inetaddress instance inetaddress ia = inetaddress.getbyname ("192.168.49.50"); //determine if it's up to system.out.println ("oneedu is reachable" +ia.isreachable); // Gets the IP string for the inetaddress instance system.out.println (ia.gethostaddress ()); //Gets the host name of the IP string for the inetaddress instance system.out.println ( Ia.gethostname ()); //Gets the fully qualified domain name
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
Application scenario: A school, whenever the time is over to provide tips for the class function.
Analysis: The use of UDP to achieve broadcast to the same network segment of the computer to send data, the client received the data, Pop-up prompts dialog box. The service side needs a loop to determine whether the time is over, if you use the dead loop here, very much of the system resources, I tested the cup in 100%, with
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.