Skip to main content

Socket communication between server and client using threads [closed]



I am busy with a project where the client Gui sends user and password details to a server where the details needs to be verified Sever side against the data in the database on the server. We are required to use Threads(from what I understand is that the Server needs to be able to handle multiple requests).





Where I get stuck is when the server is started it is set so that is can listen to requests from clients. However the Server gets stuck listening to client requests until it received a request from the client.





Furthermore on the client side if I send wrong username and password I struggle to re-submit the info for verification.





This is the code that I have used. Would just like to know if I am on the right track?





Firstly I start the server.







private void butStartUpServerActionPerformed(java.awt.event.ActionEvent evt) {



try

{



Class.forName(driverName);



connection = DriverManager.getConnection(SourceURL);



Statement verifyUsernamePassword = connection.createStatement();



user = txtUsernameServer.getText();



passwordChar = txtPasswordServer.getPassword();



password = new String(passwordChar);





//now I execute query using a statement object

ResultSet checkUsernamePassword = verifyUsernamePassword.executeQuery("SELECT * FROM User WHERE userName = '" + user + "' AND password = '" + password + "'");



//if cursor does move to next row indicating that a record has been found

if(checkUsernamePassword.next())

{

JOptionPane.showMessageDialog(null, "The server has been started successfully");

//starting up a server socket connection

ServerSoc = new ServerSocket(7777);



listening = true;



butShutDownServer.setEnabled(true);



butStartUpServer.setEnabled(false);



while(listening)

{

boolean verifyAdministratorDetails = false;





clientSocket = ServerSoc.accept();





in = new DataInputStream(clientSocket.getInputStream());



BufferedReader is = new BufferedReader(new InputStreamReader(in));



os = new PrintStream(clientSocket.getOutputStream());



String AdministratorDetails = is.readLine();

boolean adminBoolenDetails = Boolean.parseBoolean(AdministratorDetails);

if(adminBoolenDetails == true)

{

//I userNameAdminLogin from client side

String userNameAdminLogin = is.readLine();

//I userNameAdminLogin from client side

String passwordAdminLogin = is.readLine();

//Now I check if the details exist in the database

Statement statementVerifyAdminDetailsClientSide = connection.createStatement();



ResultSet verifyAdminDetailsClientSide = statementVerifyAdminDetailsClientSide.executeQuery("SELECT * FROM User WHERE userName = '" + userNameAdminLogin + "' AND password = '" + passwordAdminLogin + "'");



boolean verificationSuccess;

if(verifyAdminDetailsClientSide.next())

{

verificationSuccess = true;

listening = false;



}

else

{

verificationSuccess = false;

listening = true;

}

os.println(verificationSuccess);







}







}



}

else

{

JOptionPane.showMessageDialog(null, "Please enter correct user name and password to start up server (check spelling)");

}







Now I run the client. Constructor.







public class Administrator extends javax.swing.JFrame implements Runnable







{ Socket clientSocket = null; PrintWriter out = null; BufferedReader in; Thread runner;







public Administrator()

{

super("Administrator Window");

initComponents();

try

{

//I call the socket constructor

clientSocket = new Socket("192.168.56.1", 7777);





}

catch(UnknownHostException uh){}

catch(IOException io)

{

JOptionPane.showMessageDialog(null, io.toString());

}

catch(NullPointerException nullpoint)

{

JOptionPane.showMessageDialog(null, nullpoint.toString());

}

}







I run the thread in the button eventhandler...is that right?







private void butSubmitActionPerformed(java.awt.event.ActionEvent evt) {



//I open InputStream and OutputStream

try

{

in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

out = new PrintWriter(clientSocket.getOutputStream(), true);



}

catch(IOException io)

{

JOptionPane.showMessageDialog(null, io.toString());

}





//now i start my thread

//starting the thread

while(runner == null)

{

runner = new Thread(this);

//the run method will now be executed throught the start method

runner.start();

}

}

public void run()

{

boolean verifyAdministratorDetails;

String userNameAdminLogin;

String passwordAdminLogin;

while(runner == Thread.currentThread())

{

userNameAdminLogin = txtUserName.getText();

passwordAdminLogin = txtPassword.getText();

verifyAdministratorDetails = true;

out.println(verifyAdministratorDetails);

out.println(userNameAdminLogin);

out.println(passwordAdminLogin);





try

{

String verifcationSuccess = in.readLine();





if(verifcationSuccess.equals("true"))

{

JOptionPane.showMessageDialog(null, "Login Successfull");



}

else

{

JOptionPane.showMessageDialog(null, "Please enter correct user name and password (check spelling)");



}



System.out.println(verifcationSuccess);



}

catch(IOException io)

{

JOptionPane.showMessageDialog(null, io.toString());

}



}



}







Regards Arian


Comments

  1. It looks like your server is started from a Button press or similar, since you have an ActionEvent in the method. Your server doesn't currently start a new Thread to handle requests. The result of this is that your server GUI will probably lock up as soon as you start the server.

    (Java GUIs use a dedicated thread called the Event Dispatching Thread (EDT), often called the AWT Thread or the Swing Thread - if you perform long-running tasks on this thread then the GUI will stop responding).

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex