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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:07:45+00:00 2026-05-24T16:07:45+00:00

I’ve been struggling along with sockets, making OK progress, but I keep running into

  • 0

I’ve been struggling along with sockets, making OK progress, but I keep running into problems, and feeling like I must be doing something wrong for things to be this hard.

There are plenty of tutorials out there that implement a TCP client and server, usually where:

  • The server runs in an infinite loop, listening for and echoing back data to clients.
  • The client connects to the server, sends a message, receives the same thing back, and then quits.

That I can handle. However, no one seems to go into the details of what you should and shouldn’t be doing with sequential communication between the same two machines/processes.

I’m after the general sequence of function calls for doing multiple messages, but for the sake of asking a real question, here are some constraints:

  • Each event will be a single message client->server, and a single string response.
  • The messages are pretty short, say 100 characters max.
  • The events occur relatively slowly, max of say, 1 every 5 seconds, but usually less than half that speed.

and some specific questions:

  1. Should the server be closing the connection after its response, or trying to hang on to the connection until the next communication?
  2. Likewise, should the client close the connection after it receives the response, or try to reuse the connection?
  3. Does a closed connection (either through close() or through some error) mean the end of the communication, or the end of the life of the entire object?
    1. Can I reuse the object by connecting again?
    2. Can I do so on the same port of the server?
    3. Or do I have reinstantiate another socket object with a fresh call to socket.socket()?
  4. What should I be doing to avoid getting ‘address in use’ errors?
  5. If a recv() times out, is the socket reusable, or should I throw it away? Again, can I start a new connection with the same socket object, or do I need a whole new socket?
  • 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-24T16:07:46+00:00Added an answer on May 24, 2026 at 4:07 pm
    1. If you know that you will communicate between the two processes soon again, there is no need for closing the connection. If your server has to deal with other connections as well, you want to make it multithreaded, though.
    2. The same. You know that both have to do the same thing, right?
    3. You have to create a new socket on the client and you can also not reuse the socket on the server side: you have to use the new socket returned by the next (clientsocket, address) = serversocket.accept() call. You can use the same port. (Think of webservers, they always accept connections to the same port, from thousands of clients)

    In both cases (closing or not closing), you should however have a message termination sign, for example a \n. Then you have to read from the socket until you have reached the sign. This usage is so common, that python has a construct for that: socket.makefile and file.readline

    UPDATE:

    1. Post the code. Probably you have not closed the connection correctly.
    2. You can call recv() again.

    UPDATE 2:
    You should never assume that the connection is reliable, but include mechanisms to reconnect in case of errors. Therefore it is ok to try to use the same connection even if there are longer gaps.
    As for errors you get: if you need specific help for your code, you should post small (but complete) examples.

    • 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 am currently running into a problem where an element is coming back from
I have a jquery bug and I've been looking for hours now, I can't
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.