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

  • SEARCH
  • Home
  • 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 8963819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:26:34+00:00 2026-06-15T16:26:34+00:00

ANSWER IS MY OTHER COMMENT I have a problem, I’m sending user and password

  • 0

ANSWER IS MY OTHER COMMENT

I have a problem, I’m sending user and password String via DatagramPacket and DatagramSocket to a remote machine and I want to do a select statement in a database, but the thing is that the received string appears to be what it is supossed to be,here some code:

//build and send method
public void packetCompose(String user, String password) {
    try {
        byte[] userBytes = user.getBytes();
        byte[] passwordBytes = password.getBytes();
        byte[] buf = new byte[256];
        System.arraycopy( userBytes    , 0, buf,   0, Math.min( userBytes.length, 128 ) );
        System.arraycopy( passwordBytes, 0, buf, 128, Math.min( userBytes.length, 128 ) );

        DatagramPacket packet = new DatagramPacket(buf, 256, serverAddress, 4445);
        socket.send(packet);
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

and now the decompose of the packet method

public void packetDecompose(DatagramPacket packet) {
            // packet has this structure
            // 128 bytes            user String
            // 128 bytes            password String
            clientAddress = packet.getAddress();
            String user = new String(packet.getData(),0,128);
            String password = new String(packet.getData(),128,128);
            System.out.println("Packet content: \nuser: "+user+"\npassword: "+password);
            boolean exists = userExists(user, password);
            byte[] buf = new byte[128];
            if(exists) {
                    System.out.println("User exists");
                    System.arraycopy( accessGranted.getBytes(), 0, buf, 0, Math.min(
accessGranted.getBytes().length, 128 ) );
                    send(new DatagramPacket(buf, 128, clientAddress, 4445));
            } else {
                    System.out.println("User does not exist");
                    System.arraycopy( accessDenied.getBytes(), 0, buf, 0, Math.min(
accessDenied.getBytes().length, 128 ) );
                    send(new DatagramPacket(buf, 128, clientAddress, 4445));
            }

    }

    public boolean userExists(String user, String password) {
            boolean exists = false;
            System.out.println("user: "+user.equals("asdf"));
            System.out.println(" pass: "+password.equals("asdf"));
            try {

                    ResultSet result = dataBase.Select("SELECT ipaddress FROM users
WHERE name='"+user+"' AND pass='"+password+"'");
                    while(result.next()) {
                            exists = true;
                    }
            } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
            return exists;
    }

Im introducing asdf as user and password via interface of application so the lines:

System.out.println("user: "+user.equals("asdf"));
System.out.println(" pass: "+password.equals("asdf"));

should print true, but they print false.
Any suggestion on this? Thank you all in advance

  • 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-15T16:26:36+00:00Added an answer on June 15, 2026 at 4:26 pm

    I finally managed to resolve it, thanks to Peter Lawrey for giving some help.

    You have to insert the first byte containing the length of the user String and same with pasword, so when you decompose the packet you can get user and password without null padding.
    Here is the code:

    Compossing:

    public void packetCompose(String user, String password) {
        try {
            byte[] userBytes = user.getBytes();
            byte[] passwordBytes = password.getBytes();
            byte[] buf = new byte[256];
            //first byte contain user length in bytes
            System.arraycopy( new byte[]{(byte)userBytes.length}    , 0, buf, 0, 1 );
            // Then the user
            System.arraycopy( userBytes    , 0, buf,   1, userBytes.length );
            //a byte containing password length in bytes after user
            System.arraycopy( new byte[]{(byte)passwordBytes.length} ,0 , buf,   userBytes.length +1, 1);
            // password
            System.arraycopy( passwordBytes , 0, buf,   userBytes.length+2, passwordBytes.length );
    
            DatagramPacket packet = new DatagramPacket(buf, 256, serverAddress, 4445);
            socket.send(packet);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    

    Decompossing:

    public void packetDecompose(DatagramPacket packet) {
                // packet has this structure
                // 1 byte                       user length in bytes
                // ? bytes                      user String
                // 1 byte                       password length in byte
                // ? bytes              password String
                clientAddress = packet.getAddress();
                byte[] userLength = new byte[]{packet.getData()[0]};
                String user = new String(packet.getData(), 1, (int)userLength[0]);
                byte[] passwordLength = new byte[]{packet.getData()[(int)userLength[0]+1]};
                String password = new String(packet.getData(), (int)userLength[0]+2, (int)passwordLength[0]);
                System.out.println("Packet content: \nuser: "+user+"\npassword: "+password);
                boolean exists = userExists(user, password);
                byte[] buf = new byte[128];
                if(exists) {
                        System.out.println("User exists");
                        System.arraycopy( accessGranted.getBytes(), 0, buf, 0, Math.min(
    accessGranted.getBytes().length, 128 ) );
                        send(new DatagramPacket(buf, 128, clientAddress, 4445));
                } else {
                        System.out.println("User does not exist");
                        System.arraycopy( accessDenied.getBytes(), 0, buf, 0, Math.min(
    accessDenied.getBytes().length, 128 ) );
                        send(new DatagramPacket(buf, 128, clientAddress, 4445));
                }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having read other people's questions I thought window.onload=... would answer my question. I have
My answer: After getting annoyed, I have found a solution. The problem was indeed
Ok this is my problem that no one can seem to answer. I have
(Came up with this question in the course of trying to answer this other
There a bunch of other questions like this, but the only substantial answer I've
I've tried to look for an answer to this, but none of the other
[EDIT] I am NOT accepting any answer which involves BigInteger, or other similarly inefficient
I have gone through some other answers, but cannot get the solution to my
I have seen some of the other answers on this topic but dont really
I have a weird problem with defining a mpi_type_contiguous and using mpi_gatherv later on.

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.