Results 1 to 4 of 4
  1. #1
    mfaizan24 is offline Member
    Join Date
    Apr 2009
    Posts
    27
    Rep Power
    0

    Default 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){}
    }

    }
    }

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    I didn't try to read your code... i would suggest putting [ c o d e ] [ / c o d e ] (no spaces) around the code in your post to make it readable.

    There really isn't a way to block the connection from the server, that would be a hardware or OS problem. What you will have to do is allow the connection, get the info about the user (username or ip), and then disconnect the connection after that point. You can even send them a small message saying you disconnected the connection (like if they didn't authenticate or you said no).
    My Hobby Project: LegacyClone

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    366
    Rep Power
    6

    Default

    What is the real reason for block client's direct connection to server?

    What do you want to achieve by this?

    Is this about blocking some specific client and what about others
    in that case? Authentication?

  4. #4
    lohithbl is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    hi, can u tell me a step by step guide to block the client after he has connected... thnx

Similar Threads

  1. Request: FTP-server code
    By Limro in forum JDBC
    Replies: 4
    Last Post: 02-28-2009, 09:06 PM
  2. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  3. Server not able to connect client
    By Kapil Gupta in forum New To Java
    Replies: 15
    Last Post: 07-22-2008, 05:09 AM
  4. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  5. Replies: 1
    Last Post: 07-14-2007, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •