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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:39:37+00:00 2026-06-15T18:39:37+00:00

I’ve been messing around with Python sockets in hope of understanding how network programming

  • 0

I’ve been messing around with Python sockets in hope of understanding how network programming is done better. Right now I’m trying to set up a client that connects to a server and can send it any number of messages before closing. I’m still not quite understanding everything and I can only get it to send one message. It’s probably something obvious to someone experienced with socket programming. Could someone explain to me how to get it to send multiple messages? Maybe provide me a good source that explains how the connect, bind, close, recv, and all the main socket functions work? (Most the sources I find just tell me when to use them.)

Client

# !usr/bin/python

import socket
import sys


def main():
    host = ""
    port = 8934
    message = "Hello World!"

    host = raw_input("Enter IP: ")
    #Create Socket
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, msg:
        print "Failed to create socket. Error code: %s Error Message: %s"%(str(msg[0]),msg[1])
        sys.exit()
    print "Socket created"

    #Connect to server
    s.connect((host,port))

    while message != "/e":
        #Send Data
        message = raw_input("Send >> ")
        try:
            s.sendall(message)
        except socket.error, msg:
            print "ERROR %s"%(msg[1])
            print "Failed to send."
        s.close()

if __name__ == "__main__":
    main()

Server

# !usr/bin/python

import socket
import sys

HOST = ""
PORT = 8934
s = socket.socket()

class BoServer:


    def __init__(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error,msg:
            print "Unable to create socket"
            sys.exit()
        print "Socket created."
    def bind(self):
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            s.bind((HOST,PORT))
        except socket.error,msg:
            print "Bind failed. Closing..."
            sys.exit()
        print "Socket bound."
    def run(self):
        while True:
            s.listen(10)
            print "Socket Listening"
            conn, addr = s.accept()
            print "Connected to %s:%s"%(addr[0],addr[1])
            income = conn.recv(4096)
            if income != "":
                print income

def main():
    serv = BoServer()
    serv.bind()
    serv.run()

if __name__ == "__main__":
    main()
  • 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-15T18:39:38+00:00Added an answer on June 15, 2026 at 6:39 pm

    On your client side, you’re closing the socket inside your while True: loop, so you will not be able to transmit more messages on future iterations of the loop. If you intend to send each message on a different connection, then you will need to create the socket within the loop. If you intend to send multiple messages on the same connection, then you will need to move the close call outside the loop.

    On the server side, you’re running listen() once every iteration of the loop which is unnecessary: its purpose is to set the queue length for buffered incoming connections, so only needs to be called once. You are also only performing a single read from the socket before continuing to a second iteration of the loop where you accept another incoming connection.

    Depending on how you want your server to behave, another potential problem is that you are only servicing a single incoming connection at a time. If you intend to deal with long running connections, then this could be a problem. To handle multiple connections at once, you’ll probably want to either handle each connection on its own thread (e.g. using the ThreadingTCPServer class from the SocketServer standard library module), or use an asynchronous IO framework like Twisted.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have been unable to fix a problem with Java Unicode and encoding. The

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.