TCP和UDP协议实例

2022-05-07 09:12:00 by wst

web开发

学过的知识要时常总结下,这样才能变成自己的知识。

TCP协议

TCP协议是建立在IP协议之上的,TCP协议负责在两台计算机之间建立可靠链接,保证数据包按顺序到达。TCP协议会通过三次握手建立可靠链接。

tcp服务端实例

import socket                                           # 导入socket模块

host = '127.0.0.1'                                      # 主机IP
port = 8080                                             # 端口号
web = socket.socket()                                   # 创建socket对象
web.bind((host, port))                                  # 绑定端口
web.listen(5)                                           # 设置最多连接数

print("等待客户端链接。。。")

while True:
    conn, addr = web.accept()                           # 建立客户端链接
    data = conn.recv(1024)                              # 获取客户端请求数据
    print(data)                                         # 打印接收到的数据
    conn.sendall(b'HTTP/1.1 200 OK\r\n\r\nHello World') # 向客户端发送数据
    conn.close()                                        # 关闭链接

代码逻辑:使用socket模块,客户端浏览器向本地服务器发起请求,服务器接到请求,向浏览器发送“Hello World”。

验证:打开浏览器,输入网址:127.0.0.1:8080,成功链接后显示“Hello World”

tcp客户端实例

import socket

s = socket.socket()
host = '127.0.0.1'
port = 8080

s.connect((host, port))
send_data = input("请输入要发送的数据:")
s.send(send_data.encode())
recvData = s.recv(1024).decode()
print('接收到的数据为:', recvData)
s.close()

验证:打开一个命令行窗口,运行服务端的程序;然后再打开一个命令行窗口,运行客户端程序,并输入nihao, 然后就能收到返回的“Hello World”

UDP协议

UDP协议是面向无链接的协议,使用UDP协议时,不需要建立链接,只需要知道对方的IP地址和端口号,就可以直接发数据包。但是,无法保证数据一定到达。

udp服务端实例

逻辑:在客户端输入要转换的摄制温度,然后发送给服务器,服务器根据转化公式,将摄氏温度转换为华氏温度,发送给客户端显示。

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('127.0.0.1', 8888))
print("绑定udp到8888端口")
data, addr = s.recvfrom(1024)
data = float(data)*1.8 + 32
send_data = '转换后的温度(单位:华氏温度):' + str(data)
print('Received from %s:%s.' % addr)
s.sendto(send_data.encode(), addr)
s.close()

udp客户端实例

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
data = input('请输入要转换的温度(单位:摄氏度):')
s.sendto(data.encode(), ('127.0.0.1', 8888))
print(s.recv(1024).decode())
s.close()

 

总结:socket编辑是网络编程的基础,值得好好学学。

 


Comments(130) Add Your Comment

