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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:09:19+00:00 2026-05-23T20:09:19+00:00

So I’ve scoured the net (mostly SO, google) to find good examples of socket

  • 0

So I’ve scoured the net (mostly SO, google) to find good examples of socket programming with Android. I’ve done lots of Android dev (just not with sockets). I dont understand why my readLine() ALWAYS RETURNS NULL. And please ignore the hideous code, this is a quick and dirty prototype for a friend. My overall goal is to establish a connection to a server, send header data (GET /MOUNTPOINT Content-Type: … etc…), and receive a response, based on the response I need to either continue the stream or close it. Here is my most recent attempts code:

                    String userPass = new     String(Base64.encodeToString("user:password".getBytes(),     Base64.DEFAULT));
            boolean connected = false;
            String requestmsg = "GET /MOUNTPOINT" + " HTTP/1.0\r\n";
            requestmsg += "User-Agent: MYCUSTOMAGENT\r\n";
            requestmsg += "Accept: */*\r\n";
            requestmsg += "Connection: close\r\n";

            requestmsg += "Authorization: Basic " + userPass;

            requestmsg += "\r\n";
            DataOutputStream dos =  null;
            DataInputStream dis = null;
            try {

                Log.d("ClientActivity", "Connecting...");
                Socket socket = new Socket("1.2.3.4", 9000);
                connected = true;
                String data = "";

                while (connected) {
                    try {
                        Log.d("ClientActivity", "C: Sending command.");

                        dos = new DataOutputStream(socket.getOutputStream());
                        dis = new DataInputStream(socket.getInputStream());

                        dos.write(requestmsg.getBytes());
                        Log.i("ClientActivity", "RequestMsg Sent");

                        StringBuilder sb = new StringBuilder();

                        while ((data = dis.readLine()) != null)
                            {
                                sb.append(data);
                            }
                            Log.i("ClientActivity", "C: Sent.");
                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);

                    }
                }
                socket.close();
                Log.d("ClientActivity", "C: Closed.");
            } catch (Exception e) {
                Log.e("ClientActivity", "C: Error", e);
                connected = false;
            }

I’ve tried using the HttpClient API but it inserts a “GET /” message in the header and I need to specify “GET /MOUNTPOINT”. Plus I don’t know if HttpClient allows streaming. Ive also tried URLConnection, but also it inserts either GET or POST by default. Thanks for any help!

Also I have tried the following:

SocketAddress sockaddr = new InetSocketAddress("1.2.3.4", 9000);
nsocket = new Socket();

nsocket.connect(sockaddr, 10 * 1000); // 10 second connection timeout
    if (nsocket.isConnected()) {
        //nsocket.setSoTimeout(20 * 1000); // 20 second timeout once data is flowing
        nis = nsocket.getInputStream();
        nos = nsocket.getOutputStream();
        Log.i(NTAG, "Socket created, streams assigned");
        // Build request message
        requestmsg = "GET /MOUNTPOINT" + " HTTP/1.0\r\n";
        requestmsg += "User-Agent: MYANDROIDAGENT\r\n";
        requestmsg += "Accept: */*\r\n";
        requestmsg += "Connection: close\r\n";
        requestmsg += "Authorization: Basic " + userPass;
        requestmsg += "\r\n";

        nos.write(requestmsg.getBytes());


        nos.write(genGPGGA().getBytes());
        Log.i(NTAG, "Waiting for inital data...");
        byte[] buffer = new byte[4096];
        int read = nis.read(buffer); // This is blocking
        //Log.i("ReadNTRIP", tempdata.toString());
        String test = "TESTING";

        while (read != -1) {
            tempdata = new byte[read];
            System.arraycopy(buffer, 0, tempdata, 0, read);
            read = nis.read(buffer, 0, 4096); // This is blocking

        }
    }
} catch (SocketTimeoutException ex) {
    ex.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        nis.close();
        nos.close();
        nsocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.i(NTAG, "Finished");

}

But read always returns -1. Im ready to pull my hair out.

EDIT: I added a bounty (worth very little) – I don’t have much rep to give, but this problem has become a show stopper for me. To get the bounty, write a simple android program to connect to thor.lsu.edu, port 9000, send a simple GET / HTTP/1.0 command and actually get back data (using sockets – cannot be with URLConnection or HttpClient). Post the program and a screenshot of the results. I’ve tried it so many different ways I don’t know what else to do (using InputStreams, BufferedInputStreams, InputStreamReaders, etc…). I’ve even used an example Android program (THAT WORKS), yet I still don’t get back data in my implementation. I don’t know if its a timing issue or what.

Thanks

Edit: I’ve noticed that I cannot even browse to http://thor.lsu.edu:9000 in Androids browser. It gives a data connectivity problem. Any idea why this port may be blocked?

  • 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-23T20:09:20+00:00Added an answer on May 23, 2026 at 8:09 pm

    All I did was a few modifications to your code. I think the major change was just not using the DataInputStream since the documentation explicitly says to not use readLine() anymore on this data structure. http://download.oracle.com/javase/1.4.2/docs/api/java/io/DataInputStream.html#readLine%28%29

    I also remove the authorization stuff since it wasn’t required. If you run this code somewhere, you will notice the log printing out the HTTP response.

    String requestmsg = "GET /MOUNTPOINT" + " HTTP/1.0\r\n";
        requestmsg += "User-Agent: MYCUSTOMAGENT\r\n";
        requestmsg += "Accept: */*\r\n";
        requestmsg += "Connection: close\r\n";
    
        // requestmsg += "Authorization: Basic " + userPass;
    
        requestmsg += "\r\n";
        DataOutputStream dos = null;
        BufferedReader dis = null;
        try {
    
            Log.d("ClientActivity", "Connecting...");
            Socket socket = new Socket("thor.lsu.edu", 9000);
            String data = "";
    
            try {
                Log.d("ClientActivity", "C: Sending command.");
    
                dos = new DataOutputStream(socket.getOutputStream());
                dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    
                dos.write(requestmsg.getBytes());
                Log.i("ClientActivity", "RequestMsg Sent");
    
                StringBuilder sb = new StringBuilder();
    
                while ((data = dis.readLine()) != null) {
                    sb.append(data);
                }
                Log.i("ClientActivity", "C: Sent.");
                Log.i("ClientActivity", "C: Received " + sb.toString());
            } catch (Exception e) {
                Log.e("ClientActivity", "S: Error", e);
    
            }
    
            socket.close();
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a reasonable size flat file database of text documents mostly saved in
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.