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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:29:16+00:00 2026-06-11T10:29:16+00:00

I’m trying to make a server / client connection with sockets. But they will

  • 0

I’m trying to make a server / client connection with sockets. But they will not close properly and I can’t wrap my head around why.

Update 1

I’ve corrected my stupid misstake below of not actually CALLING the s.close function in the question.
But it turn out this was’nt my problem.

End of updates

This is my server code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket
import sys

if __name__ == '__main__':
    # Server connection
    s = socket.socket()          # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12345                 # Reserve a port for your service.

    print 'Server started!'
    print 'Waiting for clients...'

    s.bind((host, port))        # Bind to the port
    s.listen(5)                 # Now wait for client connection.
    c, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    msg = c.recv(1024)

    print addr, ' >> ', msg

    if msg == 'close':
        print 'Closing down'
        c.send('SENT: Closing down')

    c.shutdown(socket.SHUT_RDWR)
    c.close()

This is my client code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import socket

if __name__ == '__main__':
    # Server
    s = socket.socket()          # Create a socket object
    host = socket.gethostname()  # Get local machine name
    port = 12345                 # Reserve a port for your service.

    print 'Connecting to ', host, port
    s.connect((host, port))

    msg = raw_input('CLIENT >> ')
    s.send(msg)
    msg = s.recv(1024)
    print 'SERVER >> ', msg

    s.close()                     # Close the socket when done

And this is the error message it produces:

In [13]: %run cjboxd.py
Server started!
Waiting for clients...
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    173             else:
    174                 filename = fname
--> 175             __builtin__.execfile(filename, *where)

/home/nine/slask/cjboxd.py in <module>()
     19     print 'Waiting for clients...'
     20 
---> 21     s.bind((host, port))        # Bind to the port
     22     s.listen(5)                 # Now wait for client connection.
     23     c, addr = s.accept()     # Establish connection with client.

/usr/lib/python2.7/socket.pyc in meth(name, self, *args)
    222 
    223 def meth(name,self,*args):
--> 224     return getattr(self._sock,name)(*args)
    225 
    226 for _m in _socketmethods:

error: [Errno 98] Address already in use

It will work like a minute later.

  • 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-11T10:29:17+00:00Added an answer on June 11, 2026 at 10:29 am

    you need socket.socket.setsockopt, .i.e s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    Though the socket will be closed by the os when process ended, it is a good behavior to call close() explicitly.
    However, after that, the local addr(local_ip, local_port) is not avaiable until 2 MSL(maximum segment lifetime) has passed. Why? and what we can do? you can read these:

    http://www.tcpipguide.com/free/t_TCPConnectionTermination-3.htm
    and http://www.unixguide.net/network/socketfaq/4.5.shtml

    It will be difficult to me to post it more clear than they do :).

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
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
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; 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.