Java--socket Communication

Source: Internet
Author: User

The following is a high-level knowledge of Java development content, which requires a certain foundation for object-oriented, IO, multi-threading, and network-related knowledge in Java. ( knowledge is always in depth, this section is updated in the long term )

1, the basic knowledge of the network

Network communication conditions: 1, two communication end must have their own IP address as a unique identifier, the simple IP address used to distinguish between different machines (computers). 2, the language should be communicated. 3, how to distinguish between different programs of communication requires a port number to distinguish, simply speaking, the port number is used to identify different applications.

TCP/IP is the most widely used protocol in the world, and it is a collection of multiple protocols on different levels based on TCP and IP, also known as the IP protocol family or the TCP/IP protocol stack.

Tcp:transmission Control Protocol Transmission protocol
Ip:internet Protocol Internet Protocol

TCP/IP model

>>ip and Ports <<
1, used to distinguish between different applications.
2, the port number range is 0~65535, wherein 0~1023 is reserved for the system. If you customize the port number, it is recommended to use the port number after 1024.
3. The IP address and port number make up the so-called Socket,socket is the endpoint of the bidirectional communication link between running programs on the network, and it is the basis of TCP and UDP.


Commonly used port number need to remember: http:80 ftp:21 telnet:23

—————————— the network support in Java —————————
For the different levels of network communication, Java provides four main types of network functions:
>>1, inetaddress: Used to identify hardware resources on the network
>>2, URL: Uniform Resource Locator can directly read or write data on the network through a URL
>>3, Socket: A socket-related class that implements network communication using the TCP protocol.
>>4, Datagram: Using the UDP protocol, the data is stored in the datagram and communicated over the network.


2, InetAddress class

Look at the API documentation for I-net-address and discover that there is no construction method, that is, cannot be created with new. So there must be a static method to create.

1 Importjava.net.InetAddress;2 Importjava.net.UnknownHostException;3 Importjava.util.Arrays;4 5  Public classtest1{6      Public Static voidMain (string[] args)throwsunknownhostexception{7         //get the Inetadresss instance of this machine8InetAddress address =inetaddress.getlocalhost ();9SYSTEM.OUT.PRINTLN ("Computer Name:" +address.gethostname () + "\nip Address:" +address.gethostaddress ());Ten  One         //gets the IP address in the form of a byte array A         byte[] bytes =address.getaddress (); -System.out.println ("IP in byte array:" +arrays.tostring (bytes)); - System.out.println (address); the  -         //Inewadress can also be obtained by machine name. -InetAddress address2 = Inetaddress.getbyname ("macbook-air-2.local"); -System.out.println ("InetAddress object created by computer name:" +address2); +SYSTEM.OUT.PRINTLN ("Computer name:" +address2.gethostname ()); -SYSTEM.OUT.PRINTLN ("IP address:" +address2.gethostaddress ()); +  A         //you can also get inewadress by IP address atInetAddress ADDRESS3 = Inetaddress.getbyname ("192.168.1.102"); -System.out.println ("InetAddress object created from the computer IP address:" +ADDRESS3); -SYSTEM.OUT.PRINTLN ("Computer name:" +address3.gethostname ()); -SYSTEM.OUT.PRINTLN ("IP address:" +address3.gethostaddress ()); -  -  in     } -}

Output Result:


3. URL

A URL (Uniform Resource Locator) Uniform Resource Locator that represents the address of a resource on the Internet. Commonly known as the Web site.

The URL consists of two parts: the protocol name + the resource name.

In the java.net package, a URL class is provided to represent the URL.


4. TCP Programming
5. UDP programming

Java--socket Communication

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.