1

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 )) 

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.