Java Network Entry 1 (j2se entry 18)

Source: Internet
Author: User

Basic network knowledge
 
Network programming aims to communicate directly or indirectly with other computers through network protocols.
The computer network is diverse and complex. Computers on the network must follow certain protocols to communicate with each other. Currently, the most widely used network protocol is the TCP/IP protocol used on the Internet.
IP Address: the unique identifier of the computer in the network. The IP address is a logical address relative to the Internet.

Class A address

Class A address: 1st bytes are the network address, and the other 3 bytes are the host address. The maximum bit of the other 1st bytes is fixed to 0.
Class A address range: 1.0.0.1 to 126.155.255.254.
Private and reserved addresses in Class A addresses:
10.0.0.0 to 10.255.255.255.255 is a private address ).
127.0.0.0 to 127.20.255 are reserved addresses for cyclic testing.

Class B address

Class B address 1st bytes and 2nd bytes are network addresses, and the other two bytes are host addresses. In addition, the first two digits of the 1st bytes are fixed as 10.
Class B address range: 128.0.0.1 to 191.20.254.
Private and reserved addresses of Class B addresses
172.16.0.0 to 172.31.255.255 is a private address.
169.254.0.0 to 169.254.255.255 are reserved addresses. If your IP address is automatically obtained, and you have not found any available DHCP server on the network, you will get an IP address from 169.254.0.0 to 169.254.255.255.

Class C address

Class C address 1st bytes, 2nd bytes, and 3rd bytes are network addresses, and 4th bytes are host addresses. The first three digits of the other 1st bytes are fixed to 110.
Class C address range: 192.0.0.1 to 223.20.254.
Private address in Class C address:
192.168.0.0 to 192.168.255.255 is a private address.

Class D address 

Class D address, regardless of the network address and host address, the first four digits of its 1st bytes are fixed as 1110.
Class D address range: 224.0.0.1 to 239.20.254

MAC address: the private address of each Nic, which is also unique.
Port: the application (process) identifier (network communication program) OS can have 65536 (2 ^ 16) ports, and the process exchanges data through the port. When connecting lines, you need to enter the IP address and port information.
In fact, the Process Communication Between Hosts needs to be performed on the port.
192.168.0.23: 21
Protocol: rules, standards, or conventions established for data exchange (communication) in the network. The protocol is used to ensure communication security.
The protocols at different layers are completely different.

Network Layer: Addressing and routing (the process of arriving at the address)
Transport Layer: port connection
TCP model: Application Layer/Transport Layer/Network Interface
One-way dependency between layers, the upper layer depends on the lower layer, the lower layer does not rely on the upper layer, and the connection between the layer is virtual connection. Establish protocols between peer layers.
A port is an abstract software structure related to the Protocol: The tcp23 port and the udt23 port are two different concepts.
Port 1024 or above should be used, and the following ports have been set to function.

TCP/IP Model

Application
(FTP, HTTP, telnet, POP3, smpt)
Transport
(TCP, UDP)
Network
(IP, ICMP, ARP, RARP)
Link
(Device driver ,....)

Note:
IP: Addressing and Routing
Address Resolution Protocol (ARP): converts an IP address to a MAC address.
RARP (reflect Address Resolution Protocol) Reverse Address Resolution Protocol: opposite to the above
ICMP (Internet Control Message Protocol) detects link connection conditions. Tools using this Protocol: Ping, Traceroute

TCP socket

TCP is short for tranfer control protocol.Connection-oriented protocol for reliable transmission. It is transmitted over TCP to obtain a sequential error-free data stream. The sender and receiver must establish a connection between two pairs of sockets to facilitate communication based on the TCP protocol. When a socket (usually a server socket) is waiting to establish a connection, another socket can require a connection. Once the two sockets are connected, they can perform bidirectional data transmission, and both parties can send or receive data.
1) The server allocates a port number. The server uses the accept () method to wait for the signal from the client. Once the signal reaches the socket connection, outputstream and inputstream are obtained from the socket.
2) The client provides the host address and port number to establish a connection using the socket port to obtain outputstream and inputstream.

TCP/IP Transport Layer Protocol

Create a TCP Server

Generally, we write the server as a distribution socket, that is, it is always running,
To create a TCP server program:
1). CreateServersocket
2). From serversocketAccept customer connection requests
3) createService threads process new connections
4 ).In the service threadObtain the I/O stream in the socket
5 ).I/O Stream reads and writes to complete interaction with customers
6 ).Disable the I/O Stream
7 ).Disable socket
Serversocket Server = new serversocket (post)
Socket Connection = server. Accept ();
Objectinputstream put = new objectinputstream (connection. getinputstream ());
Objectoutputstreamo put = newobjectoutputstream (connection. getoutputstream ());
Processes input and output streams;
Disable stream and socket.
Typical server. Import java. Io .*;
Import java.net .*;

Public class server1 {
Public static void main (string [] ARGs) throws exception {
Serversocket Ss = new serversocket (9000 );
While (true ){
Socket S = ss. Accept (); // obtain a socket object.
Thread t = new thread1 (s); // distribution socket.
T. Start ();
}
}
}
Class thread1 extends thread {
Socket S;

Public thread1 (socket s ){
This. S = s;
}

Public void run (){
Try {
Outputstream o = S. getoutputstream ();
Printwriter out = new printwriter (O );
Out. println ("Hello client ");
Out. Flush ();
S. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}

Related Article

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.