Network communication & first understanding of socket

Posted by vishal99 on Tue, 14 May 2019 15:10:59 +0200

The main contents of this section are as follows:

1. Customer Short Server Architecture

2. Network communication process

3. Initial knowledge of socket

 

I. Client Server Architecture

ClientServer Architecture:

Client/S erver (C/S) structure is a well-known software system architecture. By assigning tasks reasonably to Client and S erver, the communication overhead of the system is reduced, and the client needs to be installed to manage the operation.
The client side and the server side have different programs. The user's programs are mainly in the client side. The server side mainly provides data management, data sharing, data and system maintenance and concurrency control, etc. The client side mainly completes the user's specific business.
 
B\S architecture:
Browser/Server (browser/server) structure is a kind of structure that changes or improves C/S structure with the rise of Internet technology. In this structure, the user interface is implemented entirely through WWW browser.
There is basically no special application on the client side, and the application programs are basically on the server side. Because there is no program on the client side, the upgrade and maintenance of the application can be completed on the server side, and the upgrade and maintenance are convenient. Because the client uses browser, the user interface is "rich and colorful", but the printing and output functions of data are limited. In order to overcome this shortcoming, browser is usually used to realize the difficult function, which can be separately developed into a control that can be published, and the program calls are used to complete it on the client side.
 

Software C S Architecture: Client Server Architecture: Jingdong, Taobao, Today's Headlines, qq, Wechat...

B S Architecture: Browser Server > Unified Interface

Hardware CS Architecture: Printer.

 

2. Network communication process

Network Line: Transmission of Electrical Signals

Hub: Connect all network devices connected to the hub

Switch: Upgraded Hub

Network Card: Provide Network Interface, Receive Electric Signal

MAC address: Physical address, 48-bit binary length, usually represented by 12-bit hexadecimal number (the first six are manufacturer numbers, the last six are pipeline numbers)

How to view the mac address: Enter the ipconfig-all instruction in the cmd window under windows: The physical address shown below is the mac address.

IP Address: It consists of four decimal digits, each of which corresponds to eight binary digits.

about Network Communications Please click on the link to view the whole process.

 

3. Initial knowledge of socket

socket is a tool for network programming. Here are two simple pieces of code.

import socket
#Create a socket object
server = socket.socket()  #It's equivalent to creating a phone.
ip_port = ('192.168.111.1',8001) #Create a phone card
server.bind(ip_port) #Plug in the phone card
server.listen(5) #Listen to the phone, I can listen to five, after receiving a phone call, there are four people to call me.
But the last four people have to wait in line, waiting for my first phone to hang up, and the sixth person's cell phone will report a mistake when it comes to the sixth one.
print('11111') #Waiting for someone to call me, when they call, I get this connection to each other. conn The phone number of the opposite party addr conn,addr = server.accept() #Block up,Wait until someone connects me, and then they get a Yuanzu, which is a connecting channel. conn Address of the other party(ip+port) print('22222') print(conn) print('>>>>>>>>>',addr) while True: from_client_data = conn.recv(1024) #The server must receive messages through a connection between the two. from_client_data = from_client_data.decode('utf-8') print(from_client_data) if from_client_data == 'bye': break server_input = input('Mingwei said>>>>: ') conn.send(server_input.encode('utf-8')) if server_input == 'bye': break conn.close() #Hang up the phone server.close() #Cell phone
import socket
import time

client = socket.socket()
server_ip_port = ('192.168.111.1',8001)

client.connect(server_ip_port)

while True:
    client_input = input('Xiaowen said>>>>: ')
    client.send(client_input.encode('utf-8')) #Send a message to the server
    if client_input == 'bye':
        break
    from_server_data = client.recv(1024)

    print('Messages from the server:',from_server_data.decode('utf-8'))
    if from_server_data.decode('utf-8') == 'bye':
        break
client.close() #Client hangs up

 

Note: First run the server, then run the client, and then you will find the client file and the output desk where you enter content, you enter a content and then return, you will find that the server side of the console output the content sent by the client.
Through the above two pieces of code, our team has a rough understanding of network programming.

Topics: Python socket network Mac Programming