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