Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8495687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:38:31+00:00 2026-06-10T23:38:31+00:00

I’ve been trying to resolve this issue since morning. I have client and server

  • 0

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();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:38:32+00:00Added an answer on June 10, 2026 at 11:38 pm

    Your code results in a NullPointerException in TCPClient if you don’t have a Console associated with your JVM.


    ResultSet res = st.executeQuery(“SELECT * FROM table WHERE user=’clientSentence’ AND password=’clientpassword'”);

    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 PreparedStatement and 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 like ImbecilUserException. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.