21

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?

0

5 Answers 5

21

The problem is in the address of your client:

SERVER_IP   = '127.0.0.1'

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)

...

import time

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect((SERVER_IP,PORT_NUMBER))

while True:
        mySocket.send(b'cool')
        time.sleep(.5)

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

  • OSX/Linux: ifconfig
  • Windows: ipconfig /all
Sign up to request clarification or add additional context in comments.

7 Comments

I used the address at whatismyip.com and it yields the same result
@user1340048: That is your public IP. Unless you have set up your router to forward traffic on a port to your server machine, thats not going to do you much good. Use the local network ips. Are these two machines in two different network locations?
no they are on the same wireless connection. Where do I find the correct local ip? I had tried using SERVER_IP = ''
Then you do want to be using local network ips. It depends on your OS. Look at your network configuration to find your ip. Usually in a control panel or a commmand line tool
@user1340048: Also, setting SERVER_IP="" is the same as saying localhost. Since they are running on two different machines, you need to specify the target IP. On osx/linux a command can be: ifconfig en0. On windows it might be ipconfig /all
|
15

This program is used for sending "small letters string" from the client and getting "capital letters" from the server

Server side

import socket

def Main():
   
    host = '192.168.0.12' #Server ip
    port = 4000

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host, port))

    print("Server Started")
    while True:
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        print("Message from: " + str(addr))
        print("From connected user: " + data)
        data = data.upper()
        print("Sending: " + data)
        s.sendto(data.encode('utf-8'), addr)
    c.close()

if __name__=='__main__':
    Main()

Client side

import socket

def Main():

    host='192.168.0.13' #client ip
    port = 4005
    
    server = ('192.168.0.12', 4000)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((host,port))
    
    message = input("-> ")
    while message !='q':
        s.sendto(message.encode('utf-8'), server)
        data, addr = s.recvfrom(1024)
        data = data.decode('utf-8')
        print("Received from server: " + data)
        message = input("-> ")
    s.close()

if __name__=='__main__':
    Main()

1 Comment

Is there an example with TCP?
1

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:

C:\Users\jackc>ipconfig

Windows IP Configuration

Wireless LAN adapter Wireless Network Connection 3:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::ed97:91a4:9449:204b%13
   IPv4 Address. . . . . . . . . . . : 192.168.8.106
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.8.1

I tried the following test code that works for me.

Client:

#!/usr/bin/env python3

import sys
from socket import socket, AF_INET, SOCK_DGRAM

SERVER_IP   = '192.168.8.102'
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 )
myMessage = "Hello!"
myMessage1 = ""
i = 0
while i < 10:
    mySocket.sendto(myMessage.encode('utf-8'),(SERVER_IP,PORT_NUMBER))
    i = i + 1

mySocket.sendto(myMessage1.encode('utf-8'),(SERVER_IP,PORT_NUMBER))

sys.exit()

Server:

#!/usr/bin/env python3

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.exit()

Comments

0

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.

1 Comment

It still is not working unfortunately and the firewall says that it allows everything.
0

To connect to an arbitrary client you must bind the socket to either socket.gethostname() which is what I'm using with success or use empty string ""

In reference to the server code: We used socket.gethostname() so that the socket would be visible to the outside world. If we had used

s.bind(('localhost', 80)) 

or

s.bind(('127.0.0.1', 80)) 

we would still have a “server” socket, but one that was only visible within the same machine. s.bind(('', 80)) specifies that the socket is reachable by any address the machine happens to have.

source

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.