简体   繁体   中英

socket.error errno=10022 when using socket.send(packet, (ip, port))

I learned to send packet using socket by Python, but there is an error (socket.error errno=10022) when running the code below on the Windows machine, and everything works fine on the Linux. How can I fix it?

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
except socket.error , msg:
    print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

source_ip = self.source
dest_ip = self.destination

packet = '';
ip_header = self.construct_ip_header()
tcp_header = self.construct_tcp_header()

packet = ip_header + tcp_header
s.sendto(packet, (dest_ip , 0 ))

Error 10022 from Winsock means "Invalid argument" possibly because you're trying to mix SOCK_RAW with IPPROTO_TCP which are incompatible. The third argument is probably being ignored on linux, but windows is complaining about it.

Something is also very wrong with your code sample - you've tried to open a raw socket, but are manually writing tcp and ip headers, but not handling the TCP state machine yourself? This seems highly overcomplicated unless you have some very specialist requirements.

If you're just trying to open a TCP socket between this code and a server I'd suggest to go back the examples at https://docs.python.org/2/library/socket.html#example . The "Echo Client Program" looks like what you're trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM