In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
A preliminary study of MarkdownPad DocumentSocket Network programming client / Server Architecture
That is, the web S architecture, in fact, in a sense, the web service can also be regarded as the Cpact S architecture.
One feature is that the server runs continuously to provide services.
Why to learn socket must first learn the Internet protocol:
The software of Cpact S architecture communicates based on the network.
The core of the network is a bunch of protocols, namely standards. If you want to develop a software based on network communication, you must follow these standards.
Socket is a set of interfaces between the application layer and the transport layer
Speaking of which, what on earth is socket? Socket is the middle software abstraction layer for the communication between the application layer and the TCP/IP protocol family, and it is a group of interfaces. In the design pattern, socket is actually a facade pattern, which hides the complex TCP/IP protocol family behind the socket interface. For users, a simple set of interfaces is all, allowing socket to organize the data to comply with the specified protocol. So, there is no need to delve into the TCP/UDP protocol, socket has sealed it for us.
File type based socket family: AFUNIX network type based socket family: AFINET
The workflow of sockets
Let's start with the server side. The server initializes Socket, then binds to the port (bind), listen the port, calls accept blocking, and waits for the client to connect. At this point, if a client initializes a Socket and then connects to the server (connect), if the connection is successful, the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection. An interaction ends.
Server socket function
S.bind () bind (host, port number) to socket s.listen () start TCP snooping s.accept () passively accept connections from TCP customers, (blocking) wait for the connection to arrive
Client socket function
S.connect () initializes the extended version of the TCP server connection s.connect_ex () connect () function, and returns the error code when an error occurs instead of throwing an exception
Socket function for public use
S.recv () receives TCP data s.send () sends TCP data (when the amount of data to be sent is greater than the remaining space of the self-end cache, the data is lost and will not be sent out) s.sendall () sends the complete TCP data (in essence, when the amount of data to be sent is greater than the remaining space of the own-end cache, the data will not be lost. Call send in a loop until it is finished) s.recvfrom () receives UDP data s.sendto () sends UDP data s.getpeername () connects to the address of the remote end of the current socket s.getsockname () the address of the current socket s.getsockopt () returns the parameter s.setsockopt () of the specified socket sets the parameter s.close () of the specified socket closes the socket
Lock-oriented socket method
S.setblocking () sets the blocking and non-blocking modes of sockets s.settimeout () sets the timeout of blocking socket operations s.gettimeout () gets the timeout of blocking socket operations
Functions of file-oriented sockets
The file descriptor s.makefile () of the s.fileno () socket creates a file associated with the socket
Code example:
Server side
From socket import * phone=socket (AF_INET,SOCK_STREAM) phone.bind ('127.0.0.1) phone.listen (5) conn,addr=phone.accept () while True: data=conn.recv (1024) print (' server=== >') print (data) conn.send (data.upper ()) conn.close () phone.close ()
Client
From socket import * phone=socket (AF_INET,SOCK_STREAM) phone.connect (('127.0.0.1) while True: msg=input (' > >:'). Strip () phone.send (msg.encode ('utf-8')) print (' client==== >') data=phone.recv (1024) print (data)
Note: at this time, when the client input message is empty, the program will get stuck, that is because both the server and the client have to go to their host buffer to get the message, because the send is empty, the server will not have any reaction at all, so the client's buffer will not have any content, so it will wait in vain at this time.
The solution at this time, of course, needs to be found on the client side. It is very simple that we do not let the client send an empty message, and make a judgment before the send () method:
If not msg: continue
You must think it's over at this time, please try to disconnect the client link. Just imagine, since as a server, as the name implies, it is necessary to continuously provide services for different clients, how can a client be used up and finished? Solutions are as follows:
While True: # Link Loop conn,addr=phone.accept () print ('phone line is', conn) print ('client's mobile number is', addr) while True: # Communication Loop try: data=conn.recv (1024) if not data:break print ('message from client is' Data) conn.send (data.upper ()) except Exception: break conn.close () phone.close ()
Note: someone may encounter the situation of Address already in use when restarting the server. This is because your server still has a timewait status of four waves and occupies the address (if you do not understand, please further study the 1.tcp three-way handshake, four waves 2.syn flood * 3. In the case of high server concurrency, there will be a large number of timewait state optimization methods)
Solution:
# add a socket configuration, reuse ip and port phone=socket (AF_INET,SOCK_STREAM) phone.setsockopt (SOL_SOCKET,SO_REUSEADDR,1) # that's it, add phone.bind (('127.0.0.1) phone.setsockopt 8080) before bind)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.