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
ActionListener {String msg = null; String line = System.getproperty ("Line.separator");p ublic void actionperformed (ActionEvent e) {try {msg = Messagearea.gettext (); FileOutputStream Note = new FileOutputStream ("Note.txt"); Messagearea.append ("The record has been saved in NOTE.TXT "); Note.write (Msg.getbytes ()); Messagearea.append (line); Note.close ();} catch (IOException E1) {System.out.println ("Send Failed");}}} Clear the chat record on the panel class Clearmsg implements ActionListen
];//prevents the file name word from being too long (here the maximum 256 characters) Datagrampacket Packet_tip = new Datagrampacket (filetipbuf,0, Filetipbuf.length); socket.receive (Packet_tip); String Str_filetip = new String (Packet_tip.getdata (), 0,packet_tip.getlength ()), filename = str_filetip.substring (8); Ta.append ( Str_filetip + "\ n");//Receive file Length (bytes) byte[] Filelenbuf = new byte[12];//capable of transmitting 1T files Datagrampacket Packet_len = new Datagrampac Ket (f
(BUF, 1024); //receive data, put in a datagramDs.receive (DP); //extracting data from a datagramString info =NewString (Dp.getdata (), 0, Dp.getlength ()); System.out.println ("The message Received is:" +info); }}Service side:ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress; Public classUdpserver { Public Static voidMain (string[] args)throwsException {datagramsocket ds=NULL; Datagrampacket DP=NULL; //building a Send objectDS =NewDatagramsocket (); //packag
UDP transportTwo classes:Datagramsocket and Datagrampacket(see API)A: Set up the sending side, the receiving end.B: Set up the data packet.C: Call the socket's send receive method.D: Close the socket.E: The sending and receiving end is two separate running programs.UDP transmission-send-side ideasUDP sends data:A: Create A Socket send-side objectB: Create a Datagram Package (package the data)C: Call the Socket object to send a datagram packetD: Freein
There are two types of communication protocols in an existing network: TCP and UDPTcp
is a protocol designed to provide reliable, end-to-end byte-stream traffic on unreliable internet, a connection-oriented protocol in which TCP connections are byte streams rather than message flows.
The TCP protocol is like making a phone call, and there is a speech and answer after the switch.
Udp
Provides a way for an applicati
Java to obtain the available UDP port number. The TCP method is similar to this one.
Method 1: If you do not mind obtaining the port number range, you can use the assumramsocket constructor to define 0 as its port number. The system will assign it an idle Port Number:
public static DatagramSocket getRandomPort() throws SocketException {DatagramSocket s = new DatagramSocket(0);return s;}
Method 2: If you w
: Udpclient_demopublic class Udpclient_demo {public static void main (string[] args) throws exception{//TODO auto-generated method stub datagramsocket ds = null;//Declaration Datagramsocket Object byte[] buf = new byte[1024];//definition receive data BYTE data datagrampacket DP = null;//Declaration Datagrampacket Object ds = new Datagramsocket (9000);//This client listens on 9000 port DP = new Datagrampacket ( buf,1024)///Specifies the length of the received data is 1024system.out.printl
() + "\ n"); Ta_send.settext ("");} catch (Exception ex) {System.out.println (ex);}}}); /try {ip = inetaddress.getbyname (STR_IP); socket = new Datagramsocket (this.myport); socket1 = new Datagramsocket ( This.myport + 1); Socket2 = new Datagramsocket (This.myport + 2); Timer timer = new timer ();//timer, flush Receive Message timer.schedule (new mytimertask_receive (), 0, +); while (Socket!=null) {try {// byte filetipbuf[] = new BYTe[256];//prevents the file name word from being too long (up t
,bytes.length); - //accepting packets with sockets - recevicesocket.receive (datagrampacket); - //get the IP address of the sending end -inetaddress IP =datagrampacket.getaddress (); in //converts the received message to a string -String rec =NewString (Datagrampacket.getdata ()); toSystem.out.println (ip.gethostaddress () + "message Sent:" +rec); + -}Catch(SocketException e) { the e.printstacktrace (); *}Catch(IOException e) { $ e.printstacktrace
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.