A server is a computer or program that provides information, a client is a computer or program that requests information, and a network is used to connect a server to a client to communicate with each other. But sometimes it is difficult to separate the server from the client area in a network. What we usually call "local area Network,lan" is a group of computers connected by certain forms. It can be composed of two computers or thousands of computers in the same region. Extended from a LAN to a larger range, such a network is called a "wan" (Wide area Network,wan). The Internet (Internet) We are familiar with is made up of countless LANs and WANs.
In general, a computer has only a single "physical Connection" (physical Connection) connected to the network, and all data is delivered internally and externally to a particular computer through this connection. This is the port. The port in network programming is not a real physical existence, but an imaginary connection device. The port is defined as an integer between 0~65535. HTTP services typically use port 80, and the FTP service uses 21 ports. If a computer provides a variety of services such as HTTP, FTP, and so on, the client uses a different port to determine which service is connected to the server, as shown in.
A socket (socket) in a network program is used to connect an application to a port. A socket is an imaginary connection device, like a plug-in device "receptacle", used to connect electrical appliances and wires, as shown in. Java abstracts sockets into classes, and programmers can use sockets simply by creating a socket class object.
TCP Program Design:
Steps:
1. The server creates serversocket and calls the Accept () method to wait for the client to request.
2, the client creates the socket, requests the server.
3, the server receives the request, the Accept () method returns a socket, which establishes the connection.
InetAddress class--IP related
ServerSocket class--server sockets
The main function is to wait for a "request" from the network, which can wait for a connected socket through the specified port. A server socket can be connected to a socket at a time. If more than one client makes a connection request at the same time, the server socket stores the client that requested the connection in a queue and then extracts a socket from it to connect to the new socket on the server. If the number of requested connections is greater than the maximum number of holds, the extra connection request is rejected. The default size of the queue is 50.
constructor function:
ServerSocket () throws IOException
ServerSocket (int port) throws IOException
ServerSocket (int port, int backlog) throws IOException
ServerSocket (int port, int backlog, inetaddress bindaddr) throws IOException
There are two streams in a Java socket,
One is the input stream getinputstream, used to read the data sent by the socket, is to receive the message.
The other is the output stream Getoutputstream, which is used to send data to the socket, that is, to send the message.
code example:
Tcpserver.java
/** TCPServer * @author chenming * @version 2016-7-10*/ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTCPServer { Public Static voidMain (string[] args) {Try{ /******************************* Monitor and connect ***********************************/ServerSocket ServerSocket=NULL ; Try{ServerSocket=NewServerSocket (4000);//Create a Port listener}Catch(Exception e) {e.printstacktrace (); } Socket Socket=NULL ; Try{System.out.println ("Open the server!" "); Socket= Serversocket.accept ();//blocks until a request is received and a socket is returned}Catch(Exception e) {e.printstacktrace (); } /******************************* receive connection ***********************************/ //The input stream is obtained by the socket object and the corresponding BufferedReader is constructedBufferedReader Read =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); Try{ while(true) {System.out.println ("Server Received:" +read.readline ()); } }Catch(Exception e) {e.printstacktrace (); } /******************************* Closing the connection ***********************************/ Try{ if(read!=NULL) Read.close (); if(socket!=NULL) Socket.close (); if(serversocket!=NULL) Serversocket.close (); System.out.println ("Server Off! "); }Catch(Exception e) {e.printstacktrace (); } }Catch(Exception e) {E.printstacktrace (); } }}
Tcpclient.java
/** TCPClient * @author chenming * @version 2016-7-10*/ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket; Public classTCPClient { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub Try{ /******************************* Request Connection ***********************************/ //make a customer request to port 5000 on this machineSocket socket =NULL ; Socket=NewSocket ("127.0.0.1", 4000) ; //constructing Bufferreader objects from System standard input devicesSYSTEM.OUT.PRINTLN ("Client connection succeeded! "); /******************************* transmitting data ***********************************/BufferedReader in=NULL ; Inch=NewBufferedReader (NewInputStreamReader (system.in)); //The output stream is obtained by the socket object, and the PrintWriter object is constructedPrintWriter writer =NULL ; Writer=NewPrintWriter (Socket.getoutputstream ()); Try{ while(true) {writer.println (In.readline ());//output A string read from the system standard input to the serverWriter.flush ();//refreshes the output stream so that the server receives the string immediately } }Catch(Exception e) {e.printstacktrace (); } /******************************* Closing the connection ***********************************/ Try{ if(in!=NULL) In.close (); if(writer!=NULL) Writer.close (); if(socket!=NULL) Socket.close (); System.out.println ("Client connection Disconnected!" "); }Catch(Exception e) {e.printstacktrace (); } }Catch(Exception e) {e.printstacktrace (); } }}
Java Network Programming Learning