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 7887751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:39:45+00:00 2026-06-03T05:39:45+00:00

This is in my client: public static void logIn(String name, String pass) { try

  • 0

This is in my client:

public static void logIn(String name, String pass) {
try {
    Socket s = new Socket("localhost", 9000);
    PrintStream out = new PrintStream(s.getOutputStream());
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    out.print(name + "," + pass);
    out.flush();
    out.close();

    System.out.println(in.readLine());

    in.close();
    s.close();
}
catch(UnknownHostException exp)
{
    exp.printStackTrace();

}
catch(java.io.IOException exp)
{
    exp.printStackTrace();
}   
}

and this I have in my server:

 public static void main(String[] args){
 boolean clientExists=false;
 ArrayList<User> users = new ArrayList<User>();
 users.add(new User("jmeno","heslo"));
 ServerSocket ss;
 try {
    ss = new ServerSocket(9000);
    while(true) {
                clientExists=false;
        Socket s = ss.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintStream out = new PrintStream(s.getOutputStream());
        String xmlpacket="";
                String pom;

                while ((pom = in.readLine())!=null){
                     xmlpacket+=pom;
                     xmlpacket+="\n";
                }

                for(User us: users)
                    {
                            if(us.isUserNameAndPasswordRight(login, passwd))
                            {
                                    out.print("user is connected");
                                    out.flush();
                                    clientExists=true;
                            }
                    }
                }
                if(clientExist != true)
              out.print("bad login");
        out.flush();
        out.close();
        in.close();
        s.close();
}
catch(java.io.IOException exp)
{
    System.out.println("chyba u socketu!");
}

}

Is it posible to working like that? I can´t get this working because exception on client side when try reading from server the answer.

Edit:
This is stacktrace:

java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
    at sun.nio.cs.StreamDecoder.read(Unknown Source)
    at java.io.InputStreamReader.read(Unknown Source)
    at java.io.BufferedReader.fill(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at java.io.BufferedReader.readLine(Unknown Source)
    at client.client.logIn(client.java:79)


    at client.GUI.GUI.jMenuItem1ActionPerformed(GUI.java:379)
    at client.GUI.GUI.access$5(GUI.java:367)
    at client.GUI.GUI$5.actionPerformed(GUI.java:151)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
  • 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-03T05:39:46+00:00Added an answer on June 3, 2026 at 5:39 am

    I did several things, so give all the code.

    • Placed the buffered reader later, to possibly prevent blocking.
    • Added encoding, so it does not use default OS encoding (client on different OS).
    • Did println i.o. print.
    • Autoflushing.
    • Inportant: no PrintStream but PrintWriter.
    • No loop on xmlpacket; that was code for later.

      public static void logIn(String name, String pass) {
      try {
          Socket s = new Socket("localhost", 9000);
          //PrintStream out = new PrintStream(s.getOutputStream(), true, "UTF-8");
          PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"), true);
          out.println(name + "," + pass);
          out.flush();
      
          BufferedReader in = new BufferedReader(new InputStreamReader(
                  s.getInputStream(), "UTF-8"));
      
          System.out.println(in.readLine());
      
          out.close();
          in.close();
          s.close();
      } catch (UnknownHostException exp) {
          exp.printStackTrace();
      
      } catch (java.io.IOException exp) {
          exp.printStackTrace();
      }
      }
      
      
      public static void main(String[] args) {
      ArrayList<User> users = new ArrayList<User>();
      users.add(new User("jmeno", "heslo"));
      ServerSocket ss;
      try {
          ss = new ServerSocket(9000);
          while (true) {
              Socket s = ss.accept();
              System.out.println("Accept...");
              BufferedReader in = new BufferedReader(new InputStreamReader(
                      s.getInputStream(), "UTF-8"));
              PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"), true);
              String xmlpacket = "";
              String pom;
      
              pom = in.readLine();
              //while ((pom = in.readLine()) != null) {
                  xmlpacket += pom;
                  xmlpacket += "\n";
              //}
              int commaPos = xmlpacket.indexOf(',');
              int newlinePos = xmlpacket.indexOf('\n');
              String login = xmlpacket.substring(0, commaPos);
              String passwd = xmlpacket.substring(commaPos + 1, newlinePos);                        
      
              boolean clientExists = false;
              for (User us : users) {
                  if (us.isUserNameAndPasswordRight(login, passwd)) {
                      out.println("user is connected");
                      clientExists = true;
                      break;
                  }
              }
              if (!clientExists)
                  out.println("bad login");
              out.close();
              in.close();
              s.close();
          }
      } catch (java.io.IOException exp) {
          System.out.println("chyba u socketu!");
      }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class FtpDownloadDemo { public static void Connection(String filename) { FTPClient client = new
Here is the code I am using. Client: public static void main(String[] args) throws
Here we have the Connect(); function: public static void Connect(string username, string password, string
Take a look at this: WebClient client = new WebClient(); WebRequestSettings wrs = new
svn: This client is too old to work with working copy 'D:\CemT\PublishFolder' lease get
i have this client/server application, and the client application sometimes completely freezes when i
I'm sick of getting This client is too old to work with working copy
My Situation: The data request chain of my application looks like this: (Client) ->
I am building a EC system for a client, this client is selling second
Hey all, I'm about to rip my hair out. I have this client that

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.