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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:10:30+00:00 2026-06-09T06:10:30+00:00

I have a problem with a simple python tcp server (I’m using SocketServer class)

  • 0

I have a problem with a simple python tcp server (I’m using SocketServer class) that have to receive data from a java client.

Here the server side code:

class ServerRequestHandler(SocketServer.BaseRequestHandler):
    [...]
    def handle(self):
        requestCode = struct.unpack('>i', self.request.recv(4))[0]
        [...]
    [...]

Here there is the client:

addr = InetAddress.getByName(hostname);
SocketAddress sockaddr = new InetSocketAddress(addr, port);
clientSocket = new Socket();
clientSocket.connect(sockaddr, timeoutMs);
clientSocketDataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
int   requestCode = 1;
clientSocketDataOutputStream.writeInt(requestCode);
clientSocketDataOutputStream.flush();

I start the Python server and then I try to execute the client that have to send an integer to the server.

On the server side Python raise an exception because the recv(4) function does not read 4 bytes, it returns only one byte.

My java client sends 4 bytes correctly in fact, if I try to call recv(1) 4 times it reads 4 bytes correctly.

I tried to write a Python client that execute the same operation of my java client and in this case the server’s recv(4) works well.

How to solve this problem? I thought to implement a simple python buffered reader function that reads from socket byte by byte but I’m sure that a smarter solution exists.

  • 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-09T06:10:32+00:00Added an answer on June 9, 2026 at 6:10 am

    the recv doesn’t have to read 4 bytes, it just grabs whatever is there up to a max of four bytes. Since, as you said, you can call recv(1) 4 times. you can do this

    def recvall(sock, size):
        msg = ''
        while len(msg) < size:
            part = sock.recv(size-len(msg))
            if part == '': 
                break # the connection is closed
            msg += part
        return msg
    

    this will repeatedly call recv on sock until size bytes are received. if part == '' the socket is closed so it will return whatever was there before the close

    so change

    requestCode = struct.unpack('>i', self.request.recv(4))[0]
    

    to

    requestCode = struct.unpack('>i', recvall(self.request, 4))[0]
    

    I’d suggest making recvall a method of your class to make things cleaner.


    this is a modification of a method from the safe socket class defined here: http://docs.python.org/howto/sockets.html

    • 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 using Twisted for simple concurrency in python. The problem
I have a simple problem in Python that is very very strange. def estExt(matriz,erro):
I have a simple Python server, import BaseHTTPServer import SimpleHTTPServer PORT = 8080 class
I have a problem with sending urgent data in python via TCP. I have
Trying to make simple minesweeper game in python, but have one problem. I have
I have a simple problem with SQL SERVER charindex function. DECLARE @VAR1 varchar SET
I have a simple problem that I have not been able to find an
I have a simple problem that I've been stuck on for some time and
I have problem with creating a simple MySQL trigger in C#. I'm using StringBuilder
I have a simple problem that i cannot solve. I have a dictionary: aa

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.