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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:16:21+00:00 2026-06-08T09:16:21+00:00

I am suffering with this codes since two days now. In fact I am

  • 0

I am suffering with this codes since two days now. In fact I am developping an application which has a server side and the clients side. The server receives request from clients every seconds, treat the request by contacting the database and then send back the result to the client.
I do such a way that if the client starts before the server, it will remain trying to connect to the server on the given port and the given host.
1. This is the server side:

try
    {   
        Client client = new Client(jTable1,jLabel3);
        Thread t = new Thread(client);
        t.start();

    }catch(IOException e){}

Class Client.java

public class Client implements Runnable{

private int svrPort = 0;
ServerSocket serverConnect = null;
static  Socket clientSocket = null;
static  ClientConnectThread t[] = new ClientConnectThread[1000];
JTable jtable;
JLabel jlabel;

public Client(JTable table, JLabel label) throws IOException {

    this.svrPort = 9450;
    this.jtable = table;
    this.jlabel = label;

}

public void run(){
    try{
        serverConnect = new ServerSocket(this.svrPort);

    }catch(IOException e){}
    while(true){
        try{
            clientSocket = serverConnect.accept ();
            for(int i=0; i<=1000; i++){ //I can accept up to 1000 clients
        if(t[i]==null)
        {
            (t[i] = new ClientThread(client, t, jtable, jlabel)).start();
                        System.out.println ("Etat12. Apres bloc try");
            break;
        }
    }
        }catch(IOException e){}
    }
}

}

Class ClientThread.java

public ClientThread(Socket socket, ClientThread t[], JTable table, JLabel label){

    this._socket = socket;
    this.jTable = table;
    this.jlabel = label;
    this.totalConnected = 0;       
    this.t = t;
}

public void run(){

    int index = 0;
    try{
        this._output = new PrintWriter(this._socket.getOutputStream ());
        this._input = new BufferedReader(new InputStreamReader(this._socket.getInputStream()));

        while((clientMsg = this._input.readLine ()) != null){
            if(clientMsg.equals ("@CONNECT")){ // If it is the first time the user is signig in, fill the table

                jTable.setValueAt (this._socket.getInetAddress (), index, 0);
                jTable.setValueAt (new Date(), index, 1);
                jTable.setValueAt (new Date(), index, 2);
                totalConnected++;
                jlabel.setText ("");
                jlabel.setText (totalConnected+"");

            }else if(Integer.parseInt (clientMsg) == 1){
                int p = Integer.parseInt (clientMsg);
                this._output = new PrintWriter(this._socket.getOutputStream(), true);
                if (this.getData.connect ())
                {
                    if(this.getData.getDataByType (1).size () == 0){
                    }
                    _output.println (this.getData.getDataByPeriod (1));
                }else{System.out.println("You are not connected to the database server");}

            }else if(Integer.parseInt (clientMsg) == 2){
                int p = Integer.parseInt (clientMsg);
                this._output = new PrintWriter(this._socket.getOutputStream(), true);
                if (this.getData.connect ())
                {
                    if(this.getData.getDataByPeriod (2).size () == 0)System.out.println ("There is no data corresponding");
                    this._output.println (this.getData.getDataByPeriod (2));
                }else{System.out.println("You are not connected to the database server");}


            }else if(Integer.parseInt (clientMsg) == 3){
                int p = Integer.parseInt (clientMsg);
                this._output = new PrintWriter(this._socket.getOutputStream(), true);
                if (this.getData.connect ())
                {
                    if(this.getData.getDataByType (3).size () == 0)System.out.println ("There is no data corresponding");
                    this._output.println (this.getData.getDataByType (30));
                }else{System.out.println("You are not connected to the database server");}


            }else if(Integer.parseInt (clientMsg) == 4){
                int p = Integer.parseInt (clientMsg);
                this._output = new PrintWriter(this._socket.getOutputStream(), true);
                if (this.getData.connect ())
                {
                    if(this.getData.getDataByType (4).size () == 0)System.out.println ("There is no data corresponding");
                    this._output.println (this.getData.getDataByType (60));
                }else{System.out.println("You are not connected to the database server");}


            }else{

            }
        }
        this._input.close ();
        this._output.close ();
    }catch(IOException e){}

}

These are the two classes that make my server running. The class Client.java starts and waits to accept connection. When a client connects an instance of clientThread is created and associated to the client.
Till here every thing seems to be working well.

Client side

