0

I'm trying to commence chat between 2 RPI3, exactly as shown on https://www.raspberrypi.org/learning/networking-lessons/lesson-1/worksheet/

  1. both devices have unique IP, and successfuly ping each other.
  2. both devices run chat.py successfuly ( one as a server chat.py and a client chat.py 192.168.0.2

BUT text typed on each terminal doesn't go thru.

any ideas why ?

here is the chat.py code :

import network
import sys

def heard(phrase):
  print("them:" + phrase)

if (len(sys.argv) >= 2):
  network.call(sys.argv[1], whenHearCall=heard)
else:  
  network.wait(whenHearCall=heard)

while network.isConnected():
  #phrase = raw_input() #python2
  phrase = input() # python3
  print("me:" + phrase)
  network.say(phrase)

1 Answer 1

0

Server file:

import socket

def Main():
host = "127.0.0.1"
port = 5000

mySocket = socket.socket()
mySocket.bind((host,port))
mySocket.listen(2)
conn, addr = mySocket.accept()
print ("Connection from: " + str(addr))
varz=["var1","var2","var3"]
while True:
        data = conn.recv(1024).decode()
        if data in varz[0] :
           print("YES!")
        print(data.split(' '))
        if not data:
                break
        print ("from connected  user: " + str(data))

        data = str(data[0:2]).upper()
        print ("sending: " + str(data))
        conn.send(data.encode())

conn.close()

if __name__ == '__main__':
  Main()

Client file:

import socket

def Main():
    host = '127.0.0.1'
    port = 0


    mySocket = socket.socket()
    mySocket.connect((host,5000))
    user1=input("type usename:") 
    message = input("%s: "%user1)

    while message != 'q':
            mySocket.send(message.encode())
            data = mySocket.recv(1024).decode()

            print ('Received from server: ' + data)

            message = input("%s: "%user1)

    mySocket.close()

  if __name__ == '__main__':
   Main()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.