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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...