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

  • Home
  • SEARCH
  • 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 6793499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:06:28+00:00 2026-05-26T18:06:28+00:00

I write client-server appliaction. Client is on android, server on plain Java. I want

  • 0

I write client-server appliaction. Client is on android, server on plain Java. I want to client make connection with server next send him some file then server write this file on server-disc and then send to client another file. For now client make connection then send file to server but here starts problems. When I want to server send file to client I get “socket is close” exception.

Here is my code:

server:

public class ThreadHandler implements Runnable
{
public ThreadHandler(Socket i)
{ 
    sk = i; 
}

public void run()
{  
    try
    {
        File file = getFile();
        ParseData data = parseXML(file);

        if(data.getEvent().equals("registration"))
        {
            sendRegistrationResponse();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public File getFile() throws Exception
{
    File file=null;

    InputStream input = sk.getInputStream();

    file = new File("C://protocolFile/" + "temp.xml");
    FileOutputStream out = new FileOutputStream(file);

    byte[] buffer = new byte[sk.getReceiveBufferSize()];

    int bytesReceived = 0;

    while((bytesReceived = input.read(buffer))>0) {
        out.write(buffer,0,bytesReceived);
    }

    input.close();
    input = null;
    out.flush();
    out.close();
    out = null;
    System.gc();

    return file;
}

public void sendRegistrationResponse() throws Exception
{
    String fileName = createRegistrationResponseXML();
    sendToApp(fileName);
}

public void sendToApp(String fileName) throws Exception
{
    OutputStream output = sk.getOutputStream();     

    FileInputStream fileInputStream = new FileInputStream(fileName);
    byte[] buffer = new byte[sk.getSendBufferSize()];
    int bytesRead = 0;

    while((bytesRead = fileInputStream.read(buffer))>0)
    {
        output.write(buffer,0,bytesRead);
    }

    output.close();
    fileInputStream.close();
}

}

client:

public class AndroidProtokolActivity extends Activity {

private File directory;

XmlSerializer serializer = Xml.newSerializer();
StringWriter writer=new StringWriter();;

Socket sk;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "protocol");
    directory.mkdirs();
}

public void fun(View view)
{
    Log.i("======", "fun==============");
    try
    {
        sk = new Socket("55.555.555.555", 5555);

        String registrationFile = createRegistrationXML(serializer);
        sendToServer(registrationFile,sk);
        getRegistrationXML();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

public void sendToServer(String file, Socket sk) throws Exception
{
    Log.i("======", "sendToServer==============");
    OutputStream output = sk.getOutputStream();     

    String pathToOurFile = directory + File.separator + file;

    FileInputStream fileInputStream = new FileInputStream(pathToOurFile);
    byte[] buffer = new byte[sk.getSendBufferSize()];
    int bytesRead = 0;

    while((bytesRead = fileInputStream.read(buffer))>0)
    {
        output.write(buffer,0,bytesRead);
    }

    output.close();
    fileInputStream.close();
}

public File getRegistrationXML() throws Exception
{
    Log.i("======", "getRegistrationXML==============");
    File file=null;

    InputStream input = sk.getInputStream();

    file = new File(directory, "rejestracjaOdpowiedz.xml");
    FileOutputStream out = new FileOutputStream(file);

    byte[] buffer = new byte[sk.getReceiveBufferSize()];

    int bytesReceived = 0;

    while((bytesReceived = input.read(buffer))>0) {
        out.write(buffer,0,bytesReceived);
    }

    input.close();
    input = null;
    out.flush();
    out.close();
    out = null;
    System.gc();

    return file;
}
}

I cut from this code creating XML files functions. I get “java.net.SocketException: Socket is closed” on line “OutputStream output = sk.getOutputStream();” in function “sendToApp” on server side and “java.net.SocketException: Socket is closed” on line “InputStream input = sk.getInputStream();” in “getRegistrationXML” line in function “getRegistrationXML” on client side. I try to find out what is wrong but I dont have any idea. Do anyone of you know what is wront with this code? Thanks for any help.

EDIT:

This code show how I create XML file:

public String createRegistrationXML(XmlSerializer serializer) throws Exception
{
    Log.i("======", "createRegistrationXML==============");
    XmlSerializer serializer = Xml.newSerializer();
    writer = new StringWriter();
    serializer.setOutput(writer);

    serializer.startDocument("UTF-8", true);
        serializer.startTag("", XMLTag);
            serializer.startTag("", eventTag);
                serializer.text(REGISTRATIONEVENT);
            serializer.endTag("", eventTag);
        serializer.endTag("", XMLTag);
    serializer.endDocument();

    File outputFile = new File(directory, "file.xml");
    FileOutputStream fos = new FileOutputStream(outputFile);
    OutputStreamWriter osw = new OutputStreamWriter(fos); 
    osw.write(writer.toString());
    osw.flush();
    osw.close();

    return "file.xml";
}
  • 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-26T18:06:29+00:00Added an answer on May 26, 2026 at 6:06 pm

    Try not to close the socket’s input and output stream individually. Just close the socket when all is done.

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

Sidebar

Related Questions

I write client-server application. Client will run on android and server on plain java.
I'm trying to write a client-server application in Java with an XML-based protocol. But
I want to create SOCKET chat(server + clients) with private messaging. When client write
I am new to java NIO. I have to write a simple server client
I write a client-server application which will be sending an .xml file from the
I need to write a client-server application. I want to write it in python,
I'm trying to secure a connection from a Java Client/Server application that communicates over
I have an client server application on Android.And I have to send data from
I need to write some kind of client-server application using bluetooth. I need to
I'm trying to write a simple client/server chat application in 2 languages - Java

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.