public class ServerConnect implements Runnable{

public static Socket clientSocket = null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static int port=9450;
public static String host = "127.0.0.1";
public static JLabel myLabel;
public static JButton button;
public static ResourceMap resourceMap;
private static String serverMsg = "";

public ServerConnect(JLabel jLabel, JButton b)
{
    jLabel.setText ("Trying to contact the server");
    myLabel = jLabel;
    button = b;
    port = Integer.parseInt("9450");
    host = "127.0.0.1";

        try{
            clientSocket = new Socket(host, port);
            }catch(IOException e){e.printStackTrace ();}

}

public void run()
{
    while(true){
        while(!this.connect ())
        {myLabel.setText ("You are not connected to the server : "+host);
         button.setEnabled (false);
            try{
                clientSocket = new Socket(host, port);
            }catch(IOException e){}
        }

        myLabel.setText ("You are connected to the server : "+host);
        button.setEnabled (true);
        try{
           out = new PrintWriter(clientSocket.getOutputStream(), true);
           out.println("@CONNECT");

           in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
           while((serverMsg = in.readLine ()) != null){
                System.out.println ("<=> :"+serverMsg);
           }
        }
        catch(IOException e){e.printStackTrace ();}
    }
}
private boolean connect()
{
    try{
        clientSocket = new Socket(host, port);
        return true;
    }catch(IOException e){}
    return false;
}}

My problem is that when the two sides are starting, the only thing the client sends @CONNECT, the server receives it and all stops here. If the client sends a requests again, the server does not answer.
I would like someone to show me step by step how I can setup this applications
– Server side. Accepts connections in thread with the WHILE loop
– Client side. In another thread to try to contact the server everytime to establish a connection
– In another thread again, client sends request to server
– The server is another thread request the information from the database and sends back to client.

I thank you so much for your help

  • 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-08T09:16:22+00:00Added an answer on June 8, 2026 at 9:16 am

    Ooohh its a bad idea to put everything onto a low level socket with a PrintWriter etc.
    I got several encoding and multithreading errors in the past. So my (of course a little bit slower, but easy to use) solution is: Jersey & Grizzly.

    The big advantage is: You can modify and extend your transfer objects very easily, without modifying your transport code(the low level socket writing)

    An interface…

    public interface I_ServiceCommons {
            public static final String  MEDIATYPE           = "text/xml";
            public static final String  MEDIATYPE_ENCODING  = I_ServiceCommons.MEDIATYPE + "; charset=utf-8";
            public static final String SERIVCENAME = "/FUNNY_SERVICE_V001";
        }
    

    The server-side code

    @Path(I_ServiceCommons.SERIVCENAME)
    public class YourService {
    
        @POST
        @Produces(I_ServiceCommons.MEDIATYPE)
        @Consumes(I_ServiceCommons.MEDIATYPE)
        public ResultObject request(final RequestObject yourRequest) {
            //process here your request in Server
        }
    }
    

    Your Client….

    public class abstract YourAbstractClient{
            protected Logger                            log             = Logger.getLogger(this.getClass());
            protected final String                      serviceUrl;
            private final WebResource                   resource;
    
            public YourAbstractClient(final String url) {
                this.serviceUrl = url + getService();
                this.resource = Client.create().resource(this.serviceUrl);
            }
    
            public abstract String getService();
    
            protected <RES, REQ> RES post(final Class<RES> resultClazz, final REQ req) {
                final Builder builder = this.resource.type(I_ServiceCommons.MEDIATYPE).accept(I_ServiceCommons.MEDIATYPE);
                try {
                    final RES result = builder.post(resultClazz, req);
                    return result;
                } catch (final Exception e) {
                    throw new RuntimeException("Error posting data to [" + this.serviceUrl + "]: " + e.getMessage(), e);
                }
            }
    
        }
    

    Your ServiceClient…

    public class Service extends YourAbstractClient {
    
        public Service(final String url) {
            super(url);
        }
    
        public MyResult getResult(final MyRequest req) {
            return super.post(MyResult.class, req);
        }
    
        @Override
        public String getService() {
            return I_ServiceCommons.SERIVCENAME;
        }
    }
    

    Your TransferObjects

    @XmlRootElement
    public class MyRequest implements Serializable {
        public void setRequestType(final int p_AequestType) {
            this.requestType = p_AequestType;
        }
    
        public int getRequestType() {
            return this.requestType;
        }
    }
    

    And the end…

    String url = "http://127.0.0.1";
    GrizzlyServerFactory.create(url) //starts the server
    MyRequest res = new MyRequest();
    MyResult result = new Service(url).getResult(req); // Corrected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application, which has been suffering high load recent days. The
I have a page which has document.body.onload=function(){...} but this is suffering delays because the
For some days now I have been suffering with UITabBarItem s. I have done
I'm trying to build an application that has two process that uses Shared memory
I have a form, which sets these styles in constructor: this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true);
i have a java applet application in which i use rich text area .
This stackoverflow question has an interesting discussion on how to avoid giving enums and
I'm trying to tweak an application that is suffering in speed department. Because of
When I try to export my android application, I get this error message.. [2012-05-02
So this isnt specific to a code sample, but we have two apps, 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.