Fweyvq
atorvastatin drug <a href="https://lipiws.top/">buy lipitor 80mg generic</a> buy lipitor 10mg online
Ozwaaq
ciprofloxacin for sale - <a href="https://cipropro.top/augmentin500/">order amoxiclav online</a> buy augmentin 375mg generic
Bsujuj
cipro generic - <a href="https://metroagyl.top/clindamycin/">buy trimethoprim online</a> augmentin canada
Oymend
buy generic flagyl - <a href="https://metroagyl.top/clindamycin/">cleocin price</a> cheap azithromycin 500mg
Fzyiet
ciprofloxacin 500 mg without prescription - <a href="https:/septrim.top/">ciplox tablet</a> erythromycin 500mg pills
Yyalqj
valacyclovir 1000mg for sale - <a href="https://gnantiviralp.com/zovirax/">acyclovir 800mg us</a> order acyclovir pill
Tpxvkd
ivermectin 3mg stromectol - <a href="https://keflexin.top/">buy ciplox paypal</a> where can i buy tetracycline
Lwdptv
metronidazole brand - <a href="https://metroagyl.top/">flagyl usa</a> azithromycin medication
Ibfufy
ampicillin pills <a href="https://ampiacil.top/doxycyclineantb/">monodox drug</a> cheap amoxil
Kramkr
lasix oral - <a href="https://antipathogenc.com/">lasix 100mg us</a> capoten pill
Fjujqt
metformin 500mg uk - <a href="https://gnantiacidity.com/">glucophage cheap</a> buy lincomycin 500mg online
Ptrvoq
buy retrovir online - <a href="https://canadiangnp.com/">how to buy glycomet</a> brand allopurinol
Ofocdk
buy clozaril 100mg pills - <a href="https://genonlinep.com/accupril/">oral accupril 10 mg</a> order pepcid pill
Ncsntu
buy quetiapine generic - <a href="https://gnkantdepres.com/effexor/">order effexor online</a> order generic eskalith
Uhpquq
oral clomipramine 50mg - <a href="https://antdeponline.com/celexa/">oral celexa</a> buy doxepin without a prescription
Fkqatz
atarax 25mg canada - <a href="https://antdepls.com/prozac/">fluoxetine 40mg cost</a> amitriptyline ca
Ljnqhs
augmentin 375mg oral - <a href="https://atbioinfo.com/septra/">where to buy trimethoprim without a prescription</a> cipro price
Khoqav
purchase amoxicillin generic - <a href="https://atbioxotc.com/ceftin/">purchase cefuroxime</a> purchase baycip pills
Wmwqww
order zithromax 250mg without prescription - <a href="https://gncatbp.com/emetronidazole/">order flagyl 200mg pills</a> purchase ciprofloxacin sale
Pwmsxv
order cleocin 150mg online cheap - <a href="https://cadbiot.com/cefixime/">brand suprax 200mg</a> chloromycetin sale
Zuwdej
buy stromectol online uk - <a href="https://antibpl.com/ascefaclor/">brand cefaclor 500mg</a> buy cefaclor 500mg pills
Ysczfv
buy ventolin inhalator generic - <a href="https://antxallergic.com/cvallegra/">fexofenadine 180mg sale</a> buy theophylline 400 mg generic
Irhcwc
methylprednisolone 16 mg oral - <a href="https://ntallegpl.com/fluorometholone/">buy cheap generic fluorometholone</a> buy astelin 10ml online cheap
Bziwzp
desloratadine over the counter - <a href="https://rxtallerg.com/flixotidespray/">order flixotide sale</a> buy albuterol 2mg generic
Hnwhjr
buy glyburide generic - <a href="https://prodeprpl.com/pioglitazone30/">buy generic pioglitazone 15mg</a> dapagliflozin 10 mg without prescription
Layvdb
buy glycomet - <a href="https://arxdepress.com/acarbose50/">acarbose brand</a> buy acarbose 25mg without prescription
Mskhue
order repaglinide 1mg online cheap - <a href="https://depressinfo.com/">repaglinide 1mg cost</a> order empagliflozin 25mg online cheap
Sfyjmp
buy terbinafine online - <a href="https://treatfungusx.com/">lamisil online buy</a> buy griseofulvin online
Qzuqyg
purchase rybelsus sale - <a href="https://infodeppl.com/ddavpspray/">buy desmopressin generic</a> DDAVP price
Irmulp
order nizoral 200mg generic - <a href="https://antifungusrp.com/itraconazole100/">buy itraconazole 100 mg generic</a> order itraconazole without prescription
Flqblv
buy generic famvir - <a href="https://amvinherpes.com/acyclovircream/">buy zovirax 800mg pills</a> valcivir 1000mg drug
Wqmiqj
buy lanoxin 250 mg online - <a href="https://blpressureok.com/furosemidediuret/">lasix 100mg over the counter</a> lasix cost
Dxjygg
order metoprolol 50mg online cheap - <a href="https://bloodpresspl.com/olmesartan/">benicar 20mg cheap</a> adalat 30mg brand
Jbhlxw
microzide 25mg over the counter - <a href="https://norvapril.com/felodipine/">felodipine 5mg brand</a> cost zebeta 5mg
Ruuvjz
nitroglycerin over the counter - <a href="https://nitroproxl.com/indapamide/">buy indapamide 1.5mg pills</a> valsartan 80mg usa
Cwhckh
zocor mingle - <a href="https://canescholest.com/">simvastatin self</a> lipitor boat
Iettry
crestor wretch - <a href="https://antcholesterol.com/">rosuvastatin rabbit</a> caduet pills green
Mgbtgy
buy viagra professional offer - <a href="https://edsildps.com/superavana/">avana safety</a> levitra oral jelly heart
Odlppv
dapoxetine blame - <a href="https://promedprili.com/viagraplus/">viagra plus sooner</a> cialis with dapoxetine near
Cdsdxp
cenforce stain - <a href="https://xcenforcem.com/cialistadalafil/">cialis price</a> brand viagra pills joe
Rpqsjo
brand cialis teeth - <a href="https://probrandtad.com/alprostadilonline/">alprostadil assure</a> penisole bar
Pkcftw
cialis soft tabs pills firm - <a href="https://supervalip.com/cialissuperactive/">cialis super active pills whilst</a>1 viagra oral jelly thomas
Hjlsyv
brand cialis business - <a href="https://probrandtad.com/brandlevitra/">brand levitra glitter</a> penisole legend
Aiqwgs
cialis soft tabs particular - <a href="https://supervalip.com/valifonline/">valif online soar</a> viagra oral jelly online price
Cpizcx
cenforce online side - <a href="https://xcenforcem.com/brandviagrasildenafil/">brand viagra pills computer</a> brand viagra web
Respil
dapoxetine issue - <a href="https://promedprili.com/cialiswithdapoxetine/">cialis with dapoxetine dear</a> cialis with dapoxetine confuse
Dauxas
acne medication lack - <a href="https://placnemedx.com/">acne medication mutter</a> acne treatment design
Xsvqqn
asthma treatment kind - <a href="https://bsasthmaps.com/">asthma treatment thank</a> inhalers for asthma smooth
Aikbdb
uti medication contact - <a href="https://amenahealthp.com/">treatment for uti brick</a> uti treatment axe
Kemmvc
prostatitis treatment eventual - <a href="https://xprosttreat.com/">prostatitis pills fir</a> prostatitis pills suspicion
Hojvuk
valtrex silver - <a href="https://gnantiviralp.com/">valtrex moral</a> valacyclovir pills lantern
Optcpj
loratadine medication case - <a href="https://clatadine.top/">loratadine knife</a> claritin pills volume
Mrxgbg
dapoxetine matter - <a href="https://prilixgn.top/">priligy foot</a> dapoxetine organ
Zegltk
claritin everyone - <a href="https://clatadine.top/">claritin grace</a> claritin mad
Sczzwa
ascorbic acid lower - <a href="https://ascxacid.com/">ascorbic acid walk</a> ascorbic acid demand
Hrgbpr
promethazine trouser - <a href="https://prohnrg.com/">promethazine whether</a> promethazine together
Yfhxdx
clarithromycin pills cellar - <a href="https://gastropls.com/gncytotec/">cytotec permission</a> cytotec pills rough
Xlxrau
fludrocortisone pills open - <a href="https://gastroplusp.com/prlansoprazole/">lansoprazole fierce</a> prevacid pills courage
Xyetlf
buy rabeprazole no prescription - <a href="https://gastrointesl.com/">rabeprazole 20mg without prescription</a> cheap domperidone
Dvybhj
buy dulcolax online - <a href="https://gastroinfop.com/">dulcolax 5 mg for sale</a> liv52 without prescription
Mokluf
cheap bactrim - <a href="https://tobmycin.com/">purchase tobramycin generic</a> cheap tobramycin 10mg
Qbzuno
buy eukroma no prescription - <a href="https://danaterone.shop/">zovirax cream</a> order dydrogesterone online
Uowslo
dapagliflozin drug - <a href="https://forpaglif.com/">buy forxiga 10 mg</a> buy generic precose
Ivmlic
buy fulvicin 250 mg - <a href="https://fulviseoful.com/">buy fulvicin 250 mg</a> buy gemfibrozil pills for sale
Asyquc
order generic dimenhydrinate - <a href="https://actodronate.com/">buy risedronate online</a> buy generic risedronate for sale
Uhdhuz
buy enalapril 5mg for sale - <a href="https://vasolapril.com/">vasotec pills</a> xalatan drug
Sdrlnx
monograph order online - <a href="https://cilosetal.com/">purchase pletal generic</a> order pletal generic
Woovxf
feldene 20 mg us - <a href="https://feldexicam.com/">buy feldene 20mg pills</a> exelon 6mg generic
Lcbgxp
order piracetam 800mg for sale - <a href="https://nootquin.com/secnidazole/">cheap secnidazole 20mg</a> sinemet 10mg without prescription
Knjhnf
buy hydroxyurea without prescription - <a href="https://hydroydrinfo.com/methocarbamol/">robaxin drug</a> buy methocarbamol 500mg pills
Yhtijg
buy depakote 500mg pills - <a href="https://adepamox.com/acetazolamide/">order acetazolamide generic</a> topiramate pills
Kifcuo
norpace pills - <a href="https://anorpica.com/pregabal/">order pregabalin generic</a> buy chlorpromazine cheap
Datcem
cheap cyclophosphamide generic - <a href="https://cycloxalp.com/dimenhydrinate/">buy dramamine 50 mg generic</a> vastarel price
Digmeh
spironolactone cheap - <a href="https://aldantinep.com/dosulepin/">prothiaden brand</a> buy naltrexone 50 mg pill
Bqpejn
buy flexeril 15mg without prescription - <a href="https://abflequine.com/">cyclobenzaprine for sale</a> vasotec 10mg ca
Ftprjq
ondansetron 8mg oral - <a href="https://azofarininfo.com/procyclidine/">buy kemadrin tablets</a> requip over the counter
Hjmqhm
ascorbic acid price - <a href="https://mdacidinfo.com/ferroussulfate/">generic ferrous 100mg</a> order generic compro
Dyccrh
order durex gel online cheap - <a href="https://xalaplinfo.com/">buy durex gel sale</a> buy generic latanoprost
Okersp
cheap rogaine for sale - <a href="https://hairlossmedinfo.com/">minoxidil cost</a> purchase proscar for sale
Nchmpd
arava 20mg pill - <a href="https://infohealthybones.com/alfacalcidol/">brand alfacalcidol</a> buy cartidin medication
Itmyaa
verapamil generic - <a href="https://infoheartdisea.com/">verapamil for sale online</a> buy generic tenoretic
Ctszvd
atenolol 50mg price - <a href="https://heartmedinfox.com/clopidogrel/">buy plavix generic</a> buy coreg 6.25mg for sale
Gaqfty
buy gasex for sale - <a href="https://herbalinfomez.com/">buy gasex pill</a> buy diabecon pills for sale
Sordth
buy lasuna generic - <a href="https://infoherbalmz.com/himcolin/">buy himcolin tablets</a> order himcolin generic
Bqhqbw
cheap noroxin sale - <a href="https://gmenshth.com/flutamide/">oral flutamide</a> where can i buy confido
Kvllzq
buy speman generic - <a href="https://spmensht.com/finasteride/">generic finasteride</a> order fincar for sale
Gttwwv
order finax sale - <a href="https://finmenura.com/">cheap finax</a> buy alfuzosin generic
Yjhegc
terazosin 5mg without prescription - <a href="https://hymenmax.com/tamsulosin/">tamsulosin 0.4mg tablet</a> priligy 60mg price
Nfjdwv
order trileptal sale - <a href="https://trileoxine.com/pirfenidone/">pirfenex for sale online</a> cheap levothroid online
Qhzcbd
buy duphalac without a prescription - <a href="https://duphalinfo.com/">purchase lactulose bottles</a> betahistine 16 mg ca
Plnbbq
imusporin eye drops - <a href="https://asimusxate.com/gloperba/">colchicine 0.5mg brand</a> colcrys 0.5mg cheap
Qrseot
purchase deflazacort without prescription - <a href="https://lazacort.com/">buy deflazacort pills for sale</a> buy alphagan medication
Kwurvg
buy besivance sale - <a href="https://besifxcist.com/">purchase besifloxacin sale</a> buy sildamax online cheap
Nlkzeh
gabapentin 800mg brand - <a href="https://aneutrin.com/">purchase neurontin for sale</a> purchase azulfidine generic
Swdkvv
benemid 500 mg over the counter - <a href="https://bendoltol.com/">order benemid 500 mg for sale</a> buy carbamazepine online cheap
Cnrbmv
mebeverine tablet - <a href="https://coloxia.com/etoricoxib/">order etoricoxib 60mg online cheap</a> order pletal 100 mg sale
Nfnlgg
cheap celecoxib - <a href="https://celespas.com/">buy celecoxib 200mg generic</a> indocin 50mg price
Ybngbl
where can i buy diclofenac - <a href="https://dicloltarin.com/">order voltaren 100mg online cheap</a> generic aspirin 75 mg
Xkgxkb
rumalaya brand - <a href="https://rumaxtol.com/shallaki/">purchase shallaki sale</a> purchase amitriptyline generic
Yecwfy
cheap mestinon - <a href="https://mestonsx.com/azathioprine/">buy imuran 25mg for sale</a> imuran order
Wbwjcl
buy diclofenac without a prescription - <a href="https://vovetosa.com/">cheap diclofenac</a> nimodipine brand
Tvyvae
order baclofen 25mg for sale - <a href="https://baclion.com/nspiroxicam/">purchase feldene</a> buy feldene 20 mg for sale
Wmavrm
order mobic - <a href="https://meloxiptan.com/rtrizatriptan/">order toradol online cheap</a> order toradol 10mg generic
Wgxqql
order periactin 4mg online cheap - <a href="https://periheptadn.com/">order cyproheptadine 4mg sale</a> zanaflex canada
Ebkhdg
artane online buy - <a href="https://voltapll.com.com/">order artane for sale</a> order diclofenac gel sale
Qwahcj
omnicef 300mg tablet - <a href="https://omnixcin.com/clindamycin/">cheap clindamycin</a> cleocin brand
Kppfoc
buy generic isotretinoin online - <a href="https://aisotane.com/asdeltasone/">deltasone 5mg us</a> buy generic deltasone 10mg
Eowxhv
purchase prednisone for sale - <a href="https://apreplson.com/prednisolone/">purchase prednisolone pill</a> buy permethrin for sale
Qqglsc
acticin online - <a href="https://actizacs.com/tretinoin/">tretinoin buy online</a> buy tretinoin tablets
Zehtpe
buy betamethasone cream for sale - <a href="https://betnoson.com/benoquincre/">monobenzone buy online</a> monobenzone oral
Xwuean
flagyl order - <a href="https://cenforcevs.com/">cenforce 100mg usa</a> order cenforce 100mg without prescription
Jodtte
augmentin 625mg cost - <a href="https://alevonted.com/">order synthroid 100mcg pills</a> buy levothroid without prescription
Uxensk
buy generic cleocin for sale - <a href="https://indometnp.com/">indomethacin 75mg uk</a> buy indocin 50mg capsule
Xucwlm
generic losartan - <a href="https://cozartan.com/">order losartan pills</a> brand cephalexin 500mg
Gxyykr
order eurax without prescription - <a href="https://aeuracream.com/caczone/">aczone buy online</a> aczone sale
Hosjuf
bupropion 150 mg canada - <a href="https://bupropsl.com/orlistat/">order orlistat generic</a> purchase shuddha guggulu generic
Zbxlyr
modafinil 100mg pills - <a href="https://sleepagil.com/melatonin/">melatonin 3mg us</a> buy meloset paypal
Ueoibf
progesterone 100mg ca - <a href="https://apromid.com/clomiphene/">order clomiphene 50mg generic</a> fertomid pill
Vlrlrn
xeloda online - <a href="https://xelocap.com/naproxen/">order naproxen 500mg generic</a> purchase danocrine generic
Zkhptt
buy generic fosamax 70mg - <a href="https://pilaxmax.com/medroxyprogesterone/">buy provera medication</a> medroxyprogesterone usa
Mmwnwy
dostinex 0.25mg uk - <a href="https://adostilin.com/cabgolin/">order cabgolin generic</a> order alesse without prescription
Kodklc
purchase estrace generic - <a href="https://festrolp.com/ginette-35/">order ginette 35 pills</a> buy arimidex sale
Dhdteb
バイアグラ の購入 - <a href="https://jpedpharm.com/">シルデナフィル どこで買える</a> タダラフィル 副作用
Dtsamo
プレドニン 海外通販 - <a href="https://jpaonlinep.com/jazithromycin/">アジスロマイシンの飲み方と効果</a> 正規品ジスロマック錠の正しい処方
Cdnklb
eriacta evidence - <a href="https://eriagra.com/">eriacta find</a> forzest fill
Kmycbq
indinavir medication - <a href="https://confindin.com/">order indinavir</a> purchase emulgel online cheap
Frldne
valif online remind - <a href="https://avaltiva.com/awsustiva/">sustiva 20mg brand</a> purchase sinemet
Tgxcir
where to buy provigil without a prescription - <a href="https://provicef.com/cefadroxil/">cefadroxil for sale</a> combivir pill
Yurbhb
ivermectin australia - <a href="https://ivercand.com/">stromectola online</a> carbamazepine oral
Bvsikw
cheap promethazine - <a href="https://prometacin.com/jilincomycin/">purchase lincomycin for sale</a> buy lincocin medication