JAVA server-side programming brief

JAVA server-side programming (TCP)

Network programming is the data exchange between two or more devices. In fact, more specifically, network programming is the data exchange between two or more programs. Compared with ordinary stand-alone programs, the biggest difference between network programs is The programs that need to exchange data run on different computers, which causes the complexity of data exchange. Although you can find a program running on the network by IP address and port, if you need to program the network, you also need to understand the process of network communication.

Network programming is divided into client server (C / S) mode and browser server (B / S) mode, and the protocols followed by network programming are divided into: TCP and UDP. In fact, network programming is not difficult. The basic idea is that the client and the server must first establish a connection, then the client and the server exchange data, and finally the client and the server disconnect to release resources. Network programming is the data exchange between two or more devices. In fact, more specifically, network programming is the data exchange between two or more programs. Compared with ordinary stand-alone programs, the biggest difference between network programs is The programs that need to exchange data run on different computers, which causes the complexity of data exchange. Although you can find a program running on the network by IP address and port, if you need to program the network, you also need to understand the process of network communication.

Network programming is divided into client server (C / S) mode and browser server (B / S) mode, and the protocols followed by network programming are divided into: TCP and UDP. The basic idea is that first the client and server must establish a connection, then the client and server exchange data, and finally the client and server disconnect to release resources.

Simple server-side programming

The server side waits for the client's connection, then accepts the client's request, logically analyzes the requested data, and then feeds back the analyzed result to the client. The specific implementation steps are as follows:

(1) Create a ServerSocket object, listen to a specific port, and wait for the client's connection
ServerSocket serverSocket = new ServerSocket (port);
Socket socket2 = serverSocke.accept ();
Create an input and output stream according to the created connection
OutputStream os = socket2.getOutputStream () ; // Get the output stream
InputStream is = socket2.getInputStream (); // Get the input stream
(2) Receive the content sent by the client
byte [] b = new byte [1024];
int n = is.read (b);
String quite = new String (b, 0, n);
(3) Return the processing result to the client
os.write (result.getBytes ());
(4) Close the stream and connect
os.close ();
is. close ();
socket2.close ();
serverSocket.close ();
server-side code example:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleSocketServer {
public static void main (String [] args) {
// TODO Auto-generated method stub
ServerSocket serverSocket = null;
Socket socket2 = null;
OutputStream os = null;
InputStream is = null;
int port = 10000;
try {
// Establish connection
serverSocket = new ServerSocket (port);
// Get connection
socket2 = serverSocket.accept ();
// Accept content sent by the client
is = socket2.getInputStream ();
byte [] b = new byte [1024] ;
int n = is.read (b); // Output
System.out.println ("The content sent by the client is:" + new String (b, 0, n)); // Send feedback content to the client
os = socket2.getOutputStream ();
os.write (b, 0, n);
} catch (Exception e) {
e.printStackTrace ();
} finally {
try {// Close the stream and link
os.close ();
is.close ();
socket2.close ();
serverSocket.close ();
} catch (Exception e) {
e.printStackTrace ():
}
}
}
}

How to establish a Socket connection

How to establish a connection and perform multiple data exchanges? In fact, it is very simple. After the connection is established, the logic of data exchange can be written into a loop. So as long as the loop does not end, the connection will not be closed. According to this idea, the above code can be modified to allow the program to send data three times after establishing a connection once, of course, the number of times here can also be multiple times. The sample code is as follows:
import java.io.InputStream;
import java. io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleSocketServer {

     public static void main(String[] args) {
      // TODO Auto-generated method stub 
      ServerSocket serverSocket=null; 
      Socket socket2=null; OutputStream os=null;
       InputStream is=null; 
       int port=10000;
        try{ 
        // 建立连接 
        serverSocket=new ServerSocket(port);
         // 获得连接 
         socket2=serverSocket.accept(); 
         // 接受客户端发送的内容 
         is=socket2.getInputStream();
          os=socket2.getOutputStream();
           byte[] b=new byte[1024];
            for(int i=0;i<3;i++){ int n=is.read(b);
             // 输出 
                  System.out.println("客户端发送内容为:"+new String(b,0,n));
                   // 向客户端发送回馈内容 
                   os.write(b,0,n);
                    }
             }catch(Exception e){
                      e.printStackTrace();
              }finally { try{
               // 关闭流和链接
                os.close();
                 is.close();
                  socket2.close();
                   serverSocket.close();
    }catch(Exception e){
          e.printStackTrace();
           }
      }
   }

}

Published 9 original articles · received 0 · views 927

Guess you like

Origin blog.csdn.net/weixin_46146588/article/details/105088185