importsocket
host = '
127.0.0.1
'port = 9999
#建立乙個tcp socket套接字
tcp_server =socket.socket(socket.af_init, socket.sock.stream)
#繫結ip,port
tcp_server.bind((host, port))
#設定最大連線數
tcp_server.listen(5)#監聽
while
true:
client, address =tcp_server.accept()
"received from %s:%d
" %(address[0], address[1])
#設定接收位元組
request = client.recv(1024)
'receive data %s
' %request
#傳送接收成功通知
client.send('
ack!')
#關閉client.close()
乙個簡單的tcp服務端
importsocket
target_host = '
127.0.0.1
'target_port = 9999
#建立乙個tcp socket套接字
tcp_client =socket.socket(socket.af_init, socket.sock_stream)
#連線到目標
tcp_client.connect((target_host, target_port))
#傳送資料
tcp_client.send('
test')
#接收來自伺服器的資料
response = tcp_client.recv(1024)
response
tcp_client.close
乙個簡單的tcp客戶端
importsocket
host = '
127.0.0.1
'port = 9999udp_server =socket.socket(socket.af_inet, socket.sock_dgram)
udp_server.bind((host, port))
'..waiting for message..
'while
true:
data, address = udp_server.recvfrom(1024)
'received data %s for %s:%d
' %(data, address[0], address[1])
udp_server.sendto(
'success
', address)
udp_server.close()
乙個簡單的udp服務端
importsocket
target_host = '
127.0.0.1
'target_port = 9999client =socket.socket(socket.af_inet, socket.sock_dgram)
client.sendto(
"aaabbbccc
", (target_host, target_port))
data, addr = client.recvfrom(4096)
data
print addr
client.close()
乙個簡單的udp客戶端
最大的區別在於是否需要建立連線,tcp是可靠的需要連線,而udp是不可靠的不需要連線,其他的都比較類似
SpringBoot Swagger簡單使用
io.springfox springfox swagger2 2.9.2 io.springfox springfox swagger ui 2.9.2 import org.springframework.context.annotation.bean import org.springfram...
UICollectionView的簡單使用
所屬controller要遵循三個協議 uicollectionviewdatasource,uicollectionviewdelegate,uicollectionviewdelegateflowlayout uicollectionviewflowlayout flowlayout uicol...
BGAQRCode Android的簡單使用
compile com.google.zxing core 3.2.1 xmlns android 這句是使用選擇器框架時用到的 xmlns tools package com.huaqiang.zxingdemo 選擇框架用到 android name android.permission.cam...