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

  • SEARCH
  • Home
  • 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 7524481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:05:59+00:00 2026-05-30T03:05:59+00:00

I’m trying to simulate a token ring using python with sockets, but I have

  • 0

I’m trying to simulate a token ring using python with sockets, but I have run into a problem

main program

node1 = node.node(8081,8082,token)
node2 = node.node(8082,8083,emptyFrame)
node3 = node.node(8083,8084,emptyFrame)
node4 = node.node(8084,8081,emptyFrame)

node1.firstrun()
node1.start()
node2.start()
node3.start()
node4.start()

node

class node(threading.Thread):
    def __init__(self,s,d,frame):
        threading.Thread.__init__(self)
        self.dest = d
        self.current = frame
        self.newframe = frame
        self.source = s

    def firstrun(self):
        time.sleep(1)
        host = "localhost"
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind(("", self.source))
        sendmessage = str(self.newframe.fullFrame())
        s.sendto(sendmessage, (host,self.dest))
        print "sent"

    def run(self):
        host = "localhost"
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind(("", self.source))
        while True:         
            print "running"
            message, addr = s.recvfrom(4096)
            self.newframe = message
            print "received"

In the code, I create 4 nodes giving them different ports and one of them a message. I make this first port send a message to the next node and all of them run the main function. In the main function I wait for a message and do a while loop. The runing gets printed for all 4 nodes but the received does not. Thus I don’t understand why the node never receives the first token. My program just waits infinitely.
(I removed excess code for clarity, none of it should have hindered the main socket process)

  • 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-30T03:06:00+00:00Added an answer on May 30, 2026 at 3:06 am

    you should first dispatch all the threads, otherwise there is no socket listening.
    time.sleep() is bad coding style, you can’t rely on timed functions when it comes to threading.

    dispatch your threads at one point.

    threads = [node1, node2, node3, node4]
    [thread.start() for thread in threads]
    

    and scrape the firstrun() method from your node class and either implement it in a class initnode (it doesn’t need to be a child of threading.Thread) or just send off the message from your main after you dispatched your socket threads.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.