UDP sokcet

'''
Created on 2013-1-10

@author: kimiz
'''
import socket

if __name__ == '__main__':
    address = ('192.168.1.103', 3610)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(address)
    
    while True:
        data, addr = s.recvfrom(1024)
        if not data:
            print('client has exist')
        print'received', data, 'from', addr
        
    s.close()
'''
Created on 2013-1-10


@author: kimiz
'''
import socket
import struct


if __name__ == '__main__':
    address = ('192.168.1.103', 31500)
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    # send string
    str = '10811234'
    s.sendto(str, address)
    
    # send hex string
    sub_str = ''
    bytes = ''
    while str:
        sub_str = str[0:2];
        i = int(sub_str, 16)
        bytes += struct.pack('B', i)
        str = str[2:]        
    s.sendto(bytes, address)
    
    s.close()

 
 
 
  
  
  
  

你可能感兴趣的:(UDP sokcet)