First, the network basic concept
First of all, to clarify a concept: Network programming! = Web programming, network programming is now commonly called TCP/IP programming.
II. Network communication Protocol and interface
Three, the communication protocol stratification thought
Iv. Reference Model
V. IP protocol
Each person's computer has a unique IP address so that it doesn't transmit the wrong message when communicating with each other.
The IP address is divided into four segments with a single point, the IP address in the computer is expressed in four bytes, one byte represents a paragraph, each byte represents the maximum number can reach 255.
Vi. TCP protocol and UDP protocol
TCP and UDP are located on the same layer, and are based on the IP layer. Since there are different IP addresses between the two computers, the two computers can be separated from each other and they can talk to each other. There are generally two types of calls: The first is TCP, and the second is UDP. TCP is a reliable connection, TCP is like a phone call, you need to get to the other side of the phone, waiting for the other to have a response before they will continue to talk, that is, must be sure to send information will be sent to the message. TCP uploads anything is reliable, as long as two machines to establish a connection, the data sent on the machine will be able to upload to the other side of the machine, UDP is like a telegram, sent out to be done, the other party has not received it regardless, so UDP is unreliable. TCP transmits data reliably, but transmits slowly, UDP transmits data unreliable, but transmits quickly.
Seven, socket programming
The general network programming is called the socket programming, the socket English meaning is "the socket".
Both computers are fitted with a previous socket and are then plugged into two computer sockets using the ends of a line so that the two computers are connected. This socket is the socket.
Because we can communicate with each other, I said you are my server only logically speaking, I should send things to you first, and then by you to deal with, forward. So you call it the server. However, technically speaking, only TCP will be divided into server and client. For UDP, there is no so-called server and client in the strictest sense. The socket of TCP's server is called Socket serversocket,client.
When two computers are connected to each other, they must first know their IP address, but it is not enough to provide an IP address, it must also have a connection port number, which is the application to connect to.
The port number is used to differentiate between different applications on a single machine. the port number occupies 2 bytes inside the computer. There are up to 65,536 port numbers on a single machine. An application can consume multiple port numbers. If the port number is consumed by an application, then other applications will no longer be able to use the port number. Keep in mind that the program we write consumes the port number above 1024 of the port number, and the port number below 1024 is not occupied because the system may be requisitioned at any time. The port number itself is divided into TCP and UDP ports, and TCP 8888 ports and UDP 8888 ports are completely different two ports. There are 65,536 TCP ports and UDP ports.
VIII. TCP Socket Communication model
ix. Example of socket usage
Server-Side ServerSocket
Import java.net.*;
Import java.io.*; public class testserversocket{public static void Main (String args[]) throws exception{ServerSocket ss = new
ServerSocket (6666); /* When creating a ServerSocket object, it is often given a port number to specify the port number, which is the port number to use for the new ServerSocket object, and which port number to listen for the client connection. The meaning of specifying a port number is to tell the computer where the ServerSocket object is listening for the connection to the client./* Server-side requests to receive client connections are received uninterrupted, so server-side programming is generally a dead loop, never ending Running.
*/while (true) {Socket s = ss.accept (); /* The Accept () method is invoked on the server side to accept the client's connection object, and the Accept () method is a blocking method that has been innocently waiting for a client to connect up and then the socket socket on the server side and the client s
Ocket socket set up to connect * * * Client can connect to the server side, depending on whether the server side accept the client's connection request if the client's connection request is accepted, install the previous socket socket on the server side
Through this socket and connected to the client can establish a connection, communication with each other * * SYSTEM.OUT.PRINTLN ("A client connected!"); /* Use InputStream stream to receive information sent from the client, use DataInputStream data stream to process received information/datainputstream dis = new DataInputStream (s.getinputs
Tream ()); /* Use readUTF (method to read all the received information and store it in the variable str readutf) method is also a blocking method, will innocently wait for the client to send information, and then read out the received information If the client does not write things over, it has been innocently waiting on the server side, until the client to write something over the blocking method efficiency is often not high, such as a client connection up, but it is slow to send information, then server-side
The program is blocked, so that another client will not be connected, because another client to connect to the server side, you must call the Accept () method on the server, the accept () method must be in the next cycle can be
Call, now the server-side program runs to call readUTF () This method is blocked, it will wait for the client has been connected to send information after the message to read out, if the client has not sent information to the server side, then readUTF () method
has been unable to read the information, then the server-side program will block here, the next cycle, so that other clients will not be able to connect to the server side/String str = DIS.READUTF ();
System.out.println (str); }
}
}
Client socket
Import java.net.*;
Import java.io.*;
public class testclientsocket{public
static void Main (String args[]) throws exception{
socket s = new socket ("127 .0.0.1 ", 6666);
/*client request to connect to the server side
/
/////////////////////////////////// * Here is the use of output stream outputstream to the server-side output information * *
outputstream OS = S.getoutputstream ();
DataOutputStream dos = new DataOutputStream (OS);
Thread.Sleep (30000);/* Client sleeps 30 seconds before sending information to server side
/Dos.writeutf ("Hello server!");
}
The client requests the connection through port 6666 to the server side, after the server accepts the client's connection request, installs a socket on the server side, then lets this socket connect with the client socket, so that the server can communicate with the client, When another client requests a connection, the server side accepts, and another socket is installed to connect to the client's socket.