Python console chat with socket - python

I'm writing a simple console chat with server and client. When receiving a message from the first client server should send it to the second client and vice versa. But when first client sends a message to the server it returns back and doesn't reach the second client. Maybe there is a problem in receiving() function.
Here is my client.py:
import socket
from _thread import *
def recieving(clientSocket):
while True:
encodedMsg = clientSocket.recv(1024)
decodedMsg = encodedMsg.decode('utf-8')
print(decodedMsg)
def chat(clientSocket, name):
msg = input()
encoded_msg = f'[{name}] {msg}'.encode('utf-8')
clientSocket.send(encoded_msg)
def main():
serverAddress = (socket.gethostname(), 4444)
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(serverAddress)
name = input('Enter your name: ')
start_new_thread(recieving, (clientSocket,))
while True:
chat(clientSocket, name)
if __name__ == "__main__":
main()
And server.py:
import time
import socket
from _thread import *
def listen(clientSocket, addr):
while True:
encodedMsg = clientSocket.recv(1024)
decodedMsg = encodedMsg.decode('utf-8')
currTime = time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime())
for client in clients:
if addr != client:
clientSocket.sendto(encodedMsg, client)
print(f'[{currTime}] {decodedMsg}')
def main():
serverAddress = (socket.gethostname(), 4444)
global clients
clients = []
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind(serverAddress)
serverSocket.listen(2)
while True:
clientSocket, addr = serverSocket.accept()
if addr not in clients:
clients.append(addr)
print(f'{addr} joined chat')
start_new_thread(listen, (clientSocket, addr))
if __name__ == '__main__':
main()

sendto doesn't work as expected if its socket is connected. It just sends to the connected socket, not the specified address.
Therefore, listen needs to be able to access the open socket of each client in order to write to it.
Currently clients is a list of addresses, but you could change it to a dict of address to socket mappings:
def main():
global clients
clients = {}
Then when you get a new client connection, save address and socket:
clientSocket, addr = serverSocket.accept()
if addr not in clients:
clients[addr] = clientSocket
print(f'{addr} joined chat')
start_new_thread(listen, (clientSocket, addr))
Finally, in listen, write to each other client's socket, not the connected clientSocket for that listen thread:
for client in clients:
if addr != client:
print(f"sending message from {addr} to {client}")
clients[client].send(encodedMsg)
There's a number of other problems with your code.
Sockets are not thread safe. So there is a race condition if 2 clients happen to write the same thing at the same time; the writes could be interpolated and the messages munged up.
If a client disconnects, the server doesn't handle the disconnection well. If the server disconnects, the clients go into an infinite loop as well.

Related

Connection between client and server fails, send/recv doesnt work (python)

I have put together a server and client code to use in a messaging app. When I run the server and starts one client, everything works fine. When I start a second client, I can send messages from the first client and the second client will recieve them. I can send one message from the second client and the first client will recieve this first message. But after this message, the second client can not send or the server can not receive the data for some reason. The first client can still send messages.
I dont know where the mistake is, but I believe either the client can not .send() or the server can not .recv().
(I am quite new to programming so the code might be quite messy and not the most understandeble, and maybe there are several flaws...)
The server code
import socket
from _thread import *
import sys
HOST = "127.0.0.1"
PORT = 12000
client_socket = set()
def threaded(conn):
while True:
try:
data = conn.recv(1024).decode()
if not data:
print("Lost connection")
break
for conn in client_socket :
conn.send(data.encode())
except:
break
print("Gone")
conn.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
print("Server is up and running")
while True:
conn, addr = s.accept()
print("Connected to", addr)
client_socket .add(conn)
start_new_thread(threaded, (conn, ))
The client code
import threading
import socket, sys
HOST = "127.0.0.1"
PORT = 12000
check= ""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
def background():
while True:
answer= s.recv(1024).decode()
if check!= answer and answer!= "":
print(answer)
threading1 = threading.Thread(target=background)
threading1.daemon = True
threading1.start()
while True:
message= input()
if message!= "":
s.send(message.encode())
check = message

