Speaking of developers' thinking and habits from the example of developing a Socket using Python

Source: Internet
Author: User

Today, we mainly talk about the thinking and habits of developers.

Thinking includes programming thinking and analytical thinking for solving a specific problem, analysis ideas, analysis methods, and even analysis tools.

Both good habits and bad habits are formed by a day's thinking. Those bad habits are hard to be changed after a long time. Therefore, if you recognize it today, you can change it from today, and get the benefit of it early.

Let's talk about today's introduction, that is, using Python to develop a simple Socket Application, a small example of client/server Communication.

Suppose now we need to use python to develop a socket chat application, we may encounter the following problems.

  • Python has never been used. What should I do?
  • I have used python, but have we developed socket applications?
  • I still don't know what socket is?
  • I probably remember socket, ip + port, but I don't know the specific tcp and udp.

In fact, we may solve some problems that we have not solved almost every day. It may be a class library that we have never heard of, a technology that we have never heard of, or a language that we have never used, it is even a concept that has never been heard.

In fact, these are not problems. As long as you have a good habit of thinking, a better way of thinking, and a better way to solve problems, you don't have to worry about anything.

You can take a closer look and study. In fact, in the past 10 or 20 years, no new technologies have emerged, and new concepts have emerged. The so-called new technologies are the mining of old technologies, reassemble and apply to new fields to solve new problems from a new perspective. In fact, the fundamental knowledge of those technologies is used.

These technical knowledge includes:

Of course, in addition to the above hard technology, you also need some soft technology. For example, thinking methods, good habits, good tools, good communication, good understanding, good understanding.

The current things are a combination of the above things, or the combination solves new problems, or changes a way of thinking to solve old problems, and so on.

For example. "Cloud computing" is very popular. Cloud computing in various languages and cloud computing in various frameworks. However, if you fall into these languages and frameworks, you can imagine that your energy is exhausted, but you do not have to really understand it, there are even too many frameworks.

This requires us to strengthen our basic theoretical knowledge. The theoretical basis of cloud is distributed. It has been distributed for many years. Now, you can learn and understand the distributed architecture. Distributed + scheduling + server cluster + Communication = cloud, you can see which is new and which is not available before, right!

 

Take socket for example. For example, tcp and udp are used for chatting.

Do not search for python socket code on google first. Let's first recall the socket communication section we have learned, or first look for a book or article about socket communication to see how communication works and processes. Of course, you don't have to finish the whole article. You may not know the details in tcp and udp. It doesn't matter. If necessary, let's take a look later. But at least you can use pseudocode to describe the tcp and udp communication flows, or draw a communication flow on the paper, and use the flowchart to describe the functions you want to implement.

Don't underestimate the pseudocode and flowcharts. These two simple things represent your thinking process, your thinking methods, and the thinking tools you choose. They are the beginning of good habits and must be adhered, until these become your habits.

With these things, others will take a high look at you and think you are more reliable. They will give you more challenging jobs and opportunities for your performance, then you will .............. Everyone understands. All your ideas have the opportunity to be implemented, otherwise they will be blank.

 

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1341144505-0.gif "alt =" "/>

Figure 1 tcp communication Diagram

 

The tcp communication diagram of a socket. We all know that tcp communication requires three handshakes. Tcp is a reliable, connection-oriented, and best-effort transmission protocol, while udp is an unreliable, non-connection-oriented, and hard-to-transmit protocol. But unreliable does not mean it is useless. udp has its own application scenarios. Almost all speech and video are using the udp Protocol. Its reliability is only relative to tcp, however, its advantage is efficiency, which is more important than reliability in some scenarios. This involves trade-off, that is, trade-offs. You need to weigh the pros and cons based on your application and then choose.

After the socket chooses to initialize a tcp socket, it will bind an address and port, and then start listen. After the client connects to the tcp of the listen, the server will accept the request, then a new socket is generated. Both parties use the new socket address and port, and the address is still the listen address above. The port will be a new one, this can be seen from the printed results) for subsequent communication. The original port will continue with the new listen request.

 

