Dark Horse programmer _ Network Programming

Source: Internet
Author: User

 

IP address

1. IP address classification

Class A 1.0.0.0 to 126.0.0.0

Class B 128.1.0.0 to 191.254.0.0

Class C: 192.0.1.0 to 223.20.254.0

Class D 224.0.0.0 to 239.255.255.255

Class E: 240.0.0.0 to 255.255.255.254

Start with IP 192.168 in the LAN

Local loopback address: 127.0.0.1. The IP address of the local machine, as long as the NIC works properly, 127.0.0.1

2. java.net. inetaddress

Class that describes the IP address object. No construction method is available.

Methods In the inetaddress class:

Static inetaddress getlocalhost () returns the local IP address object.

String gethostname () returns the host name.

String gethostaddress () returns the IP address of the host.

Static inetadress getbyname (string host name) obtains the IP address corresponding to the host name.

3. Port Number

If a program occupies a port, other programs cannot use this port.

0-65535: available port number

0-1024: Reserved Port Number of the system

80: Default port number for Internet access

21: FTP upload port number

3306: MySQL database connection Port

1521: Oracle Database Connection Port

1433: Connection port of Ms sqlserver Database

 

 

Communication Protocol

1. UDP protocol

Features of UDP:

Connection-free

Insecure and data may be lost

Data cannot exceed 64 KB

Fast

Suitable for communication, such as feiqiu, QQ, and MSN.

2. Socket Service

Both parties must have socket services. Pass your data to the socket transmission tool and send the data. The receiver also uses the socket tool.

To accept the sent data.

Dategrampacket data packet object

Dategramsocket is a UDP communication tool that sends and receives data.

3. UDP sending operations

Encapsulate data into a data packet dategrampacket dategramsocket (array, array length, destination IP address, receiver port number)

Create a communication tool dategramsocket object

Send the data packet to the dategramsocket tool for sending.

Close Resources

  

1 import java.net. *; 2 public class udpsend {3 Public static void main (string [] ARGs) throws exception {4 5 byte [] bytes = "Hello UDP ". getbytes (); 6 // create a data packet object. 7 bytes rampacket dp = new 8 bytes rampacket (bytes, bytes. length, inetaddress. getbyname ("127.0.0.1"), 8000); 9 // create a communication tool socket service, datagramsocket10 datagramsocket DS = new datagramsocket (10000); 11 // call the send method of DS, transmit data packet 12 Ds. send (DP); 13 // close resource 14 DS. close (); 15} 16}

 

4. UDP receiver operations

Create a data packet object to accept the sent data packet

Create a communication tool dategramsocket object dategramsocket (byte array, length)

Send the data packet to the tool dategramsocket to accept the receive (data packet)

Detach data packets

Package Splitting:

Converts a byte array into a string.

The number of valid bytes sent by the getlength () method in the dategrampacket class

The method getport () in the dategrampacket class specifies the sender's port number.

The method getaddress () in the dategrampacket class obtains the inetaddress type of the sender's IP address object,

Gethostaddress () obtains the IP address from this IP address object.

Close Resources

1 import java.net. *; 2 public class udpreceive {3 Public static void main (string [] ARGs) throws exception {4 byte [] bytes = new byte [10]; 5 // create data packet 6 datagrampacket dp = new datagrampacket (bytes, bytes. length); 7 // establish a communication tool, open the port, listen to a port 8 datagramsocket DS = new datagramsocket (8000); 9 // call the DS receive (packet) 10 Ds. receive (DP); // method of receiving, thread waiting effect 11 // get valid bytes 12 INT length = DP. getlength (); 13 // port number 14 int Port = DP. getport (); 15 // sent IP object 16 string IP = DP. getaddress (). gethostaddress (); 17 system. out. println (new string (bytes, 0, length) + "" + IP + "" + port); 18 // disable resource 19 Ds. close (); 20} 21}

 

5. TCP protocol

TCP features:

Connection-oriented

Security

Suitable for Big Data Transmission

Three-way handshake, slow speed

Suitable for secure data transmission, such as downloading and online videos.

Io data transmission is a byte stream. It is obtained through a socket connection object and cannot be created by yourself.

 

6. tcp client operations

Create a connection object Socket socket ("ip", port number)

Get the byte output stream through the socket object and send the data to the server

Get the byte input stream through the socket object and read the data sent back by the server

Close Resources

1 import Java. io. *; 2 Import java.net. *; 3 Public class fileclient {4 public static void main (string [] ARGs) throws exception {5 socket S = new socket ("192.168.1.102", 8100 ); 6 fileinputstream FCM = new fileinputstream ("E: \ 2.jpg"); 7 outputstream out = S. getoutputstream (); 8 int line = 0; 9 byte [] bytes = new byte [1024]; 10 while (line = FCM. read (bytes ))! =-1) {11 out. write (bytes, 0, line); 12} 13 // end mark 14 S. shutdownoutput (); 15 inputstream in = S. getinputstream (); 16 line = in. read (bytes); 17 system. out. println ("the server says:" + new string (bytes, 0, line); 18 fi. close (); 19 s. close (); 20} 21}

 

7. TCP server operations

Establish the server connection object seversocket

Get the connection object of the client, know the client and server connection, seversocket class method accept

Obtain the client connection object. You can obtain the object from the client connection object socket.

 

1 import Java. io. *; 2 Import java.net. *; 3 Import Java. util. *; 4 public class fileserver {5 public static void main (string [] ARGs) throws exception {6 serversocket Ss = new serversocket (8100); 7 socket S = ss. accept (); 8 inputstream in = S. getinputstream (); 9 file = new file ("D: \ A"); 10 if (! File. exists () 11 file. mkdirs (); 12 string filename = system. currenttimemillis () + new random (). nextint (100) + ". jpg "; 13 fileoutputstream Fos = new fileoutputstream (File + file. separator + filename); 14 int line = 0; 15 byte [] bytes = new byte [1024*1000]; 16 while (line = in. read (bytes ))! =-1) {17 FOS. write (bytes, 0, line); 18} 19 outputstream out = S. getoutputstream (); 20 out. write ("Upload successful ". getbytes (); 21 FOS. close (); 22 s. close (); 23 ss. close (); 24} 25}

 

Dark Horse programmer _ Network Programming

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.