我写了一个用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();//读取数据
谢谢大家
解决方案 »
- 询问java英语的技术类网站?
- HashMap<Key,Value>如何按照value(Not Key)值的大小排序?
- 请教各位高手~怎么在JTable里显示数据库的查询结果???
- 如何当图片当软件的背景?
- 有没有在北京,参加过或者即将参加中讯集团的笔试的哥们?
- 关于String编码问题(ISO-8859-1)
- Java中如何解决Chart的问题???
- 求JAVA编的小游戏源代码
- 应届的学生想从事JAVA工作他该怎么办?
- 为何我在JDK甚至Jbuilder3.下的程序运行下常,但在jbuilder6下总出错?
- 帮忙编写一个小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不返回.直到超时,抛出异常