The following is the tcpServer code.

 

 
 
  1. import socket 
  2.  
  3. HOST='192.168.0.37' 
  4. PORT=50000 
  5. BUFFER=4096 
  6. sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
  7. sock.bind((HOST,PORT)) 
  8. sock.listen(0) 
  9. print('tcpServer listen at: %s:%s\n\r' %(HOST,PORT)) 
  10. while True: 
  11.   client_sock,client_addr=sock.accept() 
  12.   print('%s:%s connect' %client_addr) 
  13.   while True: 
  14.     recv=client_sock.recv(BUFFER) 
  15.     if not recv: 
  16.       client_sock.close() 
  17.       break 
  18.     print('[Client %s:%s said]:%s' % (client_addr[0],client_addr[1],recv)) 
  19.     client_sock.send('tcpServer has received your message') 
  20. sock.close() 

Socket. SOCK_STREAM is used to specify that the socket is based on the tcp protocol.

The following is the corresponding client code

 

 
 
  1. import socket 
  2.  
  3. HOST='192.168.0.37' 
  4. PORT=50000
  5. BUFFER=4096 
  6.  
  7. sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
  8. sock.connect((HOST,PORT)) 
  9. sock.send('hello, tcpServer!') 
  10. recv=sock.recv(BUFFER) 
  11. print('[tcpServer said]: %s' % recv) 
  12. sock.close() 

 

The following is the udpServer code.

 

 
 
  1. import socket 
  2.  
  3. HOST='192.168.0.37' 
  4. PORT=50001
  5. BUFFER=4096 
  6. sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
  7. sock.bind((HOST,PORT)) 
  8. #sock.listen(0) 
  9. print('tcpServer listen at: %s:%s\n\r' %(HOST,PORT)) 
  10. while True: 
  11.   #client_sock,client_addr=sock.accept() 
  12.   #print('%s:%s connect' %client_addr) 
  13.   while True: 
  14.     recv,client_addr=sock.recvfrom(BUFFER) 
  15.     if not recv: 
  16.        
  17.       break 
  18.     print('[Client %s:%s said]:%s' % (client_addr[0],client_addr[1],recv)) 
  19.     sock.sendto('tcpServer has received your message',client_addr) 
  20. sock.close() 

You will find that because udp is non-connected and does not require three-way handshakes, you do not need to perform listen or accept, so you can directly communicate with each other. When initializing the socket

 
 
  1. socket.SOCK_DGRAM 

To implement the initialization of the socket is based on the udp protocol.

If the udp socket is initialized, no listen or accept is required. You can specify the address and port of the other party when both parties communicate with each other.

Corresponding client code:

 

 
 
  1. #!/usr/bin/env python 
  2. # -*- coding: UTF-8 -*- 
  3.  
  4. import socket 
  5.  
  6. HOST='192.168.0.37' 
  7. PORT=50001 
  8. BUFFER=4096 
  9.  
  10. sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
  11. sock.connect((HOST,PORT)) 
  12. sock.send('hello, tcpServer!') 
  13. recv=sock.recv(BUFFER) 
  14. print('[tcpServer said]: %s' % recv) 
  15. sock.close() 
  16.  

 

After talking about the above, I mainly mean. Developers should strengthen their learning on the theoretical basis, not pursue those languages and frameworks. At least, they should focus more on basic knowledge and have a solid foundation, the superstructure is stable and long-lasting.

For a few people, this has formed their habits. They have recognized this issue as early as possible and have been doing so for so many years.

For most developers, they all encounter problems. Find the syntax, find examples, pull them, change them, debug them, check the effect, debug them again, and then check the effect. The problem may have been solved, but the process is extremely painful. I didn't think about it for a long time before, and I found that development was so boring and boring, every day is a process like this. There is no new idea. When can I finish learning such a multi-language framework? I feel like I have been dragged away, even dragged away, without a day of relief, unless dead.

But it does not matter. Most people come here, and those cool people come here. They just realized this problem early, corrected it early, and entered a fast lane early. As I have always said, developers cannot go beyond several stages, but it is important that you spend more time and energy than others to shorten the time of these stages.

If you recognize it today, do not drag it to tomorrow, or to the next project. Do not drag it to the next module. From now on, start from this project, start from this module, and modify yourself, the subsequent steps will be much more comfortable. I cannot guarantee what you will do later, but I can assure you that you will become more comfortable. Even if you have been coding to retirement, it is not coding in pain, but coding in comfort. No one will say to us, "You are 30 years old and still in coding ......", Because of our coding process, our coding results deserve our age.

 

 

 

This article is from the "breakthrough IT architects" blog, please be sure to keep this source http://virusswb.blog.51cto.com/115214/800209

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.