How this code can be made for mutilpe clients

Posted on 2004-11-16
Last Modified: 2010-04-17
Here is my client server code..the server donot send any data to clientt, in the second client window. What I mean is the code should support multiple client and send  calues to multiple cllients...and upon pressing the exit button on the window is not exiting ..can any one makes change to code and do it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame

     private JTextField textConnection;
     private JLabel labelConnection;
     Container container = null;

     public Server()


           container = getContentPane() ;
           container.setLayout(new FlowLayout());

           labelConnection = new JLabel("Connection ") ;
           container.add(labelConnection );
           textConnection = new JTextField(15) ;

          setSize ( 200,400) ;

               //creating server socket binding at port # 3000
               ServerSocket server=new ServerSocket(3000);
               System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":3000");
               System.out.println("Run the Client");
               //ready to accept client request
               Socket socket=server.accept();

               //opening the input stream to read data from client connection
               BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
               //using output stream responsing data
               DataOutputStream out=new DataOutputStream(socket.getOutputStream());
               while (true)
                  for (int i=0; i<8; i++)
                      int s =(int)(Math.random()*10);
               //closing the in & out streams
            //   in.close();
          catch(Exception err)
               System.err.println("* err"+err);


     public static void main(String a[])
          new Server();



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame

     private JTextField textsky,texttemp,texthumidity,textwindspeed,textbarometer,textdew,textheat,textvisibility;
     private JLabel labelsky,labeltemp,labelhumidity,labelwindspeed,labelbarometer,labeldew,labelheat,labelvisibility;

     public Client()
                        Container container = getContentPane() ;
                        container.setLayout(new FlowLayout());

                         labelsky = new JLabel("Sky ") ;
                         container.add(labelsky );
                         textsky = new JTextField(15) ;

                        labeltemp = new JLabel("Temp ") ;
                        container.add(labeltemp );
                        texttemp = new JTextField(15) ;
                        container.add(texttemp );

                        labelhumidity = new JLabel("Humidity ") ;
                        container.add(labelhumidity );
                        texthumidity = new JTextField(15) ;
                        container.add(texthumidity );

                        labelwindspeed = new JLabel("Wind speed ") ;
                        container.add(labelwindspeed );
                        textwindspeed = new JTextField(15) ;
                        container.add(textwindspeed );

                        labelbarometer = new JLabel("Barometer ") ;
                        container.add(labelbarometer );
                        textbarometer = new JTextField(15) ;
                        container.add(textbarometer );

                        labeldew = new JLabel("Dew ") ;
                        container.add(labeldew );
                        textdew = new JTextField(15) ;
                        container.add(textdew );

                        labelheat = new JLabel("Heat ") ;
                        container.add(labelheat );
                        textheat = new JTextField(15) ;
                        container.add(textheat );

                        labelvisibility = new JLabel("Visibillity ") ;
                        container.add(labelvisibility );
                        textvisibility = new JTextField(15) ;
                        container.add(textvisibility );

                        setSize ( 200,600) ;

               //ceating the socket to connect to server running on same machine binded on port no 3000
               Socket client=new Socket("localhost",3000);
               System.out.println("Client connected ");

               //getting the o/p stream of that connection
               PrintStream out=new PrintStream(client.getOutputStream());
               //sending the message to server
               out.print("Hello from client\n");
               //reading the response using input stream
               DataInputStream in= new DataInputStream(client.getInputStream());
               while (true)
                           if(in.readInt()>20 && in.readInt()<40)
                  textsky.setText("Sky Condition: "+"Clear");
                              if(in.readInt()>40 && in.readInt()<80)
                          textsky.setText("Sky Condition: "+"Partly Cloudy");
                          textsky.setText("Sky Condition: "+"Cloudy");
                  texttemp.setText("Temperature: "+in.readInt());
                  texthumidity.setText("Humidity: "+in.readInt());
                  textwindspeed.setText("Wind Speed: "+in.readInt());
                  textbarometer.setText("Barometer: "+in.readInt());
                  textdew.setText("Dewpoint: "+in.readInt());
                  textheat.setText("Heat Index: "+in.readInt());
                  textvisibility.setText("Visibility: "+in.readInt());

               //closing the streams

          catch(Exception err)
               System.err.println("* err"+err);


     public static void main(String a[])
          new Client();

Question by:prashanth_gurijala
    1 Comment
    LVL 49

    Accepted Solution

    Basically the problem is that you are not passing off connected clients to some other code for processing in a separate thread so that your server code can listen for new client connection requests.

    In other words you should be doing something like

    Listent For connections
    Accept Connection
    Duplicate socket
    Create worker thread (or use thread already running) to handle client request
    return to listen for connections.

    I am not a Java Guru so I am not able to post code but that is the essence of your problem. What you need to do is look at other java client / server source examples on how to pass socket connections off to worker threads and return your main thread to a listening state for new socket requests.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    mirrorEnds challenge 6 66
    withoutTen challenge 14 68
    thread-safe code in c++ 2 58
    Order table with macro 3 65
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now