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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:43:02+00:00 2026-06-17T01:43:02+00:00

I opened a socket between an Android app and a python server. The combination

  • 0

I opened a socket between an Android app and a python server. The combination is that the Server listens, and android connects to the Server.

Here is the server code. The problematic part takes place in the definition of handle :

import SocketServer
from time import sleep
import sys

HOST = '192.168.56.1'
PORT = 2000

class SingleTCPHandler(SocketServer.StreamRequestHandler):
    def handle(self):
    try:
        while(1):
            sleep(0.03)
            data = self.rfile.readline().strip()

            print data
    except KeyboardInterrupt:
        sys.exit(0)

class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    allow_reuse_address = True

    def __init__(self, server_address, RequestHandlerClass):
        SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)


server = SimpleServer((HOST, PORT), SingleTCPHandler)
try:
    server.serve_forever()
except KeyboardInterrupt:
    sys.exit(0)

The connection is established normally, and the Android app sends the following data to the socket:

'0:0'

But the data is received on the Server as:

'\x000\x00:\x000\x00'

The variable that receives the data is:

data = self.rfile.readline().strip()

and printing gives the regular format:

In [2]: print data
0:0

I didn’t manage to step into the print function with pdb to see what it does.
I’m looking for a way to convert the '\x000\x00:\x000\x00' to '0:0'.

Please advise on a way to convert the variable. You are welcome to comment/criticize the whole implementation. This is my first project in dealing with sockets so i don’t know the pitfalls.

Update

This was the original Android code:

String podaci = "0:0";
public void Socketic() throws IOException {

    Socket mojSocket = new Socket(urlServer, port);

    DataOutputStream izlazdata = new DataOutputStream(
            mojSocket.getOutputStream());
    while (podaci != "end") {
        try {
            Thread.sleep(60);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

        izlazdata.writeChars(podaci);
        izlazdata.flush();
    }
    izlazdata.close();
    mojSocket.close();
};

And the problem was, as you suspected in:

izlazdata.writeChars(podaci);

writeChars uses the method writeChar. The API documentation for writeChar states:

Writes a char to the underlying output stream as a 2-byte value, high byte first…

The two bytes represent the 16bits which UTF-16 uses for encoding.

When we changed it to everything started working:

izlazdata.writeBytes(podaci);

Update

Based on the answers given, here is how the unwanted string is to be interpreted in terms of characters.

enter image description here

This solves my concrete problem, however, if someone would give a more generic solution to what happend here so that a larger lesson can be learned.
If not, i will accept Esailijas answer in a few days.

  • 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-17T01:43:03+00:00Added an answer on June 17, 2026 at 1:43 am

    You need to show the code happening Android but it strongly seems like it’s sending data in UTF-16BE. You should specify the encoding on the Android end. The characters are not hexadecimal literally, but because the NUL character is unprintable, python shows \x00 instead.

    Another option is to decode it:

    self.rfile.readline().decode("utf_16_be").strip()
    

    note that the result of this is an unicode string.

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

Sidebar

Related Questions

I'm using python socket server to which I connect with Android and periodically send
How to open socket thru proxy server in .Net C#? So I opened up
i've tried to build a socket server in php, but found out that because
I have a script that opens a port and allows socket communication between multiple
My application has opened an UDP socket that is bound to INADDR_ANY to listen
I made a small program that listens and sends lines on a tcp socket
So I opened a socket connection like was described here I want to let
Lets assume you have a app that opens a socket port for communication purposes.
Have setup an ssl connection app, have already setup connection between client and server
I opened a files in vim using gf command.which opened a file that 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.