I am using these two programs to communicate between two of my computers, one that I am ssh'd into and I am not returning anything on either side. It just runs without sending anything
client
import sys
from socket import socket, AF_INET, SOCK_DGRAM
SERVER_IP = '127.0.0.1'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket( AF_INET, SOCK_DGRAM )
while True:
mySocket.sendto('cool',(SERVER_IP,PORT_NUMBER))
sys.exit()
server
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
while True:
(data,addr) = mySocket.recvfrom(SIZE)
print data
sys.ext()
What could I be doing wrong?
The problem is in the address of your client:
You are connecting to the local machine and sending data, while your server is sitting on a different ip. You need to connect to either the servers ip or hostname.
You can verify this by making the client connect first (and fail if it cant)
Update from comments
Because you are on a wifi connection, that implies that both these machine are on the local network. You need to find the LAN ip address of the server, to specify it as the target.
Command-line approach to finding your IP
ifconfig
ipconfig /all
If this does not work even after changing the SERVER_IP to the real server's address, check whether the firewall on your server accepts traffic for UDP on port 5000.
if your server is a linux machine, iptables -L would show you the firewall rules. iptables -F would delete all(!) firewall rules, so you can test if that helps. this is not reboot persistent.
You should see LAN ip address like this: 192.168.1.102 that are usual WiFi router default local address. For example, you will see following in windows command prompt by using ipconfig:
I tried the following test code that works for me.
Client:
Server: