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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:27:05+00:00 2026-05-31T11:27:05+00:00

socket from c program sends 3 numbers to a socket in python. code and

  • 0

socket from c program sends 3 numbers to a socket in python. code and output below:

python server code:

import SocketServer
import threading
import select
import sys

class TCPHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        self.data = self.rfile.readline()
        print "%s wrote:" % self.client_address[0]
        print (self.data)
        self.request.send(str(long(self.data)+1000))
        cur_connex.append({'ip': self.client_address, 'range': [long(self.data), long(self.data)+10000]})

        for a in cur_connex:
            print("%s did %d - %d on thread %s" % (a['ip'], a['range'][0], a['range'][1], threading.current_thread().name))

        while True:
            select.select([self.rfile], [], [])
            self.data = self.request.recv(10) #also tried "self.data = self.rfile.readline().strip("\n")" and without "strip()"
            print self.data


if __name__ == "__main__":
    HOST, PORT = "", 50001
    cur_connex = []
    done_up_to = 0

    # Create the server, binding to localhost on port 50001
    server = SocketServer.TCPServer((HOST, PORT), TCPHandler)

    # Activate the server on new thread
    listen_thread = threading.Thread(target=server.serve_forever, name='listen_th')
    listen_thread.start()

c++ client code:

for(unsigned long long i; i < perfect_numbers.size(); i++)
{
    cout << "i is " << i << " writing " << perfect_numbers[i] << "\n";

    for(int j = 0; j < perfect_numbers[i].size(); j++)
    {
        cout << perfect_numbers[i][j] << "|";
    }

    if(write(sock, perfect_numbers[i].c_str(), sizeof(perfect_numbers[i].c_str())) == -1)
    {
        cerr << "\nwrite failed: " << strerror(errno) << "\n";
        return -1;
    }
}

if(close(sock) == -1)
{
    cerr << "close sock error: " << strerror(errno) << "\n";
    return -1;
}

cout << "exiting...\n";
return 1;

server output:

>>> 127.0.0.1 wrote:
12695

('127.0.0.1', 55624) did 12695 - 22695 on thread listen_th
28
496
8128

…and then continuous newlines

client output:

that took 1.26624 seconds
i is 0 writing 28

2|8|
|i is 1 writing 496

4|9|6|
|i is 2 writing 8128

8|1|2|8|
|exiting...

I assume that last bit of the C++ client program is enough. Also assuming it’s something in that while True loop in the server. New to both these languages, I know my coding isn’t good yet, be gentle…

  • 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-31T11:27:06+00:00Added an answer on May 31, 2026 at 11:27 am

    this post handles a bit more than what OP was asking for, I apologize for this matter. If you are only interested in how to fix the pending newlines written at the server please jump to section “python“.


    both client/server

    • You should add a delimiter between the numbers sent/received, there is nothing guaranteeing that each number will arrive individually. You may get two numbers in the same call to self.request.recv (...).

    python

    • print will append a newline ('\n') to every statement passed to it, unless you end the print statement with a comma.

    • You should add a check to see if there really was any data to be read, if not there might have been an error or the connection has been closed.

      while True:
          select.select([self.rfile], [], [self.rfile])
          self.data = self.request.recv(10) 
          if self.data:
             print self.data
          else:
             # <close connection here>
      

    c++

    write(sock, perfect_numbers[i].c_str(), sizeof(perfect_numbers[i].c_str()))
    
    • The above will always try to write sizeof(char*) characters to the socket sock, no matter if perfect_numbers[i] is one or a billion bytes of length.

      The third argument to write should be the number of bytes that you wish to be written, therefore you should use perfect_numbers[i].size (). 1


    1 I assume that you’ve been confused when looking at examples such as:

     char buf[1024];
    
     ...
    
     write (sock, buf, sizeof (buf));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have socket program which sends a file from socket client to socket server
I write a little program that sends file from client to server trough udp
The following code sends messages from child processes to their parents using socket pairs.
I have a small program that sends and receives data from server and client
Is it safe if I want to call closesocket() on a server socket from
I have two simple Python files: client.py and server.py . The client simply sends
I have a simple file transfer socket program where one socket sends file data
Im creating a server client program where client sends certain information to the server
I have a C program that do recv/send operations from/to socket using a for(;;)
I'm trying to make a server/client program where the client sends the text Hello

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.