Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7166751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:27:50+00:00 2026-05-28T14:27:50+00:00

The problem I see with this code is that although it is working, some

  • 0

The problem I see with this code is that although it is working, some of the information clients(players) send to this server will end up at the method ‘permission’ when intended to end up at ‘clientRequests’, because both of them has recvfrom(). So, if I could specify WHICH addr I want information from as a parameter of recvfrom(), it would solve it. I believe it’s possible with C++, but is it with python, and how?
With TCP this problem doesn’t exist but I prefer UDP.

Something like: recvfrom(512, address=(ip, port)) ?

‘permission’ is used to handle new clients connecting.

The problem would be that information like positions, events sent from a client, will not be taken care of at ‘permission’.

class Server:
    def __init__(self):
        #host = '192.168.0.2'
        host = '127.0.0.1'
        port = 50007
        addr = (host, port)
        self.UDPSock = socket(AF_INET, SOCK_DGRAM)
        self.UDPSock.bind(addr)
        self.UDPSock.settimeout(5.0)
        self.searchForClients = True
        self.playersOnline = []
        threading.Thread(target=self.permission).start()
        Gui.add_event("Server online on port %s" % port)


    def permission(self):
        global _status
        while self.searchForClients:
            time.sleep(0.5)
            _status.set("Status: Running; Connected: %s" % len(self.playersOnline))
            try:
                clientMessage, addr = self.UDPSock.recvfrom(1024) # , MSG_PEEK
            except:
                clientMessage = ""
            if clientMessage == "CONNECT" and addr not in self.playersOnline:                       
                if self.searchForClients:
                    self.addNewClient(addr)

            elif clientMessage == "DISCONNECT" and addr in self.playersOnline:
                self.removeClient(addr)

        Gui.add_event("No longer accepting logins")
        return False

    def clientRequests(self, addr):
        latestRequest = time.time()
        while addr in self.playersOnline:
            time.sleep(0.01)
            try:
                data, requestAddr = self.UDPSock.recvfrom(1024)
            except:
                requestAddr, data = "", ""
            if requestAddr == addr:
                latestRequest = time.time()
                dataCommand = data.split(':')
            if time.time() - latestRequest > 2:
                if addr in self.playersOnline:
                    self.removeClient(addr)

        if not self.searchForClients:       
            Gui.add_event("Player %s(%s) forcibly removed" % addr)
        return False
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:27:51+00:00Added an answer on May 28, 2026 at 2:27 pm

    I believe that threads unnecessarily complicate simple UDP servers like this one. Just listen on the socket and dispatch the calls based on whether packet’s source address has already been seen.

    And no, regular recvfrom(2) system call does not allow you to “filter” what address you get data from, only to get that information. Filtering is done with connect(2) on UDP socket, but that limits you to a single source per socket.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: After some more reading I see that this problem is totally general, you
Does anyone see a problem with this, its not working saying bad file descriptor
Here's a coding problem for those that like this kind of thing. Let's see
The problem Although the code about which I will talk here I wrote in
I see this problem on and off again in my PHP coding, and I've
I'm having the following problem and wondered whether anyone could see why this is
This must be a common problem because I see it in many published papers
I've looked at this over and over again and I can't see the problem.
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
This problem has been solved thanks to your suggestions. See the bottom for details.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.