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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:10:34+00:00 2026-06-13T22:10:34+00:00

I have made a connection between my phone and my computer using the Socket-framework.

  • 0

I have made a connection between my phone and my computer using the Socket-framework. It work fine as long as i only need to send one command. Then i have to restart my server to recieve a new command. Can you help me figure out why? I’d like to be able to send multiple commands.

I’m calling the class-methods from a GUI-main-class.

My server code:

public class Server {

    private static PrintWriter out;
    private static BufferedReader in;
    private static ServerSocket serverSocket = null;
    private static Socket clientSocket = null;
    private static String inputLine, outputLine;

    public static void main() throws IOException {


        try {
            serverSocket = new ServerSocket(4444);
            System.out.println(serverSocket.getInetAddress().toString());
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }


        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        Protocol kkp = new Protocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             System.out.println(inputLine);
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
        }

    }

    public static void shutDown() throws IOException
    {
        System.exit(1);
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }

}

And my client code:

class ClientThread implements Runnable {

        public void run() {
            Socket kkSocket = null;
            PrintWriter out = null;
            BufferedReader in = null;


            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                kkSocket = new Socket(serverAddr, 4444);
                out = new PrintWriter(kkSocket.getOutputStream(), true);
                in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
            } catch (UnknownHostException e) {
                showToast("Kan ikke finde host fra: "+settings.getString("ip", "86.52.16.102"));
                System.err.println("Don't know about host: taranis.");
                System.exit(1);
            } catch (IOException e) {
                showToast("Kan ikke udveksle oplysninger med: "+settings.getString("ip", "86.52.16.102"));
                System.err.println("Couldn't get I/O for the connection to: taranis.");
                System.exit(1);
            }

            String fromServer;
            String fromUser;

            try {
                while ((fromServer = in.readLine()) != null) {
                    System.out.println("Server: " + fromServer);
                    if (fromServer.equals("Shutting down") || fromServer.equals("Wrong pin!"))
                    {
                        showToast(fromServer);

                        break;
                    }


                    //fromUser = stdIn.readLine();
                    fromUser = pinkode.getText().toString()+","+time;
                    if (fromUser != null) {
                        System.out.println("Client: " + fromUser);
                        out.println(fromUser);
                        fromUser = null;
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            out.close();
            try {
                in.close();
                kkSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
  • 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-13T22:10:35+00:00Added an answer on June 13, 2026 at 10:10 pm

    Try to put this part of the code:

        try {
            clientSocket = serverSocket.accept();
            } catch (IOException e) {
                 System.err.println("Accept failed.");
                System.exit(1);
             }
    
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        Protocol kkp = new Protocol();
    
        outputLine = kkp.processInput(null);
        out.println(outputLine);
    
        while ((inputLine = in.readLine()) != null) {
             System.out.println(inputLine);
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
        }
    

    Inside a while(true){ ... }.

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

Sidebar

Related Questions

I am using the XMPP Connection(using smack) for chat in android application.I have made
I have an applet that I want to maintain a socket connection between multiple
I have made a database structure where users can make friend connection between them
I have a simple class that handles the connection being made between a client
I have made a website using( Asp.net, c# ) and its content in English
I have made a String[] array by using String.split(.) . I am now trying
I have the following code try { using (var connection = new SqlConnection(Utils.ConnectionString)) {
I need to maintain a connection between a server and multiple clients so that
I am just learning the Entity Framework and have made some good progress on
I have to create a log file for all internet connections made by PC.

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.