How to block Client request to connect with server
Hello everyone,
I want to block client's direct connection with server, The message should pop up at server saying so n so client wants to connect with it if server click on YES then only it should get connected otherwise message should send to client connection refused.
so far, I'm working on this codes.
=============
client side...
=============
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author abbas khan
*/
class Client extends JFrame {
private JLabel lblMsg;
private JButton btnConnect;
private JPanel contents;
public Client()
{
super();
initComponents();
this.setVisible(true);
}
public void initComponents()
{
lblMsg = new JLabel();
lblMsg.setText("Connect to Server...");
btnConnect = new JButton();
btnConnect.setText("Connect...");
btnConnect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
btnConnect_actionPerformed(e);
}
});
contents = (JPanel)this.getContentPane();
contents.setLayout(null);
addComponent(contents, lblMsg, 180, 0, 130, 50);
addComponent(contents, btnConnect, 180, 50, 100, 50);
this.setTitle("Client Application");
this.setLocation(new Point(400, 200));
this.setSize(new Dimension(500, 200));
this.setDefaultCloseOperation(WindowConstants.DISP OSE_ON_CLOSE);
}
public void getConnect2()
{
String host = "localhost";
/** Define a port */
int port = 9999;
StringBuffer instr = new StringBuffer();
String TimeStamp;
System.out.println("SocketClient initialized");
try {
/** Obtain an address object of the server */
InetAddress address = InetAddress.getByName(host);
/** Establish a socket connetion */
Socket connection = new Socket(address, port);
/** Instantiate a BufferedOutputStream object */
BufferedOutputStream bos = new BufferedOutputStream(connection.
getOutputStream());
/** Instantiate an OutputStreamWriter object with the optional character
* encoding.
*/
OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
TimeStamp = new java.util.Date().toString();
String process = "Client wants to Connect with Server on "+ host + " port " + port +
" at " + TimeStamp + (char) 13;
osw.write(process);
osw.flush();
/** Instantiate a BufferedInputStream object for reading
/** Instantiate a BufferedInputStream object for reading
* incoming socket streams.
*/
BufferedInputStream bis = new BufferedInputStream(connection.
getInputStream());
/**Instantiate an InputStreamReader with the optional
* character encoding.
*/
InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");
/**Read the socket's InputStream and append to a StringBuffer */
int c;
while ( (c = isr.read()) != 13)
instr.append( (char) c);
/** Close the socket connection. */
connection.close();
JOptionPane.showMessageDialog(null,instr);
//System.out.println(instr);
}
catch (IOException f) {
System.out.println("IOException: " + f);
}
catch (Exception g) {
System.out.println("Exception: " + g);
}
}
private void btnConnect_actionPerformed(ActionEvent e)
{
getConnect2();
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
public static void main(String args[])throws IOException
{
Client c = new Client();
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
====================
Server Side...
====================
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Server implements Runnable{
private Socket connection;
private String TimeStamp;
private int ID;
public String msg="hello server";
public Server(Socket client,int i)
{
this.connection = client;
this.ID = i;
}
public Server()
{
int port = 9999;
int count = 0;
try{
ServerSocket socket1 = new ServerSocket(port);
System.out.println("Main Server Initialized");
while (true) {
int i=JOptionPane.showConfirmDialog(null,"Client wants to connect","Access",JOptionPane.YES_NO_OPTION);
if(i==0){
Socket connection = socket1.accept();
Runnable runnable = new Server(connection, ++count);
Thread thread = new Thread(runnable);
thread.start();
}else if(i==1){
break;
}
}
}catch(Exception e)
{System.out.println(e.getMessage());}
}
public void run() {
try
{
//Message from client
BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
InputStreamReader isr = new InputStreamReader(is);
int character;
StringBuffer process = new StringBuffer();
while((character = isr.read()) != 13) {
process.append((char)character);
}
try {
//Client need to wait for 10 seconds
Thread.sleep(10000);
}
catch (Exception e){}
TimeStamp = new java.util.Date().toString();
String returnCode = "Access Granted at "+ TimeStamp + (char) 13;
BufferedOutputStream os = new BufferedOutputStream(connection.getOutputStream()) ;
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
osw.write(returnCode);
osw.flush();
}
//Message End
catch (Exception e) {
System.out.println(e);
}
finally {
try {
connection.close();
}
catch (IOException e){}
}
}
}