Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: TCP JAva Server - Username, Password

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TCP JAva Server - Username, Password

    Hi, i would like to add 2 methods. Username and password. Client enter the username into the command line and server saves it. Then client enter the password. The password should be sum of ASCII values from the username. Finally the server should make a control and if the password equals the ASCII sum, it should by everything OK and server can do some other stuff.

    Do you have any idea how i can add these two methods ? Thank you !

    public class Server extends Thread {
     
        StringBuilder sb = new StringBuilder();
        int indicator = 0;
        Socket socket;
        String name = "";
        String password = "";
     
        public static void main(String[] args) {
            try {
                ServerSocket ss = new ServerSocket(3618);
                while (true) {
                    Socket s = ss.accept();
                    Server srv = new Server();
                    srv.socket = s;
                    srv.start();
                }
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        @Override
        public void run() {
            try {
                String str = "";
                int chars;
                int ch = 0;
                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     
                while ((chars = in.read()) != -1) {
                    sb.append((char) chars);
                    str = sb.toString();
                }
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
     
        void sendMessage(String message, PrintWriter pw) {
            pw.write(message);
            pw.flush();
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TCP JAva Server - Username, Password

    Does the client code execute in the command prompt without GUI?
    To get data from a user, print a prompting message and use one of the Scanner class's method to read in the user's response.

    The server-client interactions about the username and password is a lot more involved than the small piece of code that has been posted.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TCP JAva Server - Username, Password

    I just start the server and connect to it using windows command line: telnet localhost <portnumber>. I have no GUI. I just ned to know, how to get username and password from command line and save it to variables.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TCP JAva Server - Username, Password

    how to get username and password from command line
    See the Scanner class for methods to read input data from a user.

    If you are asking about what is passed to the main() method on the commandline when the class is executed, the contents of the command line are passed to main() in a String array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TCP JAva Server - Username, Password

    Quote Originally Posted by Norm View Post
    See the Scanner class for methods to read input data from a user.

    If you are asking about what is passed to the main() method on the commandline when the class is executed, the contents of the command line are passed to main() in a String array.
    I think Scanner(System.in) doesnt solve my problem. I am connected via command line in windows and i am sending commands.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TCP JAva Server - Username, Password

    If the program is executing in a command prompt window, System.in should be where the user's input is read.

    Can you explain how you are executing the program? What are the steps you take to execute it?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. TCP Java Socket Server
    By vviston in forum Java Networking
    Replies: 3
    Last Post: March 24th, 2014, 05:36 AM
  2. Username/Password Different Classes Java program help
    By JAVAHELPP in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 18th, 2013, 04:57 PM
  3. TCP/IP java client, c++ server
    By akboyd88 in forum Java Networking
    Replies: 0
    Last Post: March 24th, 2011, 10:46 AM
  4. Database settings (username and password)
    By palooza in forum JDBC & Databases
    Replies: 3
    Last Post: February 24th, 2011, 01:53 AM
  5. Connecting to a HTTPS URL using username and password
    By explore in forum Java Networking
    Replies: 1
    Last Post: October 18th, 2010, 04:57 PM