Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Socket, how do i choose between s.send and conn.send?

def send_Button():
    try:
        myMsg = "ME: " + text.get()
        msg = text.get()
        conn.send(msg) ###
        textBox.insert(END, myMsg + "\n")
        textEntry.delete(0, END)
        textBox.yview_pickplace("end")
    except NameError:
        myMsg = "ME: " + text.get()
        msg = text.get()
        conn.send(msg) ###
        textBox.insert(END, myMsg + "\n")
        textEntry.delete(0, END)
        textBox.yview_pickplace("end")

This program uses the tkinter module with socket in python2.7. My program allows for you to either connect to a server to chat with or host a server for others to connect to you, but whenever I try and test it out then the lines with the '###' on always bring up an error and it doesn't work, the error which comes up is: "NameError: global name 'conn' is not defined" OR "error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied".

Any help please?

like image 955
Blade Avatar asked May 23 '26 02:05

Blade


1 Answers

I think that you are trying to get the program to act as a Client or as a Server just changing s.send() to conn.send() saddly it isn't that simple.

Socket Initializzation

The socket have to be initialized before sending or receiving data.

For a client usually it's something like this.

send_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create the socket
send_socket.connect((serverIp, serverPort))  # Connect to the server
send_socket.send(data)  # Send the data to the server

And like this for a Server:

listen_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create the socket
listen_socket.bind(("0.0.0.0", port)) # Set the socket to accept client from every interface on the port port
listen_socket.listen(1) # Put the server on listen on the port setted before
accept_socket, addr = self.listen_socket.accept() # when a client connect return the socket to talk with it
data = self.accept_socket.recv(buffer_size) # Receive data form the client of max size buffer_size

Docs examples

From your question I guess that with s.send() and conn.send() you are talking about

this example from the python 2.7 socket docs

Here are four minimal example programs using the TCP/IP protocol: a server that echoes all data that it receives back (servicing only one client), and a client using it. Note that a server must perform the sequence socket(), bind(), listen(), accept() (possibly repeating the accept() to service more than one client), while a client only needs the sequence socket(), connect(). Also note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept().

Client

Echo client program

import socket
HOST = 'daring.cwi.nl'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

the client is pretty stright forward, it create the socket s and then after using s.connect() it just send data through it.

Server

The server one is where there there are both s and conn

Echo server program

import socket
HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: 
        break
    conn.sendall(data)
conn.close()

in this one first of all we create a socket s on which the server will listen and then using s.accept() it will wait till a client connect to the server and then return the conn which is the socket of the connected client.

So to receive or send data to the client you have to use conn.

Notes

As said in the documentation in these two example the server accept only one client. So if you want to deal with multiple clients you have to repeat the accept step and possibly generate a new Thread for each client so that other clients don't have to wait for each others.

like image 98
Tommaso Fontana Avatar answered May 24 '26 14:05

Tommaso Fontana