lunedì 2 gennaio 2012

Client/Server in UDP tra Android e PC tutto con Python

per scambiare messaggi via rete tra il cellulare Android ed un PC la soluzione piu' semplice e' usare il protocollo UDP.
Di seguito una breve implementazione di prova tutta in Python

--------------------------------- SERVER su PC------------------------

# Server program

from socket import *

# Set the socket parameters
host = "192.168.1.98" #Importante - settare l'indirizzo reale della macchina SERVER
port = 21567
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)

# Receive messages
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"

# Close socket
UDPSock.close()
--------------------------------------------------------------------------------------

---------------------------- Client UDP su Android ---------------------------

import android
import socket
droid = android.Android();


hostname = "192.168.1.98"; # Hostname o IP del server
port = 21567;# Port
data = "";
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
s.connect((hostname,port));
print "Connected!";

data = "test"
exit = False;
while not exit:
    s.sendto(data,(hostname,port)); # Sending command  

--------------------------------------------------------------------------------------

s

Nessun commento:

Posta un commento

FTP Bash script

 Uno script rubato su stack overflow per fare l'upload automatico su FTP senza criptazione   #!/bin/sh HOST='ftp.xxxxx.altervista.or...