Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Showing the connection status on server side interface  and also adding images to client side interface

Posted on 2004-10-05
2
Medium Priority
?
232 Views
Last Modified: 2010-03-31
Here are my client side and server side codes.Can any one integrate a code into it.where after connecting  client to server. The status on the server side interface (in text box) is shown as connected else disconnected. and here program shows on clients side the temperature,cloud condition, etc..can any one add images on side of those labels ..eg for skies ..small icon of clouds..temperature..image of sun ...etc..
-------------
server code
.........................
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame
{


      private JTextField textConnection;
      private JLabel labelConnection;


     public Server()

     {

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

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

            setSize ( 200,400) ;
            setVisible(true);

          try
          {
               //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()));
               System.out.println(in.readLine());
               //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);
                      out.writeInt((s*10)+20);
                  }
                  out.flush();
               }
               //closing the in & out streams
                     //out.close
            //   in.close();
          }
          catch(Exception err)
          {
               System.err.println("* err"+err);
          }

     }

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

}

client side code
.................................
import java.net.*;
import java.io.*;
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) ;
                        container.add(textsky);


                        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) ;
                        setVisible(true);

          try
          {
               //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");
               out.flush();
               //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");
                              else
                          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());
                  Thread.sleep(5000);
               }

               //closing the streams
               //in.close();
               //out.close();

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

     }

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

}
0
Comment
Question by:prashanth_gurijala
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
ThummalaRaghuveer earned 1000 total points
ID: 12232474
Following code at server will do for showing status.......


import java.net.*;
import java.io.*;
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) ;
           container.add(textConnection);
           textConnection.setText("Disconnected");

          setSize ( 200,400) ;
          setVisible(true);

          try
          {
               //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();
               textConnection.cut();
               textConnection.setText("Connected\n");
               
               
               //opening the input stream to read data from client connection
               BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
               System.out.println(in.readLine());
               //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);
                      out.writeInt((s*10)+20);
                  }
                  out.flush();
               }
               //closing the in & out streams
                  //out.close
            //   in.close();
          }
          catch(Exception err)
          {
               System.err.println("* err"+err);
               textConnection.cut();
               textConnection.setText("Disconnected\n");
          }

     }

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

}
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12232662
U do not need textConnection.cut() method and also the \n after every message
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

670 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