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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:19:16+00:00 2026-05-25T06:19:16+00:00

i run some code to connect to a server, send it an Object and

  • 0

i run some code to connect to a server, send it an Object and then receive another Object.
while running this on my Android enulator (v2.2) i get – java.net.SocketException: Socket is closed
the connection to the server is successful and i’m able to send the object but when i’m trying to do socket.getInputStream() it throws the exception

this is my connector class:

public class ConnectionToServer {
    UserProblemRequest sentProblem;
    Problem responseProblem;
    Socket socket;

    public ConnectionToServer(){
        sentProblem = null;
        responseProblem = null;
        socket = null;
    }

    public void connect(){
        try {
            Log.d(TAG, "Connecting...");
            socket = new Socket(Utils.SERVER_IP, Utils.SERVER_PORT);
            Log.d(TAG, "SUCCESS: Connected!");
        } catch (UnknownHostException e) {
            Log.d(TAG, "ERROR: Falied to connect! (UnknownHostException)");
            e.printStackTrace();
        } catch (IOException e) {
            Log.d(TAG, "ERROR: Falied to connect! (IOException)");
            e.printStackTrace();
        }
    }

    public void setProblemFromByteArray(byte[] data, boolean isFile){
        sentProblem = new UserProblemRequest();
        sentProblem.fileBArray = data.clone();
        if (isFile){
            sentProblem.requestType = Utils.requestType_IMAGE;
        }
        else {
            sentProblem.requestType = Utils.RequestType_STRING;
        }
    }

    public void sendProblem(){

        ObjectOutputStream os;

        try {
            os = new ObjectOutputStream(socket.getOutputStream());
            Log.d(TAG, "Sending file to server...");
            os.writeObject(sentProblem);
            os.flush();
            os.close();
        } catch (IOException e) {
            Log.d(TAG, "ERROR: Falied to send file to server!");
            e.printStackTrace();
        }
        Log.d(TAG, "SUCCESS: File sent to server!");
    }

    public void closeConnection(){
        try {
            socket.close();
        } catch (IOException e) {
            Log.d(TAG, "failed to close socket");
            e.printStackTrace();
        }
    }

    public Problem reciveResponseFromServer(){

        ObjectInputStream ois;

        try {
            ois = new ObjectInputStream(socket.getInputStream());
            responseProblem = (Problem) ois.readObject();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return responseProblem;
    }

}

and i use this code to run it:

ConnectionToServer serverConnection = new ConnectionToServer();
serverConnection.connect();
serverConnection.setProblemFromByteArray(temp_data, true);
serverConnection.sendProblem();
Problem responseProblem = serverConnection.reciveResponseFromServer();
serverConnection.closeConnection();

any ideas?

  • 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-25T06:19:17+00:00Added an answer on May 25, 2026 at 6:19 am

    Does closing the ObjectOutputStream() in sendProblem() wind up closing the underlying socket so that when you getInputStream() in reciveResponseFromServer() the socket is already closed?

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

Sidebar

Related Questions

I'm trying to connect with HTTP server and send some date. My code looks
I receive a warning when I run some code through Visual Studio's Code Analysis
I'm trying to send some information to a server with Android using Monodroid. The
How can I automatically run some code when a WPF form is closed?
I like to run some code when a user closes my ms 2000 app.
I need to run some code that will fetch some configuration values from the
[Rails] I need to run some code only 40% of the times, how may
I would like to run some code onload of a form in WPF. Is
I mean, how does Java decide which protocols are available? I run some code
I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually

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.