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 4001544
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:55:05+00:00 2026-05-20T07:55:05+00:00

I only get the TCP message when I try this code: from socket import

  • 0

I only get the TCP message when I try this code:

from socket import *
from select import select

def read_tcp(s):
    while True:
        client,addr = s.accept()
        data = client.recv(8000)
        client.close()
        print "Recv TCP:'%s'" % data

def read_udp(s):
    while True:
        data,addr = s.recvfrom(8000)
        print "Recv UDP:'%s'" % data

def run():
    host = ''
    port = 8888
    size = 8000
    backlog = 5

    # create tcp socket
    tcp = socket(AF_INET, SOCK_STREAM)
    tcp.bind(('',port))
    tcp.listen(backlog)

    # create udp socket
    udp = socket(AF_INET, SOCK_DGRAM)
    udp.bind(('',port))

    input = [tcp,udp]

    while True:
        inputready,outputready,exceptready = select(input,[],[])

        for s in inputready:
            if s == tcp:
                read_tcp(s)
            elif s == udp:
                read_udp(s)
            else:
                print "unknown socket:", s

if __name__ == '__main__':
    run()

And the client is like this:

from socket import *

def send_tcp():
    s = socket(AF_INET,SOCK_STREAM)
    s.connect(('localhost',8888))
    data="TCP "*4
    s.send(data)
    s.close()

def send_udp():
    s = socket(AF_INET,SOCK_DGRAM)
    data="UDP "*4
    s.sendto(data, ('localhost',8888))
    s.close()

if __name__ == '__main__':
    send_tcp()
    send_udp()
  • 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-20T07:55:05+00:00Added an answer on May 20, 2026 at 7:55 am
    1. Get rid of the ‘while’ loops in read_tcp() and read_udp(). The select() loop is the only loop you need: it will call the read_XXX() methods as often as required. The read_XXX() methods should handle exactly one event.

    2. Your read_tcp() method should be split into two parts: one to accept a socket and add it to the selection set, and another to read an accepted socket. Adjust the select loop accordingly.

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

Sidebar

Related Questions

I only get this warning if I import my helper class (#import JLHelper.h). An
I need to get only 1 record from sql result. we use SELECT TOP
I'm trying to use this to create a splash screen, but I only get
How can I get only unique departments from the below example? Dept Id Created
I have a TCP socket client receiving messages (data) from a server. messages are
I read data from a TCP socket connection, but the server sends me \0
I need to only get tumblr posts which have been tagged with 'diary' Here
I have an Activity that should only get created once. That is, onCreate can
I tried to recursively get all files and folder list.But I can only get
I have a ticker which uses a jquery and a Repeater.But I only get

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.