0

I'm trying to send data over UDP point to point (looping back to a second NIC) but it is not working. (This will be a one way connection which is why I am using UDP). I'm using Python 2.6.6. Here is the code (from: sending/receiving file UDP in python):

----- sender.py ------

#!/usr/bin/env python

from socket import *
import sys

s = socket(AF_INET,SOCK_DGRAM)
s.bind(("192.168.0.1",9999))
host = "192.168.100.1"
port = 9999
buf =1024
addr = (host,port)

file_name=sys.argv[1]

s.sendto(file_name,addr)

f=open(file_name,"rb")
data = f.read(buf)
while (data):
    if(s.sendto(data,addr)):
        #print "sending ..."
        data = f.read(buf)
s.close()
f.close()

----- receiver.py -----

#!/usr/bin/env python

from socket import *
import sys
import select

host="192.168.100.1" #second nic
port = 9999
s = socket(AF_INET,SOCK_DGRAM)
s.bind((host,port))

addr = (host,port)
buf=1024

data,addr = s.recvfrom(buf)
print "Received File:",data.strip()
f = open(data.strip(),'wb')

data,addr = s.recvfrom(buf)
try:
    while(data):
        f.write(data)
        s.settimeout(2)
        data,addr = s.recvfrom(buf)
except timeout:
    f.close()
    s.close()
    print "File Downloaded"

I can see both interfaces using ifconfig. eth0 is 192.168.0.1 and eth1 is 192.168.100.1. If I try ping -I eth0 192.168.100.1, I see packets sent on eth0 and received on eth1. This seems to confirm the interface works. However, if I run the above python script, I see no packets being sent or received on either interface.

eng3
  • 431
  • 3
  • 18
  • you really need to do something to deal with errors! some sort of sequence number so the receiver knows if it's missing/duplicating/getting out-of-order packets, and/or maybe use something like [FEC](https://en.wikipedia.org/wiki/Forward_error_correction) so it's got a chance of handling data that goes wrong – Sam Mason Jul 15 '19 at 17:10
  • I agree, but I'm losing 100% now. It seems that nothing is being sent. I'd like to figure this out first. – eng3 Jul 15 '19 at 17:12
  • since your last question you seem to be `bind`ing in the sender... are you sure this is doing the right thing? have you got routes set up properly so it knows what to do in this case? – Sam Mason Jul 15 '19 at 17:15
  • also, you can still use `connect()` with UDP, which should give you better errors back from the OS, e.g. about hosts not being reachable, as well as being slightly faster due to not needing to check the hostname on every `send` – Sam Mason Jul 15 '19 at 17:17
  • also, when you say "I see no packets being sent or received on either interface" how are you determining this? do you have `tcpdump`, wireshark, or something similar running? – Sam Mason Jul 15 '19 at 17:21
  • In my last question, I thought it was working but it was going through loopback so I created this. I think I need to bind in both. I'll try connect. I'm not using host names. I didnt think tcpdump would work since I am using UDP. I am just looking at bytes sent/received in ifconfig. – eng3 Jul 15 '19 at 17:23
  • tcpdump is lower level than either UDP or TCP, it'll happily show either, or ICMP packets, ARP resolution and AFAIK basically anything the network card sees (unless use a BPF rule that filters it out) – Sam Mason Jul 15 '19 at 17:29
  • your `bind` in sender also looks wrong, is this a typo when creating the question? you need to pass a tuple, not two parameters. are you sure you're running the version of the code you think you are? – Sam Mason Jul 15 '19 at 17:32
  • sorry that was a typo. I looked at tcpdump and see no activity when the python script is sending. When you mention setting up routes, what do you mean? The only setting I did (in addition to the IPs) was adding ARP entries for each interface. – eng3 Jul 15 '19 at 17:35
  • I tried adding `s.setsockopt(socket.SOL_SOCKET, 25, "eth0"+'\0')` and now I see `Request who-has 192.168.100.1 tell 192.168.0.1` in tcpdump. I do have a ARP table entry for each interface. – eng3 Jul 15 '19 at 18:09
  • From reading this: https://serverfault.com/questions/127636/force-local-ip-traffic-to-an-external-interface/128680 I've tried the instructions and now I can see it sending and receiving data via tcpdump on both interfaces. However, the python receiver still is not receiving anything. – eng3 Jul 16 '19 at 13:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196537/discussion-between-sam-mason-and-eng3). – Sam Mason Jul 16 '19 at 18:25
  • ok I got it to work. I had a typo in the commands that I copied from the other question. – eng3 Jul 16 '19 at 18:59

1 Answers1

0

I think you have the wrong ip in your code.

host="192.0.100.1" #second nic

Your description says the ips are 192.168.100.1 and 192.168.0.1

Whack
  • 133
  • 1
  • 6