I’ve been trying to resolve this issue since morning. I have client and server applications, client sends user name and password, server receives it, checks in database, sends a message success to the right user.
But, I’m unable to process this request from client side.
What’s the problem and where’s its lying ?
Awaiting experts solutions…
Here is the Server:
import java.io.*;
import java.net.*;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.File;
import java.sql.*;
class TCPServer
{
public static void main(String argv[]) throws Exception
{
String clientSentence,clientpassword;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(4003);
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "database";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
BufferedReader inFromClient1 =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
clientpassword = inFromClient.readLine();
System.out.println("Received User Name: " + clientSentence);
System.out.println("Received Password: " + clientpassword);
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM table WHERE user='clientSentence' AND password='clientpassword'");
while (res.next()) {
String u = res.getString("user");
String p = res.getString("password");
if (clientSentence.equals(u) && clientpassword.equals(p)){
capitalizedSentence = "Welcome "+clientSentence+" \n";
outToClient.writeBytes(capitalizedSentence);
}else{
capitalizedSentence = "Sorry, not authorized \n";
outToClient.writeBytes(capitalizedSentence);
}
}
}con.close();
}
}
and the Client:
import java.io.*;
import java.net.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.lang.*;
import java.util.Scanner;
class TCPClient
{
public static void main(String argv[]) throws Exception
{
String sentence,sentence1;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
BufferedReader inFromUser1 = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 4003);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
DataOutputStream outToServer1 = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Console console = System.console();
String username = console.readLine("Enter your Username :");
char pswd[] = console.readPassword("Enter your Password :");
String upwd=new String(pswd);
outToServer.writeBytes(username + '\n');
outToServer1.writeBytes(upwd + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
}
clientSocket.close();
}
}
Your code results in a
NullPointerExceptioninTCPClientif you don’t have aConsoleassociated with your JVM.That is definitely not going to work unless you really want to always use the username ‘clientSentence’ and the password ‘clientpassword’.
You should pass the actual value of the corresponding java variables that you set above that line. Also, the right approach for this is to use a
PreparedStatementand pass the user input as arguments, so that the driver will take care of sanitizing the input in order to avoid SQL injection or errors likeImbecilUserException. 🙂