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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:52:36+00:00 2026-06-03T22:52:36+00:00

I am getting frustrated over a java.net.SocketOutputStream that just refuses to flush properly! I

  • 0

I am getting frustrated over a java.net.SocketOutputStream that just refuses to flush properly! I must be overlooking the obvious. In my Groovy script below I attempt to connect to a simple socket server running on my Galaxy Nexus Android 4.0.2 phone and it gives me a socket closed exception right after my 5 second timeout expires.

SocketClient.groovy

import java.net.*
socket = new Socket()
socket.connect(new InetSocketAddress(InetAddress.getByAddress([172,17,57,21] as byte[]), 58789), 5000)
println "Connected!"
socket.withStreams { input, output ->
    println "Processing ${input.class.name} and ${output.class.name} streams..."
    output.withWriter {
        it << 'echo testing\n\n'; 
        it.flush();
        output.flush()
        def readerThread = Thread.start {
            println "Request written, reading response..."; println input.text
        }
        println "Waiting 5 secs for read to complete."
        readerThread.join(5000)
    }
    println "Stream closed!"
}

I’ve tried all different means of flushing, including direct flush of the output stream to indirect wrapping with a writer and flushing. No data appears on the server until the stream closes which causes the socket to close which crashes any attempt to read from the socket. What am I doing wrong? I’ll inline my Android code below as well. It’s a simple activity that launches a socket server.

MyServer.java

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MyServer extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RawSocketServer rawSocketServer = new RawSocketServer();
        String port = rawSocketServer.getLocalPort();
        TextView textView = (TextView) this.findViewById(R.id.mainTextView);
        textView.setText("Server bound to port " + port);
        rawSocketServer.start();
    }
}

RawSocketServer.java

package com.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: cliff
 * Date: 5/9/12
 * Time: 4:03 PM
 */
public class RawSocketServer {

    public static final int PORT = 0;
    private ServerSocket serverSocket;

    public RawSocketServer() {
        this(PORT);
    }

    public RawSocketServer(int port) {
        try {
            this.serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("server cannot bind to port " + port);
        }
    }

    public void start() {
        new Thread(new Runnable() {
            public void run() {
                go();
            }
        }).start();
    }

    private void go() {
        Socket socket = null;
        while (true) {
            try {
                socket = this.serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("cannot accept from server socket" + e);
            }
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Could not get input stream from socket. " + e);
            }
            PrintWriter writer = null;
            try {
                writer = new PrintWriter(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Could not get output stream from socket. " + e);
            }
            try {
                for (String line = reader.readLine(); line !=null; line = reader.readLine()) {
                    writer.print(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Error reading/writing from/to socket. " + e);
            }
        }
    }

    public String getLocalPort() {
        return "" + serverSocket.getLocalPort();
    }
}
  • 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-03T22:52:37+00:00Added an answer on June 3, 2026 at 10:52 pm

    I am wondering…. you have “it << ‘echo testing'” there. Using that on a Writer means afaik that no line end is written. Since your server uses readLine, it expects a line ending. Thus the data may arrive, but readLine is not finishing, because it waits for the end of the line. The fix would then be to do for example “it << ‘echo testing\n'” or “it << ‘echo testing’ << ‘\n'”.

    Now I don’t know the Android API so much, but your server may have the same problem in “writer.print(line);”, because I think that a line read with readLine, does not contain the line ending anymore. But since that is not read again, it should be of no problem at the moment.

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

Sidebar

Related Questions

I am getting frustrated with Google query's on code so I'll just ask all
I've just tried running the sample java files that I got with the epson
I'm getting increasingly frustrated with Flex's Dictionary (which is really just an array with
Im getting frustrated because of OpenDNS and other services (ie: roadrunner) that now always
I'm getting frustrated because I am failing to have any control over my bar
I have a question regarding file reading and I am getting frustrated over it
I've been getting frustrated with some subversion conflicts i've been getting for a long
I'm reverse-engineering a serial protocol and getting frustrated by bad tools. Does anyone know
So I am getting pretty frustrated with this and feel the only way to
I'm very frustrated with getting ImageMagick to run. The Situation: ImageMagick is installed on

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.