1

这是在我的客户中:

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();
}   
}

这是我在我的服务器上的:

 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!");
}

}

可以这样工作吗?我无法使其正常工作,因为尝试从服务器读取答案时客户端出现异常。

编辑:这是堆栈跟踪:

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)
4

3 回答 3

2

试着拿走out.close(),看看会发生什么。我相信关闭OutputStream也会关闭相关的套接字。在您阅读回复之前,您不想这样做。

更新:为了清楚起见,我的意思是带走(或移到最后)out.close()客户端,而不是服务器。

于 2012-05-03T12:43:26.743 回答
2

'out.print(name + "," + pass);' 似乎没有发出行终止符,因此您的客户端中的“readLine()”将不会返回。尝试“println”并摆脱关闭的边缘。

于 2012-05-03T12:55:48.327 回答
2

我做了几件事,所以给出所有的代码。

  • 稍后放置缓冲的阅读器,以防止阻塞。
  • 添加了编码,因此它不使用默认的操作系统编码(不同操作系统上的客户端)。
  • println io 打印了吗?
  • 自动冲洗。
  • 重要提示:没有 PrintStream 而是 PrintWriter。
  • xmlpacket 上没有循环;那是以后的代码。

    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!");
    }
    }
    
于 2012-05-03T13:57:32.213 回答