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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:41:24+00:00 2026-06-17T16:41:24+00:00

PCClient : public class PCServer { /** * @param args */ static Socket socket

  • 0

PCClient :

public class PCServer
{

/**
 * @param args
 */
static Socket socket = null;
private static String ip = "192.168.42.129";
private static int port = 18181;

public static void main(String[] args)
{
    // TODO Auto-generated method stub
    try
    {
        socket = new Socket(ip, port);
    } catch (UnknownHostException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try
    {
        SendMsg(ip, port, "BZT");
        GetMessage getMessage = new GetMessage();
        getMessage.start();
    } catch (UnknownHostException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void SendMsg(String ip, int port, String msg) throws UnknownHostException, IOException
{
    try
    {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(msg);
        writer.flush();
        writer.close();
        //socket.close();
    } catch (UnknownHostException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

static class GetMessage extends Thread
{
    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        super.run();
        try
        {
            while (true)
            {
                System.out.println("--------------------------------------------------------");
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String str = in.readLine();
                System.out.println(str);
                //client.close();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
  }
}

AndroidServer : Service

public class SocketService extends Service
{
public static final int SERVERPORT = 18181;
ServerSocket serverSocket;
@Override
public IBinder onBind(Intent arg0)
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    // TODO Auto-generated method stub
    try
    {
        serverSocket = new ServerSocket(SERVERPORT);
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    GetSocketMessage getSocketMessage = new GetSocketMessage();
    getSocketMessage.start();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
    try
    {
        this.serverSocket.close();
    } 
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

class GetSocketMessage extends Thread
{
    @Override
    public void run()
    {
        // TODO Auto-generated method stub
        super.run();
        try
        {
            Log.i("s", "S: Connecting...");
            while (true)
            {
                Socket client = serverSocket.accept();
                Log.i("s", "S: Receiving...");
                try
                {
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    String str = in.readLine();
                    Log.i("s", "S: Received: '" + str + "'");

                    if ("BZT".equals(str))
                    {
                        Intent intent =  new Intent("com.StarHope.ZWGKXT.connected");
                        sendBroadcast(intent);
                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                        writer.write("OK");
                        writer.flush();
                        writer.close();
                    }
                    if ( Lbjy_usbActivity.content !=null )
                    {
                        Log.i("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "oooooooooooooooooooooooooooooo");
                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                        writer.write("");
                        writer.flush();
                        writer.close();
                    }
                } 
                catch (Exception e)
                {
                    Log.i("s", "S: Error");
                    e.printStackTrace();
                } 
                finally
                {
                    Log.i("s", "S: Done");
                }
            }
        } 
        catch (Exception e)
        {
            Log.i("s", "S: Error");
            e.printStackTrace();
        }
    }
  }
}

The android phone server can received the message sent from PCClient “BZT”,
But when the phone send “OK” to PC , there is always an

Exception:java.net.SocketException: Socket is closed.

How can I fix this?

  • 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-17T16:41:25+00:00Added an answer on June 17, 2026 at 4:41 pm

    Closing any stream obtained using getOutputStream() or getInputStream() closes the socket.

    Do you receive something before ‘BZT’ ? If so, the socket will be closed after you sent the empty answer.

    You should use only one output stream, and flush() each message without closing.

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

Sidebar

Related Questions

public void query(String sql, QueryResult queryResult) { q = queryResult; realQuery(sql, conString); } public
I am making a very simple app in which I send a text string
I have a template class where typename is a pointer to a function: CHook<NTSTATUS(__stdcall*)(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,HANDLE,PCLIENT_ID,PCONTEXT,PUSER_STACK,BOOLEAN)>
Suppose I have a asp.net mvc 3 application with an interface named /getdata ,
I have three questions: is it possible to destroy IdTCPServer by to many connection?
I am trying to stream microphone input from my Symbian device (Nokia N8) to
i've got the below code, and it operates just fine, only it takes a
My model is setup so Business has many clients, Client has one business. Inverse
I'm using a form with an IdTCPServer on it managing strings from the client
I have a 3rd party webservice (happens to be a peoplesoft EIP service) that

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.