Hi there,
I need a simple socket server which will echo a client message to all connected clients.
I've found this easy py script below, but it only echos the string to the client that sent the message. Any way to mod this so that it will send the message to all clients connected? ...yes, this is probably really simple, but I have very little understanding of python...

http://www.pvmgarage.com/2011/03/a-multi-threaded-persistent-echo-socket-server-in-python/

from socket import *
import threading
import thread

def handler(clientsock,addr):
    while 1:
        data = clientsock.recv(BUFSIZ)
        if not data:
            break
        msg = 'echoed:... ' + data
        clientsock.send(msg)
    clientsock.close()

if __name__=='__main__':
    HOST = 'localhost'
    PORT = 5555
    BUFSIZ = 1024
    ADDR = (HOST, PORT)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.bind(ADDR)
    serversock.listen(2)

    while 1:
        print 'waiting for connection...'
        clientsock, addr = serversock.accept()
        print '...connected from:', addr
        thread.start_new_thread(handler, (clientsock, addr))

Here: http://chaos.weblogs.us/archives/164

There was code, which seemed to work. I paste here as it was normal text and messed up if copy pasting (also it had some stupid semicolons I removed):

Server:

import socket
import time

ANY = '0.0.0.0'
SENDERPORT=1501
MCAST_ADDR = '224.168.2.9'
MCAST_PORT = 1600

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

#The sender is bound on (0.0.0.0:1501)
sock.bind((ANY,SENDERPORT))

#Tell the kernel that we want to multicast and that the data is sent
#to everyone (255 is the level of multicasting)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
while 1:
  time.sleep(10)
  #send the data 'hello, world' to the multicast addr: port
  #Any subscribers to the multicast address will receive this data
  sock.sendto('Hello World', (MCAST_ADDR,MCAST_PORT) )

Client:

import socket
import time

ANY = '0.0.0.0'
MCAST_ADDR = '224.168.2.9'
MCAST_PORT = 1600

#create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)

#allow multiple sockets to use the same PORT number
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)

#Bind to the port that we know will receive multicast data
sock.bind((ANY,MCAST_PORT))

#tell the kernel that we are a multicast socket
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)

#Tell the kernel that we want to add ourselves to a multicast group
#The address for the multicast group is the third param
status = sock.setsockopt(socket.IPPROTO_IP,
                        socket.IP_ADD_MEMBERSHIP,
                        socket.inet_aton(MCAST_ADDR) + socket.inet_aton(ANY))

sock.setblocking(0)
ts = time.time()
while 1:
  try:
      data, addr = sock.recvfrom(1024)
  except socket.error, e:
      pass
  else:
    print 'We got data'
    print 'FROM: ', addr
    print 'DATA: ', data

Also, if you want to get into network programming, it would help to know more about networking in general. Learn the OSI model and how the different layers work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.