Show all the clients connected to a socket server and send them data

I have this simple code:
import socket
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen()
while True:
client_socket, addr = socket.accept()
send = input("Send: ") # but I need a way to send it to all the clients connected
if send == "devices":
# here I'd have a list of all devices connected
client_socket.send(send.encode())
data = client_socket.recv(4096)
print (data)
As I wrote in the comments, I need a way to manage them all in one. How can I do? Maybe with _thread library?
You could mainitain a list of clients that can be passed to an external function that performs an action on all clients.
import socket
host = ''
port = 1000
max_connections = 5
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen(max_connections)
clients = [] # Maintain a list of clients
try:
while True:
client_socket, addr = socket.accept()
clients.append(client_socket) #Add client to list on connection
i_manage_clients(clients) #Call external function whenever necessary
except KeyboardInterrupt:
socket.close()
def i_manage_clients(clients): #Function to manage clients
for client in clients:
client.send('Message to pass')
The above example demonstrates how send data to all clients at once. You could use the
import socket
from thread import *
host = ''
port = 1000
max_connections = 5
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen(max_connections)
try:
while True:
client_socket, addr = socket.accept()
start_new_thread(i_manage_client, (client_socket,addr))
except KeyboardInterrupt:
socket.close()
def i_manage_client(client_socket, addr): #Function to manage clients
client_socket.send('Message to pass')
data = client_socket.recv(4096)
print(client_socket)
print(addr)
print(data)

Error: Transport endpoint is not connected (Python Sockets)

I'm trying to create a simple chat application using sockets in Python (with threads). Application is simple client has to threads one to send data and another to receive. Server has to two threads one to accept client connection and another to broadcast the message. But on running the below code, I'm getting error message
Transport endpoint is not connected
Can anybody tell me why I'm getting this error
Client
import socket, threading
def send():
msg = raw_input('Me > ')
cli_sock.send(msg)
def receive():
data = cli_sock.recv(4096)
print('> '+ str(data))
if __name__ == "__main__":
# socket
cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect
HOST = 'localhost'
PORT = 5028
cli_sock.connect((HOST, PORT))
print('Connected to remote host...')
thread_send = threading.Thread(target = send)
thread_send.start()
thread_receive = threading.Thread(target = receive)
thread_receive.start()
Server
import socket, threading
def accept_client():
while True:
#accept
cli_sock, cli_add = ser_sock.accept()
CONNECTION_LIST.append(cli_sock)
print('Client (%s, %s) connected' % cli_add)
def broadcast_data():
while True:
data = ser_sock.recv(4096)
for csock in CONNECTION_LIST:
try:
csock.send(data)
except Exception as x:
print(x.message)
cli_sock.close()
CONNECTION_LIST.remove(cli_sock)
if __name__ == "__main__":
CONNECTION_LIST = []
# socket
ser_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind
HOST = 'localhost'
PORT = 5028
ser_sock.bind((HOST, PORT))
# listen
ser_sock.listen(1)
print('Chat server started on port : ' + str(PORT))
thread_ac = threading.Thread(target = accept_client)
thread_ac.start()
thread_bd = threading.Thread(target = broadcast_data)
thread_bd.start()
You're using server sockets incorrectly. You cannot recv on server sockets, instead you accept connections on them; accept returns the actual connection socket:
ser_sock.listen(1)
sock, addr = ser_sock.accept()
print('Got connection from {}'.format(addr))
# only this *connection* socket can receive!
data = sock.recv(4096)

How to make a simple multithreaded socket server in Python that remembers clients

