Java Network Design Lite Introductory Edition

Source: Internet
Author: User
Tags get ip

First, the basic knowledge

1.TCP

2.UDP

Second, IP address encapsulation

Common methods for 1.InetAddress classes

Getlocalhost ()                 returns the InetAddress object of the local host                 inetaddress type Getbyname (String host)         gets the IP address                       of the specified host name InetAddress type gethostname ()                  gets the host name                                 stringgethostaddress ()               Gets the host IP address                               stringisreachable ( int timeout) to       test whether the IP address is up to a Boolean in the millisecond time specified by timeout    

2. Example 1: Test the IP address from "192.168.131.1" to "192.168.131.150" the name of all accessible hosts, if the other side does not have a firewall installed, and the network connection is normal, can be accessed. As can be seen from the output, 192.168.131.1 is the IP of the local host, other addresses can not be connected, it may be because 1000 milliseconds is too short or the other host installed firewall reasons.

 PackageBigjunoba.bjtu.iptoname;Importjava.io.IOException;Importjava.net.InetAddress;Importjava.net.UnknownHostException; Public classIptoname { Public Static voidMain (string[] args) {String IP=NULL;  for(inti = 1; I <= 150; i++) {IP= "192.168.131." + i;//Generate IP String            Try{inetaddress host; Host= Inetaddress.getbyname (IP);//Get IP Encapsulated objects                if(host.isreachable (1000)) {//test if the IP address is up to 1 secondsString HostName =Host.gethostname (); System.out.println ("IP Address" + IP + "host name is:" +hostName); }            } Catch(Unknownhostexception e) {//Catching unknown host exceptionsE.printstacktrace (); } Catch(IOException e) {//capturing input and output exceptionsE.printstacktrace (); }} System.out.println ("Search Complete! "); }}
the host name of the IP address 192.168.131.1 is: Bigjunoba search complete! 

Third, socket

A socket (socket) is an object that represents a network connection between computers, which establishes a TCP connection between computers so that connections can be made between computers and network traffic is implemented.

1. Server-side sockets

A server-side socket is an instance object of the Sercersocket class that implements the server cache. The ServerSocket class monitors the specified port and establishes a client-to-server socket connection, which is where the customer is responsible for calling the task.

(1) Creating server-side sockets

Creating a server-side socket can use the 4 construction method.

①serverscoket ()

The default constructor method allows you to create a server socket with an unbound port number. All constructor methods for a server socket need to handle ioexception exceptions.

The General format is:

            Try {                new  serversocket ();             Catch (IOException e) {                e.printstacktrace ();            }

②serverscoket (int port)

            Try {                new serversocket (9527);             Catch (IOException e) {                e.printstacktrace ();            }        }

            Try {                new serversocket (9527);             Catch (IOException e) {                e.printstacktrace ();            }        }

            Try {                new serversocket (9527);             Catch (IOException e) {                e.printstacktrace ();            }        }

(2) Create a Receive socket connection

2. Client sockets

(1) Creating a client socket

(2) Sending and receiving data

Iv. Data Report

1.DatagramPacket

2.DatagramSocket

Five, network Chat program development

Java Network Design Lite Introductory Edition

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.