Results 1 to 4 of 4
- 02-04-2010, 03:17 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 27
- Rep Power
- 0
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){}
}
}
}
- 02-06-2010, 05:58 AM #2
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
- 02-06-2010, 03:36 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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?
- 02-28-2010, 07:26 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Request: FTP-server code
By Limro in forum JDBCReplies: 4Last Post: 02-28-2009, 08:06 PM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Server not able to connect client
By Kapil Gupta in forum New To JavaReplies: 15Last Post: 07-22-2008, 04:09 AM -
Identify Client in Socket Client Server Application
By masadjie in forum NetworkingReplies: 1Last Post: 12-20-2007, 09:18 AM -
How to post HTTPS request from java client to server
By Desai in forum NetworkingReplies: 1Last Post: 07-14-2007, 05:15 PM


LinkBack URL
About LinkBacks

Bookmarks