2012. 7. 18. 15:41

UDP server - client를 구현중 python 3.2 에서는 이상한 에러를 자주 뱉어 내게 된다. 


이이유는 sendto가 2.7에서는 string 으로 전송이 가능하였지만 3.2 부터는 바이트로 전송이 되어야 하기 때문이다.



예제코드는 아래와 같다

[ server ]


 def socketServer():

        svrsock = socket(AF_INET,SOCK_DGRAM)

        svrsock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)

        try:

                svrsock.bind(('',34444))

        except IOError:

                print ("Socket bindding error plz use another port \n")


        while 1:

                try :

                        s,addr = svrsock.recvfrom(100)

                        print(s)

                        svrsock.sendto(bytes(ALTERT_MESSAGE,'ascii'),addr)  //여기가 오류를 뱉어낸 부분이다.


                except IOError:

                        print ('socket sendto error')



[ client ] 


 import socket


HOST = '127.0.0.1'    # The remote host

PORT = 34444              # The same port as used by the server

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.sendto(bytes("aaa",'ascii'), (HOST, PORT))


buff=sock.recvfrom(100)


print(buff)



Posted by k1rha