Servers and clients

Source: Internet
Author: User

The basic spirit of the

Network is to connect the two machines together and "talk" or "communicate" with each other. Once both machines have found each other, a delightful two-way conversation can be launched. But how can they "find" each other? It's like being in an amusement park: A machine has to stay in one place and listen to other machines and say, "Hey, where are you?" The "
Stay in one place" machine is called a "server", and the "find" machine is called "Client" or "client". The difference between them is obvious only when the client tries to connect to the server. Once connected, it becomes a two-way communication, and it becomes less important who acts as a server or client.
so the primary task of the server is to listen for requests to establish a connection, which is done by the specific server object we created. The client's task is to try to establish a connection with a server, which is done by the specific client object we created. Once the connection is built, the connection is magically turned into an IO data stream object on both the server side and the client side. From this point on, we can treat the connection as if we were reading and writing a normal file. So once the connection is built, we need to use our familiar IO commands just like the 10th chapter. This is the most convenient place for Java networking.

1. Test the program without a network
For a variety of potential reasons, we may not have a client, a server, and a network to test the programs we do well. We may be practicing in a classroom environment, or writing a less reliable web application and getting to the web. The designer of IP notices this problem and establishes a special address--localhost--to meet the test requirements in the non-network environment. The most common way to generate this address in Java is to:
InetAddress addr = Inetaddress.getbyname (null);
If you pass a null (NULL) value to Getbyname (), the default is to use localhost. We use InetAddress to index a particular machine, and we must get this inetaddress (Internet address) before further action is required. We can't manipulate a inetaddress content (but we can print it out, just as the next example shows). The only way to create a inetaddress is by the static (static) member method of that class Getbyname () (which is most commonly used), getallbyname (), or getlocalhost ().
To obtain a local host address, you can also pass the string "localhost" directly to it:
Inetaddress.getbyname ("localhost");
or use its reserved IP address (four-point form), just like the following:
Inetaddress.getbyname ("127.0.0.1");
The results of these three methods are the same.

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.