近日,筆者設法創造我的電腦和我的樹莓派的插座,以使兩個設備之間的通信。目前,客戶端能夠自動發送消息到服務器。我想知道,是否可以修改腳本來發送tcp數據包而不是純文本消息,因爲我非常想在將來使用我的PC控制覆盆子pi,而不需要ssh/etc。發送/接收數據包與TCP套接字
我看了一些例子,但我沒有在寫我自己的腳本/代碼太多的經驗,我不是很清楚如何去這樣做。如果有人能夠在正確的方向引導我解釋並儘可能舉例,我將不勝感激。
反正這裏是我目前運行的服務器/客戶端腳本:
客戶:
import socket
import sys
import struct
import time
#main function
if __name__ == "__main__":
if(len(sys.argv) < 2) :
print 'Usage : python client.py hostname'
sys.exit()
host = sys.argv[1]
port = 8888
#create an INET, STREAMing socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print 'Failed to create socket'
sys.exit()
print 'Socket Created'
try:
remote_ip = socket.gethostbyname(host)
s.connect((host, port))
except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit()
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
#Send some data to remote server
message = "Test"
try :
#Set the whole string
while True:
s.send(message)
print 'Message sent successfully'
time.sleep(1)
print 'Sending...'
except socket.error:
#Send failed
print 'Send failed'
sys.exit()
def recv_timeout(the_socket,timeout=2):
#make socket non blocking
the_socket.setblocking(0)
#total data partwise in an array
total_data=[];
data='';
#beginning time
begin=time.time()
while 1:
#if you got some data, then break after timeout
if total_data and time.time()-begin > timeout:
break
#if you got no data at all, wait a little longer, twice the timeout
elif time.time()-begin > timeout*2:
break
#recv something
try:
data = the_socket.recv(8192)
if data:
total_data.append(data)
#change the beginning time for measurement
begin=time.time()
else:
#sleep for sometime to indicate a gap
time.sleep(0.1)
except:
pass
#join all parts to make final string
return ''.join(total_data)
#get reply and print
print recv_timeout(s)
s.close()
服務器:
import socket
import sys
from thread import *
HOST = '' # Symbolic name meaning all available interfaces
PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
s.bind((HOST, PORT))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
#Function for handling connections
def clientthread(conn):
#Sending message to connected client
conn.send('Welcome to the server. Receving Data...\n') #send only takes string
#infinite loop so that function do not terminate and thread do not end.
while True:
#Receiving from client
data = conn.recv(1024)
reply = 'Message Received at the server!\n'
print data
if not data:
break
conn.sendall(reply)
conn.close()
#now keep talking with the client
while 1:
#wait to accept a connection
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#start new thread
start_new_thread(clientthread ,(conn,))
s.close()
我讀了RAW插槽,以及它們如何發送/接收TCP/IP原始數據包,並需要在腳本中手動指定的TCP和IP報頭。將數據包發送到樹莓派來控制它的一些功能會更好嗎? 對不起,如上所述,我沒有太多的套接字和數據包的經驗。我正在運行的腳本是標準套接字,並且「消息」有效載荷通過TCP傳輸,是否可以修改腳本部分以發送數據包(而不是文本數據)來完成這項工作? – intensified
此外,socket.error處理是檢查發送數據中的錯誤的唯一方法嗎?或者有其他方法可以進行錯誤檢查,例如,當服務器收到數據時,它會執行冗餘檢查以確保在傳輸過程中沒有數據丟失? – intensified