0

I am running a socket program in python to communicate with an Embedded Device. I am able to send the packet and get the response immediately, which I can verify it through Wireshark. The problem I am not able to capture it in the my code, but it I am able to see the response in the Wireshark. Below code has send and receive combined. If I split test and I am able to see receive the acknowledgement in the application itself. I suspect the response is very fast to such an extent my application is missing it. Any help is appreciable.

import socket
import select
from binascii import unhexlify
from threading import Thread
import time


def client():
    UDP_IP_ADDRESS = "192.168.1.10"
    UDP_PORT_NO = 4010
    clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM  ) 
    clientSock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    packetToSend=""

    messageToSend = raw_input("Message to Send :")

    # if messageTOSend too big send as multiple  packets
    packetsToSend = addpack(messageToSend)

    for count, currentPacket in enumerate(packetsToSend):

        print "\nData Packet %d:"%count

        print currentPacket  
        packetToSend = "".join(map(chr, currentPacket))
        clientSock.sendto(packetToSend, (UDP_IP_ADDRESS, UDP_PORT_NO))
        time.sleep(1)
    print "packets sent"      

def server():
    UDP_IP_ADDRESS = ""
    UDP_PORT_NO = 4011
    print "waiting for data"
    serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    serverSock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)

    serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
    while True:
        data, addr = serverSock.recvfrom(1024)
    time.sleep(5)
    print "Message: ", data



if __name__=='__main__':

    a = 0
    try:
        Thread(target=server).start()
        Thread(target=client).start()
    except Exception, errtxt:
        print errtxt

1 Answers1

0

Your program works fine, at least the server socket. You just decide not to do anything with the data you receive.

while True:
    data, addr = serverSock.recvfrom(1024)
    print "Message: ", data

would solve your problem and you would receive the message. Now you just receive in your while loop but the loop never breaks.

Hannu
  • 11,685
  • 4
  • 35
  • 51