Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Java SE 综合讨论区
» 实战错误讨论
打印话题 寄给朋友 订阅主题 |
作者 | startsWith()的问题 |
haiyangzhixin
发贴: 2 积分: 0 |
于 2005-09-22 14:57
客户端 import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; class UserLoginMessage implements Serializable { String name; String password; } public class Login extends JFrame implements ActionListener { JPanel p1; JLabel name,password; JTextField nametext; JPasswordField passwordtext; JButton login,zhuche; GridBagLayout g1; GridBagConstraints c1; Submit s; public Login() { this.setTitle("登陆"); g1=new GridBagLayout(); c1=new GridBagConstraints(); p1=new JPanel(); p1.setLayout(g1); getContentPane().add(p1); name=new JLabel("姓名:"); nametext=new JTextField(10); password=new JLabel("密码:"); passwordtext=new JPasswordField(10); login=new JButton("登 陆"); //login.setBorder(new BevelBorder(BevelBorder.RAISED)); zhuche=new JButton("注 册"); //zhuche.setBorder(new BevelBorder(BevelBorder.RAISED)); c1.gridx=1; c1.gridy=5; g1.setConstraints(name,c1); p1.add(name); c1.gridx=4; c1.gridy=5; g1.setConstraints(nametext,c1); p1.add(nametext); c1.gridx=1; c1.gridy=8; g1.setConstraints(password,c1); p1.add(password); c1.gridx=4; c1.gridy=8; g1.setConstraints(passwordtext,c1); p1.add(passwordtext); c1.gridx=3; c1.gridy=11; g1.setConstraints(login,c1); p1.add(login); c1.gridx=5; c1.gridy=11; g1.setConstraints(zhuche,c1); p1.add(zhuche); zhuche.addActionListener(this); login.addActionListener(this); this.setLocation(200,300); setSize(300,300); setVisible(true); } public static void main(String a[]) { new Login(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==login) { UserLoginMessage message = new UserLoginMessage(); message.name=nametext.getText(); message.password=new String(passwordtext.getPassword()); try { Socket toServer; toServer = new Socket("192.168.123.56",1001); ObjectOutputStream streamToServer =new ObjectOutputStream(toServer.getOutputStream()); streamToServer.writeObject(message); BufferedReader fromServer = new BufferedReader(new InputStreamReader(toServer.getInputStream())); String status = fromServer.readLine(); if(status==null) { JOptionPane.showMessageDialog(null,"fdsfds"); } else { JOptionPane.showMessageDialog(null,status); } streamToServer.close(); fromServer.close(); } catch (Exception evt) { JOptionPane.showMessageDialog(null,evt); } } else if(e.getSource()==zhuche) { s=new Submit(); } } } 服务器端 import java.awt.event.*; import java.io.*; import java.net.*; class UserLoginMessage implements Serializable { String name; String password; } public class ServerLogin extends Thread { ServerSocket serverSocket; public ServerLogin() { try { serverSocket = new ServerSocket(1001); } catch (IOException e) { fail(e,"could not start ..."); } System.out.println("Server started ..."); start(); } public void fail(Exception e, String str) { System.out.println(str + " . " + e); } public void run() { try { while(true) { Socket client = serverSocket.accept(); Connection con = new Connection(client); } } catch (IOException e) { fail(e,"Not listening"); } } public static void main(String args[]) { new ServerLogin(); } } class Connection extends Thread { protected Socket netClient; protected ObjectInputStream fromClient; protected PrintStream toClient; BufferedReader in; String sss; public Connection(Socket client) { netClient = client; try { fromClient = new ObjectInputStream(netClient.getInputStream()); toClient = new PrintStream(netClient.getOutputStream()); } catch (IOException e) { try { netClient.close(); } catch (IOException ee) { System.err.println("Unable to set up stream "+ ee); return; } } start(); } public void run() { UserLoginMessage Message; try { for(;;) { Message = (UserLoginMessage) fromClient.readObject(); if (Message == null) break; String s1=Message.name; try { in=new BufferedReader(new FileReader("d:\\user.txt")); } catch(Exception ee){} try { while((sss=in.readLine())!=null) { if(sss.startsWith(s1)) { toClient.println(); } else { toClient.println("用户名或密码错误,请从新填写!"); } } } catch(Exception ee){} } } catch (IOException e) {} catch (ClassNotFoundException e1) { System.out.println("error in reading object " +e1); } finally { try { netClient.close(); } catch (IOException e) {} } } } 问题: 为什么通过服务器吧质料传过去就不可以用startsWith()方法验证用户存在;而不通过服务器的话就可以
why edited on 2005-09-22 22:53
联想游戏(欢迎跟贴,哈) |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
6664 | startsWith()的问题 | haiyangzhixin | 11045 | 2005-09-22 14:57 |
4833 | Re:startsWith()的问题 | why | 109 | 2005-09-22 22:58 |
5277 | Re:startsWith()的问题 | why | 128 | 2005-09-23 21:17 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |