Welcome to Dream.In.Code
Click Here
Getting Java Help is Easy!

Join 118,888 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 2,077 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Cybercontrol server

 
Reply to this topicStart new topic

Cybercontrol server, sql,swings,sockets

prajayshetty
post 30 Jun, 2008 - 12:51 PM
Post #1


D.I.C Head

Group Icon
Joined: 27 Apr, 2007
Posts: 223



Dream Kudos: 25
My Contributions


client code completed still
CODE


package cybercontrol;
import java.awt.event.ActionEvent;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
// @contribution DIC

class Main  extends JFrame

{
    String s;
    int p;  
    
      String names,pass;
    JLabel Name,Password;
JButton Connect,Setting;
String test1;
final int width=400;
  final int height=500;
  Socket socket;
ServerSocket serversocket;
ObjectInputStream cinput;
ObjectOutputStream coutput;
  Gui2 g =new Gui2();
   Main()
{
    
    setTitle("CyberControl");
    

     Name = new JLabel("Login");
     Password =new JLabel("Password");
     Connect = new JButton("Connect");
     Setting=new JButton("Setting");
    Container cp=getContentPane();
     cp.setLayout(new GridLayout (4,2));
     cp.add(Name);
     cp.add(Password);
     cp.add(Connect);
     cp.add(Setting);
     Creator cc = new Creator();
     Setting.addActionListener(cc);
     connector ccs = new connector();
    Connect.addActionListener(ccs);
    
     setSize(width,height);
     setVisible(true);
    
     setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

private  class connector implements ActionListener
{

        public void actionPerformed(ActionEvent e) {
        
           names= Name.getText();
            pass =Password.getText();
            
            g.communication();
        
        }
    
}
   private class Creator implements ActionListener
{
      
public void actionPerformed(ActionEvent e)
{
  
    
    
   g.setVisible(true);
      }
    }  

public static void main(String args[])
{
    Main m = new Main();
  
    
    
}
class Gui2 extends JFrame
{
public String servers;
public int ports;
JLabel  Serverip,serverport;
JTextField server,port;
JButton ok;
okhandler range;

    
     Gui2()
     {
    
         setTitle("Setting ");
     Serverip = new JLabel("IP");
     serverport=new JLabel("PORT");
     server=new  JTextField();
     port =new JTextField();
     ok = new JButton("ok");
    
     setSize(200,400);
    
     Container guicp =getContentPane();
     guicp.setLayout(new GridLayout(4,1));
     guicp.add(Serverip);
     guicp.add(server);
     guicp.add(serverport);
     guicp.add(port);
     guicp.add(ok);  
range = new okhandler();

ok.addActionListener( range);
setVisible(false);
}  
void communication()
      
{
Gui2 g =new Gui2();

try
    {
    socket= new Socket(servers,700);
}catch(Exception e)
{
System.out.println("Error connecting server");
System.exit(0);
}
System.out.println("Connected to Server");
try
{
System.out.println("creating input/output stream");
    cinput= new ObjectInputStream(socket.getInputStream());
coutput=new ObjectOutputStream(socket.getOutputStream());
}catch(IOException e)
{
System.out.println("Error creating stream");
System.exit(0);
}
try
{
      
    System.out.println("Input stream created sending authentication string to serve");
  
    try{
        coutput.wait(500);
    }catch(InterruptedException e)
    {
    }
    coutput.writeObject("HII");
     test1 = (String) cinput.readObject();
}catch(IOException e)
{        
System.out.println("Authentication failed server rejected");
System.exit(0);
}
catch(ClassNotFoundException f)
{
}
if(test1.equalsIgnoreCase("ready"))
{

    System.out.println("Connection Authenticated");
}else
{
System.out.println("Connection Rejected");
System.exit(0);

}

}
    
     private class okhandler implements ActionListener
{

        public void actionPerformed(ActionEvent e)
        {
      
        
          servers=server.getText();
      
      
        
          g.setVisible(false);
  }
    
}

}      
}
  

server side code
CODE


package cybercontrolserver;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

/*
*
* @author PrajayShetty
* @version test
* @contributor dic
*
*/




public class Main extends JFrame{

    
   ServerSocket serversocket;


Main()
{
    try

  {
    serversocket= new ServerSocket(700);
  
    System.out.println("socket initialized");
  
    while(true)
   {
       try{
          
      
       Socket socket=serversocket.accept();
       System.out.println("new Client requested connection");
      TcpThread t = new TcpThread(socket);
       System.out.println("connection created");  
        
       }catch(IOException e)
       {
           System.out.println("Error creating server connetion");
       }
   }
   }catch(IOException e)
   {
       System.out.println("Error creating server connection");
      
   }
   }
  

class TcpThread extends Thread
{

    Socket socket;
    
        ObjectOutputStream soutput;
        
        ObjectInputStream sinput;
        
    TcpThread(Socket socket)
    {
        this.socket =socket;
        
     }
        
public void run()
{
    try{
        System.out.println("Creating input/output stream for the client");
        sinput =new ObjectInputStream(socket.getInputStream());
        soutput = new ObjectOutputStream(socket.getOutputStream());
        
        
    }
catch(IOException e)
{
    System.out.println("Error in creating input Stream");
    return;
}
try{
    String str=(String) sinput.readObject();
  if(str.equalsIgnoreCase("HII"))
  {
soutput.writeObject("Ready");
System.out.println("Connection authentication completed succesfully");
soutput.flush();

  }
    


}catch(IOException e)
{
    System.out.println("connection rejected");
    
    return;
}catch(ClassNotFoundException e)
{
}
System.out.println("Connection Accepted");


}
}
  
    
    
    
    
    
    
    
    
    
    
    public static void main(String[] args)
    {
        // TODO code application logic here
    Main m = new Main();
    }

}

the client app just hangs

This post has been edited by prajayshetty: 30 Jun, 2008 - 12:53 PM
User is offlineProfile CardPM

Go to the top of the page


pbl
post 30 Jun, 2008 - 12:54 PM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,339



Thanked 135 times

Dream Kudos: 75
My Contributions


And the question is ?
User is offlineProfile CardPM

Go to the top of the page

prajayshetty
post 30 Jun, 2008 - 12:59 PM
Post #3


D.I.C Head

Group Icon
Joined: 27 Apr, 2007
Posts: 223



Dream Kudos: 25
My Contributions


QUOTE(pbl @ 30 Jun, 2008 - 12:54 PM) *

And the question is ?

it hangs i dont know why the client app hangs after creation of input/output stream
User is offlineProfile CardPM

Go to the top of the page

pbl
post 30 Jun, 2008 - 01:09 PM
Post #4


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,339



Thanked 135 times

Dream Kudos: 75
My Contributions


QUOTE(prajayshetty @ 30 Jun, 2008 - 12:59 PM) *

QUOTE(pbl @ 30 Jun, 2008 - 12:54 PM) *

And the question is ?

it hangs i dont know why the client app hangs after creation of input/output stream

You don't start your TcpThread in the server...
User is offlineProfile CardPM

Go to the top of the page

prajayshetty
post 30 Jun, 2008 - 01:53 PM
Post #5


D.I.C Head

Group Icon
Joined: 27 Apr, 2007
Posts: 223



Dream Kudos: 25
My Contributions


client
CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package cybercontrol;
import java.awt.event.ActionEvent;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
// @contribution DIC

class Main  extends JFrame

{
    String s;
    int p;  
    
      String names,pass;
    JLabel Name,Password;
JButton Connect,Setting;
String test1;
final int width=400;
  final int height=500;
  Socket socket;
ServerSocket serversocket;

  Gui2 g =new Gui2();
   Main()
{
    
    setTitle("CyberControl");
    

     Name = new JLabel("Login");
     Password =new JLabel("Password");
     Connect = new JButton("Connect");
     Setting=new JButton("Setting");
    Container cp=getContentPane();
     cp.setLayout(new GridLayout (4,2));
     cp.add(Name);
     cp.add(Password);
     cp.add(Connect);
     cp.add(Setting);
     Creator cc = new Creator();
     Setting.addActionListener(cc);
     connector ccs = new connector();
    Connect.addActionListener(ccs);
    
     setSize(width,height);
     setVisible(true);
    
     setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

private  class connector implements ActionListener
{

        public void actionPerformed(ActionEvent e) {
        
           names= Name.getText();
            pass =Password.getText();
            
            g.communication();
        
        }
    
}
   private class Creator implements ActionListener
{
      
public void actionPerformed(ActionEvent e)
{
  
    
    
   g.setVisible(true);
      }
    }  

public static void main(String args[])
{
    Main m = new Main();
  
    
    
}
class Gui2 extends JFrame
{
public String servers;
public int ports;
JLabel  Serverip,serverport;
JTextField server,port;
JButton ok;
ObjectInputStream cinput;
ObjectOutputStream coutput; private okhandler range;
    
     Gui2()
     {
    
         setTitle("Setting ");
     Serverip = new JLabel("IP");
     serverport=new JLabel("PORT");
     server=new  JTextField();
     port =new JTextField();
     ok = new JButton("ok");
    
     setSize(200,400);
    
     Container guicp =getContentPane();
     guicp.setLayout(new GridLayout(4,1));
     guicp.add(Serverip);
     guicp.add(server);
     guicp.add(serverport);
     guicp.add(port);
     guicp.add(ok);  
range = new okhandler();

ok.addActionListener( range);
setVisible(false);
}  
void communication()
      
{
Gui2 g =new Gui2();

try
    {
    socket= new Socket(servers,700);
}catch(Exception e)
{
System.out.println("Error connecting server");
System.exit(0);
}
System.out.println("Connected to Server");
try
{
System.out.println("creating input/output stream");
  
coutput=new ObjectOutputStream(socket.getOutputStream());
cinput= new ObjectInputStream(socket.getInputStream());
System.out.println("completed...");
}catch(IOException e)
{
System.out.println("Error creating stream"+e);
System.exit(0);
}
try
{
      
    System.out.println("Input stream created sending authentication string to serve");
  
  
    coutput.writeObject("HII");
    coutput.flush();
    
}catch(IOException e)
{        
System.out.println("Authentication failed server rejected");
System.exit(0);
}



if(test1.equalsIgnoreCase("ready"))
{

    System.out.println("Connection Authenticated");
}else
      
{
System.out.println("Connection Rejected");
System.exit(0);

}

}
    
     private class okhandler implements ActionListener
{

        public void actionPerformed(ActionEvent e)
        {
      
        
          servers=server.getText();
      
      
        
          g.setVisible(false);
  }
    
}

}      
}
  

server
CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package cybercontrolserver;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

/*
*
* @author PrajayShetty
* @version test
* @contributor dic
*
*/




public class Main extends JFrame{

    
   ServerSocket serversocket;


Main()
{
    try

  {
    serversocket= new ServerSocket(700);
  
    System.out.println("socket initialized");
  
    while(true)
   {
       try{
          
      
       Socket socket=serversocket.accept();
       System.out.println("new Client requested connection");
      TcpThread t = new TcpThread(socket);
       System.out.println("connection created");  
        t.start();
       }catch(IOException e)
       {
           System.out.println("Error creating server connetion");
       }
   }
   }catch(IOException e)
   {
       System.out.println("Error creating server connection");
      
   }
   }
  

class TcpThread extends Thread
{

    Socket socket;
    
        ObjectOutputStream soutput;
        
        ObjectInputStream sinput;
        
    TcpThread(Socket socket)
    {
        this.socket =socket;
        
     }
        
        @Override
public void run()
{
    try{
        System.out.println("Creating input/output stream for the client");
      
        soutput = new ObjectOutputStream((socket.getOutputStream()));
         sinput =new ObjectInputStream(socket.getInputStream());
        System.out.println("Reached");
        
    }
catch(IOException e)
{
    System.out.println("Error in creating input Stream"+e);
    return;
}
try{
    System.out.println("waiting to read");
    String str=(String) sinput.readObject();
  if(str.equalsIgnoreCase("HII"))
  {
soutput.writeObject("Ready");
System.out.println("Connection authentication completed succesfully");
soutput.flush();

  }
    


}catch(IOException e)
{
    System.out.println("connection rejected");
    
    return;
}catch(ClassNotFoundException e)
{
}
System.out.println("Connection Accepted");


}
}
  
    
    
    
    
    
    
    
    
    
    
    public static void main(String[] args)
    {
        // TODO code application logic here
    Main m = new Main();
    }

}

weee finally fixed

any good tuitorial for jdbc

This post has been edited by prajayshetty: 2 Jul, 2008 - 11:31 AM
User is offlineProfile CardPM

Go to the top of the page

prajayshetty
post 3 Jul, 2008 - 12:34 PM
Post #6


D.I.C Head

Group Icon
Joined: 27 Apr, 2007
Posts: 223



Dream Kudos: 25
My Contributions


added some gui to server
CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package cybercontrolserver;

import javax.swing.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

/*
*
* @author PrajayShetty
* @version test
* @contributor dic
*
*/
public class Main extends JFrame {

    String temp;
    ServerSocket serversocket;

    Main() {
        System.out.println("Reached");

        try {
            
            serversocket = new ServerSocket(700);

            System.out.println("socket initialized");

            while (true) {
                try {


                    Socket socket = serversocket.accept();
                    System.out.println("new Client requested connection");
                    TcpThread t = new TcpThread(socket);
                    System.out.println("connection created");
                    t.start();
                } catch (IOException e) {
                    System.out.println("Error creating server connetion");
                }
            }
        } catch (IOException e) {
            System.out.println("Error creating server connection");

        }
    }

    class TcpThread extends Thread {

        Socket socket;
        ObjectOutputStream soutput;
        ObjectInputStream sinput;

        TcpThread(Socket socket) {
            this.socket = socket;

        }

        @Override
        public void run() {
            try {
                System.out.println("Creating input/output stream for the client");

                soutput = new ObjectOutputStream((socket.getOutputStream()));
                sinput = new ObjectInputStream(socket.getInputStream());
                System.out.println("Reached");

            } catch (IOException e) {
                System.out.println("Error in creating input Stream" + e);
                return;
            }
            try {
                System.out.println("waiting to read");
                String str = (String) sinput.readObject();

                temp = "HII";
                if (str.equalsIgnoreCase(temp));
                {
                    soutput.writeObject("Ready");
                    System.out.println("Connection authentication completed succesfully");
                    soutput.flush();

                }



            } catch (IOException e) {
                System.out.println("connection rejected");

                return;
            } catch (ClassNotFoundException e) {
            }
            System.out.println("Connection Accepted");


        }
    }


    
    public static void main(String[] args) {
        // TODO code application logic here
        Main m = new Main();
   Gui g =new Gui();
  


    }
}

  class Gui extends JFrame {

        JTextField usrtext, passtext;
        JButton add, delete, table,exit;
        JLabel username, password;

        Gui() {
            //textbox
            setTitle("User");

            usrtext = new JTextField();
            passtext = new JTextField();
            
            //buttons
            add = new JButton("add a user");
            delete = new JButton("delete a user");
            table = new JButton("Status");
            exit = new JButton("Exit");
            //labels
            username = new JLabel("Username");
            password = new JLabel("Password");
            //setting layouts and initializing gui

            Container con = getContentPane();
            con.setLayout(new GridLayout(4, 1));
            //adding components
            setSize(500, 300);
             con.add(username);
            con.add(usrtext);
            con.add(password);
            con.add(passtext);
            System.out.println("1");
           con.add(add);
          
            con.add(delete);
            con.add(table);
            con.add(exit);
            
              
            System.out.println("called");

            
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

        }

      
    }


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/13/08 03:16AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month