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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:08:01+00:00 2026-06-18T05:08:01+00:00

We have a java socket program where the server gets data from many devices

  • 0

We have a java socket program where the server gets data from many devices and works fine. At times the server needs to send some command to the devices. When it sends individual commands it works fine. The problem comes when it sends multiple commands, only the first one is successful. We cant figure out why the rest fails. Below is the snippet showing how the message is sent. Should I set a delay after a message is sent?

public static void main(String[] args) {   

      new sServer7888();

   }
sServer7888() {


    try{
    final ServerSocket serverSocketConn = new ServerSocket(7888);               
    while (true){
    try{
       Socket socketConn1 = serverSocketConn.accept();
          new Thread(new ConnectionHandler(socketConn1)).start();                       
    }
    catch(Exception e){
        e.printStackTrace(System.out);
    }
       }
    } 
    catch (Exception e)     {
         e.printStackTrace(System.out);
    }

}


class ConnectionHandler implements Runnable {

  private Socket receivedSocketConn1;
    ConnectionHandler(Socket receivedSocketConn1) {
      this.receivedSocketConn1=receivedSocketConn1;
    }

    public void run() {

      while ((nextChar=readIn1.read()) != -1) {

         completeMessage += (char) nextChar;     
         if (nextChar == '*')
    {
         String[] splitResult = completeMessage .split(",");    
         String header=splitResult[0].trim().substring(0,4);

         if((header.equals("$ACK")){

          //update the message sent from the server as already acknowledge.
         }     
         else{
          //run query to find if there are any message to be sent out to the devices
          while(rsOC1.next()){
            commandText = rsOC1.getString("commandText");
            writeOut1.write(commandText);
            writeOut1.write("\r\n");
            writeOut1.flush(); 
          }

          //now process the normal message receive from the devices.
         } 
        completeMessage="";
       }   
      }
   }
}
  • 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-18T05:08:02+00:00Added an answer on June 18, 2026 at 5:08 am

    If your device is sending ACK on Every message and Server is able to receive it then you can proceed in following way with your server side program.
    EDIT
    I have updated the code as per the requirement analysis. Let me know if any discrepancy is found after implementing it.

    Thread.sleep(1000) is not the reliable solution for above case because
    we are not knowing how long the device might take to execute previous
    command sent by Server .

        public void run() 
        {
    
            int i = -1;
            ArrayList<String> list = new ArrayList<String>();
            while ((nextChar=readIn1.read()) != -1) 
            {
                boolean isCompleteMessage = readMessage(nextChar);
                if (isCompleteMessage)
                {
                    String[] splitResult = completeMessage .split(",");    
                    String header=splitResult[0].trim().substring(0,4);
                    if((header.equals("$ACK"))
                    {
                        String id = null;
                        if (i != -1)
                        {
                            id = list.get(i);
                            id = id.substring(0,id.indexOf("^"));
                        }
                        //update the message sent from the server as already acknowledge using id extracted above.
                        if ( i == 0)
                        {
                            list.remove(i);
                            if (list.size() == 0)
                            {
                                i = -1;
                            }
                            else
                            {
                                commandText = list.get(i);
                                writeOut1.write(commandText.substring((commandText.indexOf("^")) + 1));
                                writeOut1.write("\r\n");
                                writeOut1.flush(); 
                            }
                        }
                    }     
                    else
                    {
                        //process here the normal message receive from the devices.
                        if (i == -1)
                        {
                            list = getRecords();
                            if (list.size() > 0)
                            {
                                i = 0;
                                commandText = list.get(i);
                                writeOut1.write(commandText.substring((commandText.indexOf("^")) + 1));
                                writeOut1.write("\r\n");
                                writeOut1.flush(); 
                            }
                        }
                        else 
                        {
                            commandText = list.get(i);
                            writeOut1.write(commandText.substring((commandText.indexOf("^")) + 1));
                            writeOut1.write("\r\n");
                            writeOut1.flush(); 
                        }
                    } 
                    completeMessage = "";
                }   
            }
       }
       public boolean readMessage(int nextChar)
       {
            completeMessage += (char)nextChar;
            if (((char)nextChar) == '*')
            {
                return true;
            }
            else
            {
                return false;
            }
       }
       //Retreive all commands from database and returns the ArrayList containing those commands.
       public ArrayList<String> getRecords()
       {
           ArrayList<String> list = new ArrayList<String>();
           Statement stat = null;
           ResultSet rsOC1 = null;
           try
           {
                stat = con.createStatement();
                rsOC1 = stat.executeQuery("Query for message retrieval from database");
                while (rsOC1.next())
                {
                    String sElement = rs0C1.getString("commandID") + "^" + rs0C1.getString("commandText");
                    list.add(sElement);
                }
           }
           catch (Exception ex){}
           finally
           {
               if (rs0C1 != null)
               {
                   try
                   {
                        rs0C1.close();   
                   } catch () {}
               }
               if (stat != null)
               {
                   try
                   {
                        stat.close();   
                   } catch () {}
               }
               return list;
           }
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a TCP socket client-server program in Java. From the client, I send
I have a Java program that creates a server socket and accepts connections from
I have a Java socket server program listening on a specific port. When receiving
I have developed a Java server using Eclipse that accepts TCP socket connection from
I have a Java program that opens a socket connection to a server that
I have a Java program that mirrors a connection from a client server to
I have written a java socket server program which listens to a port continuously.
I have a java program which has a server-socket listening infinitely. In case of
I have a DataInputStream , created from a Socket in Java. The connection is
I have a problem with my Java program. It has a socket connection between

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.