Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basics of Socket

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the basic knowledge of Socket, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

How to master the knowledge related to Socket step by step.

What is Socket?

Everyone uses computers to surf the Internet, and when we visit the https://www.unixhot.com of the operation and maintenance community, our computers and the servers of the operation and maintenance community will create a Socket, which we call a network socket. Well, since it is network communication, it must be in pairs. There is at least one client and server, which we call a socket pair.

A socket pair (socket pair) is a quintuple that defines the two endpoints of the network connection, including:

Source IP address

Source port

Destination IP address

Destination port

Type: TCP or UDP

So for HTTP requests, we know that the underlying layer is a Socket of TCP, then the socket pair of TCP is a quad, because the protocol has been determined:

1. Source IP address, 2. Source port, 3. Destination IP address, 4. Destination port.

Random port of the client

In order to have a more intuitive understanding of this TCP Socket, let's do a small experiment. I have prepared two servers here:

Role

IP address

Port

Client

192.168.56.11

Random

Server side

192.168.56.12

9999

When the client 192.168.56.11 accesses port 9999 of 192.168.56.12, a random port is selected for communication, so what range does this random port come from? there is always a range that cannot be infinite.

So for TCP sockets, how many ports can there be for a client's IP address? Because the TCP protocol header uses 16 bits to hold the port number, the maximum number of ports is 65536, 2 ^ 16 = 65536.

Yes, it's 65536. But why do we often see on the Internet that the maximum number of available ports is 65535, that is, 2 ^ 16-1? Because the port number starts at 0, 0-65535 means 65536. Port 0 is reserved, and neither TCP nor UDP need to be used. Of course, this is the standard. Can you listen to port 0 or not? I will use a python script to listen to the local port 0 to try.

[root@test ~] # catbind_port_zero.py

#! / usr/bin/env python

#-*-coding: utf-8-*-

'' this script listens on local port 0 of 127.0.0.1

Explore the mysteries of port 0

Import socket

Def bind_port_zero ():

Ss = socket.socket (socket.AF_INET,socket.SOCK_STREAM)

Ss.bind (('127.0.0.0, 0))

Addr, port = ss.getsockname ()

Ss.close ()

Print (addr, port)

Bind_port_zero ()

Execute the script to see if you can listen properly:

[root@test ~] # pythonbind_port_zero.py

('127.0.0.09, 53692)

[root@test ~] # pythonbind_port_zero.py

('127.0.0.09, 59444)

It can be found that it can be monitored normally, but port 0 is not listening. Experiments show that under Linux, if port 0 is specified when bind, then the system randomly selects an available port to bind.

Well, now that we know the range of ports is 0-65535, how much can we use as a client to access other servers? Not all of this range can be used. So under Linux, we can get the local random port range as follows:

[root@test ~] # cat/proc/sys/net/ipv4/ip_local_port_range

32768 61000

Don't be surprised that the answer is indeed 32768 to 61000. Now you should understand what other people mean by sending 100000 concurrent stress tests. At least it cannot be achieved by default. Is it enlightening after reading this sentence? It's not impossible.

Is the bottleneck really only a random port range?

As we just saw, we access other servers, as clients, we have to use a random port, 32768-61000, which seems to be quite a lot, of course, you can also modify it to expand the range of random ports. For example, when we use Nginx for reverse proxy load balancer, the client and Nginx establish Socket for communication, and Nginx also needs to establish Socket for communication with the real backend server. In high concurrency scenarios, this random port must be a bottleneck. But is it really only the random port range that is the bottleneck? Next we use the ab command to conduct a stress test on Baidu.

Ab is a performance testing tool for Apache, which can simulate concurrency for Web performance testing. Under CenotOS, you can install it as follows:

[root@test ~] # yuminstall-y httpd-devel

According to our previous understanding, random port 61000-32768mm 28232, so the machine I am experimenting with is a newly installed system with no network transmission, even if there is, it should be no problem for us to create 20, 000 socket pairs. Is this really the case? We use experiments to prove:

We simulate sending 20, 000 requests and 2000 concurrency to test Baidu:

[root@test] # ab-n 10000-c 2000 https://www.baidu.com/

This isApacheBench, Version 2.3

Copyright 1996Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to TheApache Software Foundation, http://www.apache.org/

Benchmarkingwww.baidu.com (be patient)

Socket: Toomany open files (24)

That's impossible. why was it wrong? Don't worry, it's easy for us to read socket: Too many open files (24).

Cannot open too many files. Let's use ulimit to look at system resource limitations.

[root@test] # ulimit-a

Core file size (blocks,-c) 0

Data seg size (kbytes,-d) unlimited

Scheduling priority (- e) 0

File size (blocks,-f) unlimited

Pending signals (- I) 31219

Max locked memory (kbytes,-l) 64

Max memory size (kbytes,-m) unlimited

Open files (- n) 1024

(omit part of the output)

Yes, by default, the maximum number of files that current users can open is 1024, but what does this have to do with using ab testing? Didn't the ab test create a socket? If you don't understand, then go back to the essence and think about the phrase "everything is a file" that we often heard when we were just learning Linux. Who says socket is not a file?

I'm sure you know what to do, you can use ulimit-n to modify the current user, the current session restrictions, or you can modify the configuration file / etc/security/limits.conf to completely solve this problem, which is also a necessary basis for system performance tuning.

Create a TCP Socket

OK, it was just an episode. Let's continue to explore TCP Socket. All talk and no practice is a gavel. Let's create a socket pair to see:

Server:

First, we use the nc command on 192.168.56.12 to listen on port 9999.

[root@192.168.56.12] # nc-l-4-p 9999-k

[root@192.168.56.12 ~] # netstat-ntlp | grep 9999

Tcp 0 0 0.0.0 0 9999 0.0.0 0 * LISTEN 26789 area

Client:

On the client side, the nc command is also used to connect to port 9999 on the server side.

[root@192.168.56.11 ~] # nc 192.168.56.12 9999

OK, now you can enter any language on the client and have a pleasant chat on the server? But that's not the point.

View Socket

Let's first look at the TCPSocket on the client side.

[root@192.168.56.11 ~] # netstat-na | grep 9999

Tcp 0 0 192.168.56.11:11525 192.168.56.12:9999 ESTABLISHED

TCP Socket on the server

[root@192.168.56.12 ~] # netstat-na | grep 9999

Tcp 0 0 0.0.0.0 9999 0.0.0.015 * LISTEN

Tcp 0 0 192.168.56.12:9999 192.168.56.11:11525 ESTABLISHED

I believe you have really understood Socket, and all that is left is endless imagination. Remember TIME_WAIT? If there is a large amount of TIME_WAIT, then the socket pair will not be released, and if it is not released, it will take up one, the resource will be one less. How to optimize it? Let's listen to the next decomposition!

However, if you really understand the concept of Socket, you already have the ultimate solution. Since a TCP Socket is a quad, what if my machine has multiple IP addresses? Haha, this is the finishing touch, you know!

Use pseudo terminals to send data

Finally, leave a small color egg. In addition to using nc to send data, Linux also provides a way called pseudo-device. Let's try the tcp pseudo-device under / dev. Many pseudo devices are provided under / dev, such as tcp, which can be used to access remote ports directly.

[root@192.168.56.11 ~] # echo "886" > / dev/tcp/192.168.56.12/9999

Hurry up to see if the server has received 886.

These are the basic knowledge of Socket, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report