How do I make a simple Python echo server that remembers clients and doesn't create a new socket for each request? Must be able to support concurrent access. I want to be able to connect once and continually send and receive data using this client or similar:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = raw_input("Server hostname or ip? ")
port = input("Server port? ")
sock.connect((host,port))
while True:
data = raw_input("message: ")
sock.send(data)
print "response: ", sock.recv(1024)
I.e. with the server running on port 50000, using the above client I want to be able to do this:
me#mine:~$ client.py
Server hostname or ip? localhost
Server Port? 50000
message: testa
response: testa
message: testb
response: testb
message: testc
response: testc
You can use a thread per client to avoid the blocking client.recv() then use the main thread just for listening for new clients. When one connects, the main thread creates a new thread that just listens to the new client and ends when it doesn't talk for 60 seconds.
import socket
import threading
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 1024
while True:
try:
data = client.recv(size)
if data:
# Set the response to echo back the recieved data
response = data
client.send(response)
else:
raise error('Client disconnected')
except:
client.close()
return False
if __name__ == "__main__":
while True:
port_num = input("Port? ")
try:
port_num = int(port_num)
break
except ValueError:
pass
ThreadedServer('',port_num).listen()
Clients timeout after 60 seconds of inactivity and must reconnect. See the line client.settimeout(60) in the function ThreadedServer.listen()

Sending string via socket (python)

I have two scripts, Server.py and Client.py.
I have two objectives in mind:
To be able to send data again and again to server from client.
To be able to send data from Server to client.
here is my Server.py :
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
serversocket.listen(5)
print ('server started and listening')
while 1:
(clientsocket, address) = serversocket.accept()
print ("connection found!")
data = clientsocket.recv(1024).decode()
print (data)
r='REceieve'
clientsocket.send(r.encode())
and here is my client :
#! /usr/bin/python3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host ="192.168.1.3"
port =8000
s.connect((host,port))
def ts(str):
s.send('e'.encode())
data = ''
data = s.recv(1024).decode()
print (data)
while 2:
r = input('enter')
ts(s)
s.close ()
The function works for the first time ('e' goes to the server and I get return message back), but how do I make it happen over and over again (something like a chat application) ?
The problem starts after the first time. The messages don't go after the first time.
what am I doing wrong?
I am new with python, so please be a little elaborate, and if you can, please give the source code of the whole thing.
import socket
from threading import *
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while 1:
print('Client sent:', self.sock.recv(1024).decode())
self.sock.send(b'Oi you sent something to me')
serversocket.listen(5)
print ('server started and listening')
while 1:
clientsocket, address = serversocket.accept()
client(clientsocket, address)
This is a very VERY simple design for how you could solve it.
First of all, you need to either accept the client (server side) before going into your while 1 loop because in every loop you accept a new client, or you do as i describe, you toss the client into a separate thread which you handle on his own from now on.
client.py
import socket
s = socket.socket()
s.connect(('127.0.0.1',12345))
while True:
str = raw_input("S: ")
s.send(str.encode());
if(str == "Bye" or str == "bye"):
break
print "N:",s.recv(1024).decode()
s.close()
server.py
import socket
s = socket.socket()
port = 12345
s.bind(('', port))
s.listen(5)
c, addr = s.accept()
print "Socket Up and running with a connection from",addr
while True:
rcvdData = c.recv(1024).decode()
print "S:",rcvdData
sendData = raw_input("N: ")
c.send(sendData.encode())
if(sendData == "Bye" or sendData == "bye"):
break
c.close()
This should be the code for a small prototype for the chatting app you wanted.
Run both of them in separate terminals but then just check for the ports.
This piece of code is incorrect.
while 1:
(clientsocket, address) = serversocket.accept()
print ("connection found!")
data = clientsocket.recv(1024).decode()
print (data)
r='REceieve'
clientsocket.send(r.encode())
The call on accept() on the serversocket blocks until there's a client connection. When you first connect to the server from the client, it accepts the connection and receives data. However, when it enters the loop again, it is waiting for another connection and thus blocks as there are no other clients that are trying to connect.
That's the reason the recv works correct only the first time. What you should do is find out how you can handle the communication with a client that has been accepted - maybe by creating a new Thread to handle communication with that client and continue accepting new clients in the loop, handling them in the same way.
Tip: If you want to work on creating your own chat application, you should look at a networking engine like Twisted. It will help you understand the whole concept better too.

Categories