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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:23:16+00:00 2026-06-01T07:23:16+00:00

I have a problem here with this code. I’m opening a socket, then listening

  • 0

I have a problem here with this code. I’m opening a socket, then listening to it with a while loop. I send data from a php script with

socket_write($sock, $test, $len);

It works very well, but when I send several writes in a row, the Python script handles some of the writes as just one write.

import socket

HOST = 'localhost' # the host
PORT = 12126 # the port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while 1:
  s.listen(0)
  conn, addr = s.accept()
  print 'Connected by', addr
  while 1:
      data = conn.recv(1024)
      if not data: break
conn.close()

I’m looking for a way to listen to that port and get one write after another.

  • 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-01T07:23:17+00:00Added an answer on June 1, 2026 at 7:23 am

    That’s not the way sockets work. Bytes go in and bytes come out, but there has to be some other mechanism to tell you how big a message is. If you send 50 bytes, then another 75 bytes, then 20 bytes on one end of a socket, and then call recv(100), you could get anywhere from 1 to 100 bytes from a blocking socket. You are responsible for buffering recv’s until you have a complete message, and you have to define what a complete message is. Some options:

    1. Send fixed length messages.
    2. Send a fixed number of bytes representing the length of the message, then the message.
    3. Separate messages with a sentinel byte.

    Here’s an example of a class to buffer received data using a sentinel byte:

    import socket
    
    class Client(object):
    
        def __init__(self):
            self.buffer = ''
            self.sock = None
    
        def connect(self,address):
            self.buffer = ''
            self.sock = socket.socket()
            self.sock.connect(address)
    
        def get_msg(self):
            '''Append raw data to buffer until sentinel is found,
               then strip off the message, leaving the remainder
               in the buffer.
            '''
            while not '\n' in self.buffer:
                data = self.sock.recv(4096)
                if not data:
                    return ''
                self.buffer += data
            sentinel = self.buffer.index('\n') + 1
            msg,self.buffer = self.buffer[:sentinel],self.buffer[sentinel:]
            return msg
    
        def close(self):
            self.sock.close()
    
    if __name__ == '__main__':
        c = Client()
        c.connect((HOST,PORT))
        while True:
            msg = c.get_msg()
            if not msg:
                break
            print repr(msg)
        c.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
I have the following problem First here is my code: $day_difference = 1; while
i have a small problem here which i cannot fix,This post goes through but
I have read a lot of related topics here regarding this problem but I
I have some problem to order an array by a field of this, here
This is my first post here. I have a problem. I need to take
Hy! Here is my problem: i have a profile and this profile has for
i have some problem here with Edit Data Using ASP.NET Razor in WebMatrix i
i have some problem here with Edit Data Using ASP.NET Razor in WebMatrix i
This is a little bit of weird problem here. Say I have a C++

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.