search
HomeBackend DevelopmentXML/RSS TutorialJava socket programming (Part 2)(1)

Java socket programming (Part 2)(1)

Dec 23, 2016 pm 04:20 PM
java

Self-addressing sockets (Datagram Sockets)
​, because each connection using a stream socket takes a certain amount of time. To reduce this overhead, the network API provides a second type of socket: self-addressing Socket (datagram socket), self-addressing uses UDP to send addressing information (from client program to service program or from service program to client program). The difference is that multiple IP information packets can be sent through self-addressing sockets. The self-addressing information is contained in the self-addressing packet, and the self-addressing packet is contained in the IP packet, which limits the length of the addressing information to 60,000 bytes. Figure 2 shows the self-addressing information of a self-addressed packet located within an IP packet.
Different from the way TCP ensures that information reaches the information destination, UDP provides another method. If the self-addressed information packet does not reach the destination, then UDP will not request the sender to resend the self-addressed packet. This is Because UDP contains error detection information in each self-addressed packet, UDP only performs a simple error check after each self-addressed packet reaches the destination. If the detection fails, UDP will discard the self-addressed packet and will not A replacement will be re-requested from the sender, which is similar to sending a letter through the post office. The sender does not need to establish a connection with the recipient before sending the letter. There is also no guarantee that the letter will reach the recipient. Self-addressed sockets work Includes the following three classes: DatagramPacket, DatagramSocket, and MulticastSocket. The DatagramPacket object depicts the address information of the self-addressed packet, the DatagramSocket represents the self-addressing socket of the client program and the service program, and the MulticastSocket represents the self-addressing socket capable of multicast transmission. These three classes are located in java. net package.
 DatagramPacket class
 Before using the self-addressed package, you need to first be familiar with the DatagramPacket class. The address information and the self-addressed package are compressed into the object created by this class in the form of a byte array at the same time
 DatagramPacket has several constructors, even these The form of the constructor is different, but usually they all have two parameters in common: byte [] buffer and int length. The buffer parameter contains a reference to a byte array that stores self-addressed packet information, and length represents a word. The length of the section array.
 The simplest constructor is DatagramPacket(byte [] buffer, int length). This constructor determines the self-addressed data packet array and the length of the array, but does not have any address and port information of the self-addressed data packet. This information It can be added later by calling the methods setAddress(InetAddress addr) and setPort(int port). The following code demonstrates these functions and methods.
byte [] buffer = new byte [100];
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length);
InetAddress ia = InetAddress.getByName ("www.disney.com");
dgp.setAddress (ia) ;
dgp.setPort (6000); // Send datagram packet to port 6000.

The above is the content of Java socket programming (Part 2) (1). For more related content, please pay attention to the PHP Chinese website (www.php. cn)!



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering Well-Formed XML: Best Practices for Data ExchangeMastering Well-Formed XML: Best Practices for Data ExchangeMay 14, 2025 am 12:05 AM

Well-formedXMLiscrucialfordataexchangebecauseitensurescorrectparsingandunderstandingacrosssystems.1)Startwithadeclarationlike.2)Ensureeveryopeningtaghasaclosingtagandelementsareproperlynested.3)Useattributescorrectly,enclosingvaluesinquotesandavoidin

XML: Is it still used?XML: Is it still used?May 13, 2025 pm 03:13 PM

XMLisstillusedduetoitsstructurednature,humanreadability,andwidespreadadoptioninenterpriseenvironments.1)Itfacilitatesdataexchangeinsectorslikefinance(SWIFT)andhealthcare(HL7).2)Itshuman-readableformataidsinmanualdatainspectionandediting.3)XMLisusedin

The Anatomy of an RSS Document: Structure and ElementsThe Anatomy of an RSS Document: Structure and ElementsMay 10, 2025 am 12:23 AM

The structure of an RSS document includes three main elements: 1.: root element, defining the RSS version; 2.: Containing channel information, such as title, link, and description; 3.: Representing specific content entries, including title, link, description, etc.

Understanding RSS Documents: A Comprehensive GuideUnderstanding RSS Documents: A Comprehensive GuideMay 09, 2025 am 12:15 AM

RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.

RSS, XML and the Modern Web: A Content Syndication Deep DiveRSS, XML and the Modern Web: A Content Syndication Deep DiveMay 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

Beyond Basics: Advanced RSS Features Enabled by XMLBeyond Basics: Advanced RSS Features Enabled by XMLMay 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Decoding RSS: An XML Primer for Web DevelopersDecoding RSS: An XML Primer for Web DevelopersMay 06, 2025 am 12:05 AM

RSS is an XML-based format used to publish frequently updated data. As a web developer, understanding RSS can improve content aggregation and automation update capabilities. By learning RSS structure, parsing and generation methods, you will be able to handle RSSfeeds confidently and optimize your web development skills.

JSON vs. XML: Why RSS Chose XMLJSON vs. XML: Why RSS Chose XMLMay 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.