简体   繁体   中英

Socket programming, attributeerror '_socketobject' object attribute 'bind' is read-only

I'm trying to make a project where i send messages to my rpi from an android phone using sockets and print it on the screen. The raspberry pi acts as the server while the android device is the client. The code i used is off the python website and is

import socket
import sys

# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('192.168.1.108', 8080)
server_socket.bind(server_address) 
server_socket.listen(1)
while True:
    # Find connections
    connection, client_address = server_socket.accept()
    try:
        data = connection.recv(1024)
        print data

    except:
        connection.close()

But when i run it on my terminal window it shows the error "AttributeError '_socketobject' object attribute 'bind' is read-only" Could someone tell me what this error is and how do i resolve it.

You have a hard coded ip address in there. Probably the ip address of the raspberry pi. The windows machine will not have the same ip address.

You can use the special value 0.0.0.0 to bind to the current machines ip address, whatever it might be.

server_address = ('0.0.0.0', 8080)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM