我写了一个用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();//读取数据
谢谢大家
这是登录界面的,单击登录按钮事件
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();//读取数据
谢谢大家
解决方案 »
- Avaya Aura Communication Manager 的 License
- 两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
- String 字符串处理
- 修改屏幕分辨率的问题
- 这句话什么意思
- java中如何分次读取大文件
- 为什么向数据库里添加datetime类型数据会报错啊?
- 请问怎样才能将一个Date对象的小时、分钟、秒去掉,得到一个只有日期的Date对象呢?顶者有分啊。
- package问题
- 常量定义,enum,还是static final
- 帮忙编写一个小JAVA程序,我是初学者,不太会!
- 有个关于validator验证的问题不明白,请解释!!分不够就加!!
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();
}
}我很想知道这个循环是怎么退出的?
加了也一样啊
有没有谁愿意帮我调试下阿,谢谢了
服务端程序readLine只有读到换行符才返回.
所以造成服务端程序一直读取IO不返回.直到超时,抛出异常