当前位置:网站首页>QQ,微信能聊天都靠它(socket)?
QQ,微信能聊天都靠它(socket)?
2022-06-12 09:18:00 【奥利给技术饭】

说起网络信息交流,那肯定都知道微信,QQ等即时通信软件,那QQ微信等聊天软件是如何实现信息传输的呢?如何实现的呢?这就不得不提本篇的主角“socket”了。

那socket到底是什么呢?它被翻译为“套接字”,是计算机进行交流通信的一种约定或者叫方式,通过socket,两个计算机就可以互相交换数据了。

如下图所示,它处于传输层与应用层之间,是一种抽象存在的东西,也正是它的存在,所以我们在调用TCP/IP, UDP时不需要明白它们的内在原理,直接调用就好了,因为socket已经为你做好了所有的适配工作。

在理解socket之前,我们先来了解网络中进程是如何进行通信的。以下以A电脑和B电脑为例。
首先A电脑和B电脑得需要自己的名字(不知道名字我怎么找到你?),在电脑中,它们有自己的唯一MAC地址,但在网络中,它们需要一个网络中的身份名字,而这个名字就是IP,有了IP之后,AB电脑都有了自己的名字,那接下来就需要建立连接,而链接就需要网络,网络又得按照特定的协议传输,所以就需要一个协议标志如TCP或UDP协议,而在OS网络模型中,传输层用“协议+端口”来唯一标示主机中的应用程序(进程),所以至此就组成了网络传输中的三个必备要素“IP地址,协议,端口”,进程通信完成。

所以在选用协议的过程中,服务端和应用端必须使用同一种协议,否则就无法正常网络通信。

那么socket原理是怎样的?
首先是服务端,创建socket(),绑定好socket与端口,然后开始监听,等待着其他客户端的连接。

然后是客户端,它同样需要创建socket(),然后去寻找指定的服务端IP地址和端口,连接到正处于监听状态的服务端。
服务端收到了客户端的连接请求,至此,服务端和客户端正式建立起网络连接,可以互相发送数据信息。

最后来看一下实例,直接socket安排(这里用的语言是python)
服务端:

客户端:

具体实现原理还需要了解socket内部的一些函数,这个下期会单独详细讲解(由于篇幅过长)。
边栏推荐
- Solve the problem that when unity is opened, you will be prompted that the project has been opened, but you have not opened it before (it may be closed abnormally before)
- Basic SQL syntax i
- Full alphabetic arrangement (pure alphabetic password dictionary)
- Latex common symbols summary
- II Transforming regular expressions into finite state automata: NFA state machine recognizes input strings
- node示例后台搭建
- Test case and bug description specification reference
- Chapter V -[bx] and loop instructions
- Do you know how to improve software testing ability?
- Distributed transaction solution 1: TCC (compensation transaction)
猜你喜欢

Notes on data mining in Tsinghua University (1)
測試用例和bug描述規範參考

Distributed task scheduling
测试计划应该怎么写?一个思路教会你
还在原地踏步,提高软件测试能力的方法你知道吗?
How should the test plan be written? A thought teaches you

Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
自动化测试框架的设计原则有哪些?我帮你总结好了快来看

Distributed transactions - Theoretical Overview

MySQL installation
随机推荐
软件测试基础知识分享,写点你不知道的
剑指 Offer II 016. 不含重复字符的最长子字符串-滑动窗口
Top command meaning
Four steps for sending rockertmq producer messages
Quick sort
Consumer configuration related
Permission modifiers and code blocks
After going to the bathroom, I figured out the ES search server
測試用例和bug描述規範參考
2022 极术通讯-安谋科技纷争尘埃落定,本土半导体产业基石更稳
Database common interview questions are ready for you
Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
MySQL安装
Solution of hmaster process flash back after starting
卖疯了的临期产品:超低价、大混战与新希望
Node sample background setup
How should the test plan be written? A thought teaches you
128. 最长连续序列-哈希表
Meeting time (topology sorting +dp)
consul 配置相关