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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:28:46+00:00 2026-06-12T09:28:46+00:00

simple client-server socket studying code,the server-end is: import socket,select,time s = socket.socket() host =

  • 0

simple client-server socket studying code,the server-end is:

import socket,select,time

s = socket.socket()

host = socket.gethostname()
port = 1234
s.bind((host,port))
s.listen(50)
s.setblocking(0)              # (1)

fdmap = {s.fileno():s}

p = select.epoll()
p.register(s)
while True:
    events = p.poll()
    for fd,event in events:
       if fd is s.fileno():
           c,addr = s.accept()
           c.setblocking(0)                    #(2)
           print('Got connection from: ',addr)
           p.register(c)
           fdmap[c.fileno()] = c 
       elif event & select.EPOLLIN:
           data = fdmap[fd].recv(1024)
           if not data:
               print('disconnected')
               p.unregister(fd)
               del fdmap[fd]
           else:
               print(data)

and the client-end is

import socket,time

s = socket.socket()

host = socket.gethostname()
port = 1234

s.connect((host,port))
print(s.send(b'hello server'))

My questions are:
1.AFAIK in the IO-multiplexing model,epoll.poll would block until one socket become available,so when operates on it,it shouldn’t be block(should return directly),then why we should call setblocking(0) in (1),(2),what’s the purpose? I tried put them off,it still works

2.when running,the client-end can be slow,it outputs 12(which is the bytes it send to server) after about 1s,I profiling it by use time.time() one by one statement,then found that the s.connect((host,port)) usually take about 1s,where am i wrong?(This happens on poll/epoll,when use select,the client-end goes very fast)

  • 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-06-12T09:28:47+00:00Added an answer on June 12, 2026 at 9:28 am

    If you check the documentation you will see that the epoll poll function takes a timeout parameter, which defaults to -1 which means wait indefinitely. If you set the timeout to zero it will return immediately.

    As for the delay in connect it’s probably because connect has to do a DNS lookup of the hostname to find the IP address.

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

Sidebar

Related Questions

i have written simple client server socket programming code as follow MyServer.java import java.io.DataInputStream;
I have a very simple client server code written java(server listens on some port
Simple scenario: Client connects to server with socket.io ( socket = io.connect(...) ) Server
I have created my web socket server and client using this simple tutorial here
I'm trying to get a simple socket.io server running: Client: <script src=http://www.my-server.com:8080/socket.io/socket.io.js></script> var socket
Say I have a simple client/server scenario with one method: // client code $client
I have simple client/server SSL code which worked fine on Python 3.2. However, I
Based on examples here , the simple socket server client will terminate when .bye
I'm a novice/beginner programmer having problems getting some simple client/server C code working. My
I have this simple client-server application pair. The code is pretty simple, I'm using

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.