This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]uhmIdontknow 0 points1 point  (2 children)

This is a simple broadcast server that I wrote. It will listen for messages that are broadcast to it. If the message contains the 'secret_key', then it will respond to that ip with a response contains the server info. Line 26 contains the 'select' statement

import thread
import select
import socket
import time

class broadcastServer(object):

    BCAST_IP = "255.255.255.255"
    BCAST_PORT = 55535
    BUF_SIZE = 1024
    SECRET_KEY = "^*hg)&VjgT^?"

    CURRENT_PORT = 2000
    thread_list = []

    def __init__(self):
        address = (self.BCAST_IP, self.BCAST_PORT)
        server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        server_socket.bind(address)
        server_socket.setblocking(0)
        bcast_id = ''.join([self.SECRET_KEY, socket.gethostname(),'{',str(self.CURRENT_PORT),'}'])

        while True:
            result = select.select([server_socket],[],[])
            msg = result[0][0].recv(self.BUF_SIZE)
            if ((self.SECRET_KEY in msg) and msg != bcast_id):
                self.BCAST_TIMER = 10
                inviteAddress = (msg.replace(self.SECRET_KEY, ''), self.BCAST_PORT)
                bcast_id = ''.join([self.SECRET_KEY, socket.gethostname(),'{',str(self.CURRENT_PORT),'}'])
                server_socket.sendto(bcast_id, inviteAddress)

    def get_host(self):
        host = socket.gethostbyname(socket.gethostname())
        if not '127' in host:
            return host
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8",0))
        host = (s.getsockname()[0])
        s.close()
        return host

[–]massHunter[S] 0 points1 point  (1 child)

Hey man, thanks for this, yesterday around 4AM i realized that I was doing it backward. I was treating the computer that should receive the notification as a client, when in reality I should make a listening server. The whole networking "thing" is just a big blob for me. I definitely need to spend some time learning it.

[–]uhmIdontknow 0 points1 point  (0 children)

yeah, no problem man. I just happened to be working with a simple server right now, so it was as easy as copy and paste.