Skip to content

Instantly share code, notes, and snippets.

@gooooloo
Last active September 18, 2015 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gooooloo/9550707 to your computer and use it in GitHub Desktop.
Save gooooloo/9550707 to your computer and use it in GitHub Desktop.
Understanding network programming as mail system

Understanding network programming as mail system

用写信的方式理解网络编程

2014/3/14

I have NO experience on network programming at all. Recently I learnt something, they are interesting. I am trying to summarize what I have learned as my own understanding way -- the mail system. Special thanks to Barteur Zhou.

我以前从没有过网络编程的经验和知识。最近学了一些,还挺好玩的。在这里,我想试试用自己的理解方式来总结下我近来所学的知识 -- 用写信的方式来类比。特别感谢 Barteur Zhou。

ip & port for net

  • ip is like a building
  • port is like the room, or the roon number.

网络上的 ip 和端口

  • ip 就像是一栋楼。
  • 端口就是楼里的一个个房间。

ProcessID for local machine

  • we are in the same ip now.
  • PID is like the room -- just like port on net.

本机上的进程 ID

  • 本机就像一栋楼,所有进程都在这栋楼里(同个ip)
  • 进程就是一个个房间。进程 ID 就是一个个房间门牌号。

listening to a port (on its host machine)

a process can listen to a port on its host machine. It is just like the process installs a mail box on its door of room. Any one can send a mail to the mailbox (remember, you know its ip & port previously, right? then you already know its address, says building and room.) And it is promissed whenever the mail reaches its mailbox, the process will immediatly get it.

监听端口

一个进程可以在它所在机器上监听一个端口。就好像,这个进程在这栋楼的某个房间安装了一个收信箱。任何人都可以给这个信箱发信。(还记得吗?你之前已经知道这个进程的 ip 和端口了,也就是说,你已经知道他的楼和房间了。)而且这栋楼的物业保证,一旦这封信被送达到该信箱里的时候,那个进程就会马上看到这封信。

socket

just like 2 processes in each own room, sending mails to each other (they have to know each other's building & room first). The socket only handle the mail delivery issue, but in order to complete a meaningful communication, the 2 processes have to agree on how they will organize their content format -- it is called protocal. sometimes, you can assign a specific postman for delivering all of the mails between these 2 processes.

socket

就好像,2个进程在各自的房间,给对方来来回回的发信。Socket只负责信件的传递。为了两个进程能进行有意义的对话,他们必须先协商好信件内容的组织方式,比如,用什么语言、语法、格式啊什么的。。。这个组织方式,叫做 protocal。有时,你可以专门指派一个邮寄员专职负责这2个进程的所有邮件送递业务。

http

a special, popular protocal when 2 processes sending mails to each other. Just like you fill some information in a public printed table, send it out, once the receiver receives the mail, they can just look up the content in the table directly. Limitation: the table sometimes is not so simple, and no specific postman.

http

一个特殊而流行的信件内容的 protocal,用于2个 process 跟彼此通信之用。就好像一个表格,你只需要按照这个表格里规定的内容一个一个填好,寄出去,对方收到后按照该表格的栏目一栏一栏读信息。限制:有时填表格很麻烦。。。

browsing http url

actually 2 processes are used but may not be realized by user.

  • a http server process is on server side.
  • a browser process is on the client side. when you click the "enter" or "go" button on browser, the browser process parses the target building and room first, then sending mail to that room with speciall message: give me your thing the url specifies. The server side's process will recieve this mail, and parse the url to know what to give, and send back the thing, usually html files or plaintext files or binary files. Once the browser receives the *.html files, it shows it to users, or download it to disc.

浏览 http 的 url

实际上有2个进程参与了整个过程,不过通常用户没有意识到。

  • 服务器端有一个 http server 程序。
  • 客户端有一个浏览器。 当你在浏览器地址栏里输完url,点击前往或回车的时候,浏览器进程会从 url 里分析出目标在哪栋楼哪个房间,然后往那里寄一封信:“嘿,请给我 url 里指定的东西。” 对方收到信后,从 url 里分析出要给什么东西,然后寄回来。浏览器收到对方的回信后 -- 通常会是 html 文件或者文本文件或者二进制文件 -- 再进一步呈现给用户,或者下载到磁盘。

submit http header/data to server from browser

also, 2 processes involved, server process and client browser.

  • browser side fill the "http" table content.
  • send out.
  • server side receives it, knowing it is not url-like thing, but the table-like thing, then get information from the table in the mail. and later handling.

从浏览器给服务器发送 http 表头或数据

同样,2个进程牵涉在内,http server 和 浏览器。

  • 浏览器在 http 表格里一栏一栏填好信息。
  • 寄出去。
  • http server 收到信后,看到这个是表格而不是 url, 便从表格里读出数据,然后处理。

getting http header/data on browser from server

again, 2 processed involved, http server process and client browser.

  • http server process send out mail with the table format.
  • client process looks up the informations from the table received.

从服务器端发送 http 表头或数据给浏览器

还是一样,2个进程牵涉在内,http server 和浏览器。

  • http server 进程在 http 表格里填好内容。寄出。
  • 浏览器从表格里读信息。

websocket

also a kind of socket, but more reliable, delivery quality is more garanteed.

websocket

一种socket,但是服务更好,可靠性更有保证。就好像,你用 socket 可能会用一般的平邮,我用websocket却相当于使用了挂号信或者顺丰快递。

socket.io

a very specail format for the mail format. It is based on node.js. Basically you can imagine a servant knowing the mail format but you dont know, the servant will read the mail for you, and ask you : "for this kind of thing, the content is like this, what do you want to do with it?". You just need to answer it.

socket.io

一种很特殊的信件格式和传递方式。基于 node.js。基本上可以想象你有一个仆人专职帮你收信,读信,写信和寄信。收到信时他会告诉你:“主人,对方说关于某某某事情,情况是怎样怎样的,您可以自己进行后续处理。”你处理完了,或者像主动传递新词出去,你也只能跟这个仆人说:“我要告诉对方,关于某某某事情,情况是这样这样的。”仆人会帮你写好信并寄出去。当然了,对方也必须要使用同一个体系的仆人,这样他们才可以读懂另一个仆人所写的信件(或许他们是用的火星语呢?谁知道呢!反正我是主人,只在乎消息被传达,不用关心是怎么传达的。)

The End.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment