0

I have succesfully made a code which starts a server which can be connected by other devices on my LAN with the CMD command telnet hostname port when the new "client" enters a word and presses enter the word is then sent back to them through the server. My question is if i connected 2 devices to the server how would i get the message to get sent from one to the server then back to the other. Like a messaging programme. The code i have used is shown below

import socket
import sys
from _thread import *
host = ''
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
 s.bind((host, port))
except socket.error as e:
 print(str(e))
s.listen(5)
print('Waiting for a connection.')
def threaded_client(conn):
 conn.send(str.encode('Welcome, type your info\n'))
 final_char = ""
 while True:
 data = conn.recv(2048)
 char = data.decode("utf-8")
 print(data)
 if data != b'\r\n':
 final_char += char
 print(final_char)
 else:
 reply = 'Server output: '+ final_char+"\n"
 if not data:
 break
 conn.sendall(str.encode(reply))
 final_char = ""
 conn.close()
while True:
 conn, addr = s.accept()
 print(addr)
 print(conn)
 print('connected to: '+addr[0]+':'+str(addr[1]))
 start_new_thread(threaded_client,(conn,))
asked Nov 21, 2017 at 20:42

2 Answers 2

1

Not an actual implementation, but some theory:

You need to keep a list of all the clients you have an active connection to:

while ( true )
 client = server.accept()
 clientList.add(client)
 startThread(client)

Then in the thread

while ( true )
 data = connection.receive()
 for client in clientList
 client.sendall( data )
answered Nov 21, 2017 at 20:46
Sign up to request clarification or add additional context in comments.

7 Comments

how do i create a list? which i can add other clients to. would an array work?
Yep, an array should work. I didn't give an actual impl as Python is not my forte.
@J.Fitz It will not work! You are passing client to different threads in your pseudo-code.
it's cool. The only reason i am struggling as server.accept() returns 2 values the ip/port as the first value and the socket as the second e.g. <socket.socket fd=728, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('host', port), raddr=('client ip', client port)>
@J.Fitz you really only need to keep a list of the socket, I think you can get the address from the socket at a later time if you still need it. Or add the addresses to their own list? The theory here is that you need to keep track of all the sockets, which are the streams to the client. You do not have to track the address.
|
1

Take a look at ZQM the python library works perfect and it has already implemented what you need using sockets internally. https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/

Take a look at their publisher / subscriber message pattern.

answered Nov 21, 2017 at 20:55

2 Comments

I have looked at this site and at the publisher / subscriber section but i can't seem to get my head around it. could you explain more further?
Take a look at the example. It's straightforward. learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.