java--Network Knowledge Accumulation

Source: Internet
Author: User

1. Obtain all host IP addresses for the LAN

Ping IP timeout time to wait for each reply:-W 280 number of ECHO requests to send:-N 1

Process process=runtime.getruntime (). EXEC ("ping");

Process.getinputstream ()----inputstreamreader ()----Bufferreader in=new bufferreader ()----String Line=in.readline ( )----line.substring (0,2). Equals ("from") | | -line.substring (0,10). Equals ("Reply from")

Obtain all the IP address of the intranet, first obtain the network segment that the IP address of the machine belongs to, then ping the IP address in the network, read the result of the ping by the input stream object, and determine whether the IP address of the intranet

2. Get the size of a network resource

The new URL (String URL). OpenConnection (). connect (); Open a communication link to a URL reference resource

Urlconnection.getcontentlength () returns the resource size in bytes

3. Multi-threaded download of network resources

1>conn.getinputstream ()----bufferedinputstream (in)----int len=bin.read (buffer)!=1

Byte[] Buffer=new byte[2048]; Out.write (Buffer,0,len);

2>new url (String url). OpenConnection (). Getresponsecode () Get the connection Status tag code

Getcontentlength () disconnect () 200--connection Status

. Setrequestproperty ("User-agent", "Netfox") Set request properties

. Setrequestproperty ("range", "byte=" + "-") Set the request Scope property String File=getfile ()

4. Transferring pictures using the socket

DataInputStream read ()----The picture file to the byte array

DataOutputStream (Inherit dataoutput)----write ()----output byte array

New Serversocket.accept ()----instantiating the socket object

DataInputStream (Socket.getoutputstream ())----Readlong ()/readbyte () DataOutputStream (Socket.getinputstream ())

Client send: New DataInputStream (New FileInputStream (imgfile))----Read (BT)

Out.writelong (Imgfile.length ()) Out.write (BT) new byte[(int) imagefile.length ()]

5.BufferReader reader=new Bufferreader (New InputStreamReader (Socket.getinputstream ()))----String S=reader.readline ()

java--Network Knowledge Accumulation

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.