Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am relatively new to python, so please be considerate...

i'm implementing a server and a client via raw_sockets. i have the necessary privileges.

now, the server i defined so:

host = socket.gethostbyname(socket.gethostname())
address = (host, 22224)
sockSer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
sockSer.bind(address)
sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
packet, addr = sockSer .recvfrom(4096)   # wait for packet from client

Q1) why can't i simply type: hosts = 'localhost'. if i do so, it doesn't allow me to write the line: sockSer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON). and then the server doesn't receive my client's messages. only when doing gethostbyname(socket.gethostname()) i get 192.168.1.101 and then it works.

in a different class: the client socket:

host = socket.gethostbyname(socket.gethostname())
address = (host, 22224)
sockCli = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)

Q2) do i also need to type: sockCli.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON) or maybe sockCli.connect(address)? seems that it works without the connect command. for the client socket?

now, the problems arise when i do the following: 1) send a packet from client to server:

header=...
payload='a'
sockCli.sendto(header + payload, address)

2) receive packet in server and send something back to client:

while(true):
    data, addr = sockSer.recvfrom(4096)
    header2=...
    payload2='b'
    sockSer.sendto(header2 + payload2, addr)

now, my important question is: Q3) the server sent only 1 packet to client, with payload 'b'. what happens is, my client actually receives 2 packets in the while loop: first packet is what the client itself sent to server, and the other packet is what the client got from the server. hence my output is 'ab' instead of simply 'b' why is this happening???

NOTE: i didn't type the entire code, but i think my syntax,parsing,header composition etc.. are correct. is there an obvious problem in my code? if necessary i'll upload the entire code.

thanks

share|improve this question
add comment

1 Answer

I got this too. my solution is add a judge in the receive code,such as if I send Ping package so I only want ECHO　Reply( type 0 code 0), I write

if type != 0:
     continue

and you also can write as if addr == my_ip: continue

It seems not has any smooth solution

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.3.15.1463\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nos"], "copyright__copyright__copyright": ["\nI got this too. my solution is add a judge in the receive code,such as if I send Ping package so I only want ECHO\u3000Reply( type 0 code 0), I write\n\nif type != 0:\n     continue\n\nand you also can write as if addr == my_ip: continue\n\nIt seems not has any smooth solution\n\nshare|improve this answer\nadd comment\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}