我写了一个用Socket连接到服务器登录的 
这是登录界面的,单击登录按钮事件 
public void jButton1_actionPerformed(ActionEvent e) {
     String name,password;
     name=jTextField1.getText();
     password=new String(jPasswordField1.getPassword(),0,jPasswordField1.getPassword().length);
     Logins yn=new Logins(name,password);
     if(yn.yesno()){
      this.setVisible(false);
      qq.setVisible(true);
     }else{
      jLabel.setText("对不起,登录失败");
     }
    }这是Logins类 客户端,连接服务器用
import java.sql.*;
import java.net.*;
import java.io.*;
public class Logins {
    String name;
    String password;
    public Logins(String name,String password) {
        this.name=name;
        this.password=password;
    }
    public boolean yesno(){
    try{
        Socket s=new Socket(InetAddress.getByName("192.168.24.24"),1215);
        InputStream ips=s.getInputStream();
        OutputStream ops=s.getOutputStream();
      DataOutputStream dos=new DataOutputStream(ops);//输出流
      BufferedReader brNet=new BufferedReader(new InputStreamReader(ips));//输入流,接收
      dos.writeBytes(name+" "+password);
      String status=brNet.readLine();
      if(status.equalsIgnoreCase("yes")){
      dos.close();
      brNet.close();
      s.close();
          return true;
         }else{
         dos.close();
         brNet.close();
         s.close();
      return false;}
        }catch(Exception e2){e2.printStackTrace();return false;}
    }
    }ServerLogin类,服务器端,发回验证值
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.io.*;
import java.net.*;
class CheckLogin implements Runnable{
Connection conn;
ResultSet rs;
    Socket s;
    String name;
    String password;
            public CheckLogin(Socket s) {
            this.s=s;
    }
    public void run(){
        try{
     InputStream ips=s.getInputStream();
     OutputStream ops=s.getOutputStream();
     BufferedReader br=new BufferedReader(new InputStreamReader(ips));//接收数据
     DataOutputStream dos=new DataOutputStream(ops);//发送给客户端
     while(true){
       String strword=br.readLine();//读取数据
      String xinxi[]=strword.split("\\s");
      if(yesno(xinxi[0],xinxi[1])){
      dos.writeBytes("yes".toString());
      }
      else{
      dos.writeBytes("no".toString());
      br.close();
      dos.close();
      }
     }
    }catch(Exception e){e.printStackTrace();}
    }
    public boolean yesno(String name,String password){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://192.168.24.24/chat","root","1042023");
}catch(Exception e)
{System.out.print("加载类出错");return false;}
try{
PreparedStatement pstmt=conn.prepareStatement("select * from infor where yhm=? and pass=?");
pstmt.setString(1,name);
pstmt.setString(2,password);
rs=pstmt.executeQuery();
}catch(Exception e1){return false;
}finally{
  try{
    try{
    if(rs.last()){
    if(conn!=null){conn.close();return true;}
    return true;
    }else{return false;}
    }catch(Exception e3){return false;}
    }catch(Exception e2){return false;}
}
}
}
public class ServerLogin{
    public static void main(String args[]){
    try{
     ServerSocket ss=new ServerSocket(1215);
     while(true){
      Socket s=ss.accept();
      new Thread(new CheckLogin(s)).start();
     }
    }catch(Exception e){e.printStackTrace();}
    }
}点击登录就不动了 
停止,出现 
java.net.SocketException: Connection reset 
提示这句错误 
30行 String strword=br.readLine();//读取数据
谢谢大家

解决方案 »

  1.   

         while(true){
           String strword=br.readLine();//读取数据
          String xinxi[]=strword.split("\\s");
          if(yesno(xinxi[0],xinxi[1])){
          dos.writeBytes("yes".toString());
          }
          else{
          dos.writeBytes("no".toString());
          br.close();
          dos.close();
          }
         }我很想知道这个循环是怎么退出的?
      

  2.   

    输出流的时候你flush()一下看有问题没。
      

  3.   

    dos.flush();
    加了也一样啊
    有没有谁愿意帮我调试下阿,谢谢了
      

  4.   

    客户端程序在发送的数据里面,只填写了用户名+空格+密码,后面没有根换行符"\n"
    服务端程序readLine只有读到换行符才返回.
    所以造成服务端程序一直读取IO不返回.直到超时,抛出异常