Overview of Network Programming

computer network

It refers to connecting multiple computers with independent functions in different geographical locations and their external devices through communication lines to realize resource sharing and information transmission under the management and negotiation of network operating systems, network management software and network communication protocols. computer system.

network programming

Under the network communication protocol, data exchange can be carried out between programs running on different computers that realize network interconnection.

Three elements of network programming

IP address: In order for computers in the network to communicate with each other, an identification number must be formulated for each computer, and the identification number is used to specify the computer to receive data and identify the computer to send, and the IP address is this identification number, which is The identification of the device.

Port: The communication of the network is essentially the communication of two applications. Each computer has many applications, so how to distinguish these applications during network communication? If the IP address can uniquely identify the device in the network, then the port number can uniquely identify the reference program in the device, that is The identity of the application.

Protocol: Multiple computers can be connected through a computer network. Computers in the same network need to abide by certain rules when connecting and communicating, just like a car driving on the road must obey the traffic rules. In computer networks, these rules of connection and communication are called network communication protocols, which uniformly stipulate the data transmission format, transmission rate, transmission steps, etc., and both parties must abide by them to complete data exchange. Common protocols are UDP protocol and TCP protocol.

Components of an IP Address

32 bits, consisting of 4 8-bit binary numbers

Such as: 11000000.10101000.00000001.11001000, but binary is not convenient to remember, so it will be converted to decimal, such as 192.168.1.200

IP address = network address + host address

Network Address: Identifies the network segment where the computer or network device is located Host Address: Identifies a specific host or network device

IP address configuration and detection

View the IP address of the machine: win+R, enter cmd to enter the command line mode, enter ipconfig, you can view the current IP address of the machine

Test whether the network is smooth: ping the target IP address

DNS domain name resolution

Why do I enter a URL instead of an IP address when visiting a website?

DNS: Domain Name System, Domain Name System

Introduction to Socket

What is Socket?

The endpoint of the communication link is called a "socket" (English name Socket)

It is the interface provided to the application

The underlying mechanism of Socket is complex. The Java platform provides some simple API s, which can be used for Socket development more simply and effectively without understanding the underlying mechanism.

Socket programming based on TCP protocol

Socket network communication based on TCP protocol

Used to achieve two-way secure connection network communication

Socket communication model

When conducting network communication, Socket needs to use data stream to complete data transmission work

Socket network programming can generally be divided into the following steps

1. Establish a connection, 2. Open the input and output streams associated with the Socket, 3. Read and write information in the data stream, 4. Close all data streams and Sockets

This network programming model is called "client/server" (C/S)

Small exercise: the function of simulating user login

Requirement description: Simulate the function of user login, realize that the client sends the login user information, the server side displays the login letter and responds to the client that the login is successful

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) {
		// Create ServerSocket
		ServerSocket ss =null;
		Socket soc=null;
		InputStream is=null;
		BufferedReader br =null;
		OutputStream os=null;
		try {
			ss = new ServerSocket(10088);
			//Listen for client requests
			soc=ss.accept();
			//Get input stream receiving client's request information
			is=soc.getInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			
			String str;
			while((str=br.readLine())!=null){
				System.out.println("Here is the server side, the received client information is:"+str);
			}
			
			//Get the output stream and send the response information to the client
			os=soc.getOutputStream();
			String str2 = "The username and password are correct, the login is successful";
			byte[] bytes = str2.getBytes();
			os.write(bytes);
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				os.close();
				br.close();
				is.close();
				soc.close();
				ss.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
//--------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
	public static void main(String[] args) {
		//Create a Socket object
		Socket socket =null;
		OutputStream os =null;
		InputStream is =null;
		BufferedReader br =null;
		try {
			//The client endpoint of the communication link was created successfully
			socket = new Socket("localhost", 10088);
			//Get the output stream and send the login information to the server
			os =socket.getOutputStream();
			
			String str = "username: tom´╝îPassword: 123456";
			byte[] bytes=str.getBytes();
			//output information
			os.write(bytes);
			System.out.println("The request to the server was successful!");
			//close output stream
			socket.shutdownOutput();
			
			//Get the input stream to receive the response information sent by the server
			is = socket.getInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			String str2;
			while((str2=br.readLine())!=null){
				System.out.println("Here is the client, the response information received from the server is:"+str2);
			}
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
				is.close();
				os.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		
	}
}

Tags: Big Data network

Posted by disaster77 on Sun, 10 Jul 2022 10:02:01 +0930