我是想登入时通过服务器的验证来完成账号密码的验证。 final String SERVER_IP="222.198.***.**";
final int PORT=4554;
按钮单击处理事件:
public void actionPerformed(ActionEvent e){
//取得按钮的动作字符串
String actionCommand = e.getActionCommand().trim();
//取得用户名
String name = nameTextField.getText();
//取得密码
String password = new String(passwordField.getPassword());
if(actionCommand.equals("login")){
try
{
LoginMsg lm=new LoginMsg();
lm.password=password;
lm.userName=name;
Socket s=new Socket(SERVER_IP,PORT);
if(s==null)
{
System.out.println("can't connect");
throw new Exception("Host("+SERVER_IP+") or PORT ("+PORT+")not found");
}
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(lm);
DataInputStream ois=new DataInputStream(s.getInputStream());
String status=ois.readUTF();//1.这里是不是这样的
if(status.equals("NoSuchUser")||status.equals("PasswordWrong"))
{
//输出对话框
JOptionPane.showMessageDialog(null, "用户名或密码有误");
}
else if(status.equals("LoginSuccess"))
{
//跳转到主界面
}
}catch(Exception m)
{
m.printStackTrace();
}
}
if(actionCommand.equals("exit")){
//清空内存
System.exit(0);
}
if(actionCommand.equals("register"))
{
//实现页面的跳转
LoginFrame LF=new LoginFrame();
}
}问一下,这个没有显示can't connect,也没提示其他错误,但就是有错,补充下,这IP,和PORT不是本机的。
大家帮忙看看,这有什么错误。
还有就是跳转时怎么写?LoginFrame LF=new LoginFrame();再调用LF.SHOW()//有问题,这个方法过时了吗?还是我写错了!
final int PORT=4554;
按钮单击处理事件:
public void actionPerformed(ActionEvent e){
//取得按钮的动作字符串
String actionCommand = e.getActionCommand().trim();
//取得用户名
String name = nameTextField.getText();
//取得密码
String password = new String(passwordField.getPassword());
if(actionCommand.equals("login")){
try
{
LoginMsg lm=new LoginMsg();
lm.password=password;
lm.userName=name;
Socket s=new Socket(SERVER_IP,PORT);
if(s==null)
{
System.out.println("can't connect");
throw new Exception("Host("+SERVER_IP+") or PORT ("+PORT+")not found");
}
ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
oos.writeObject(lm);
DataInputStream ois=new DataInputStream(s.getInputStream());
String status=ois.readUTF();//1.这里是不是这样的
if(status.equals("NoSuchUser")||status.equals("PasswordWrong"))
{
//输出对话框
JOptionPane.showMessageDialog(null, "用户名或密码有误");
}
else if(status.equals("LoginSuccess"))
{
//跳转到主界面
}
}catch(Exception m)
{
m.printStackTrace();
}
}
if(actionCommand.equals("exit")){
//清空内存
System.exit(0);
}
if(actionCommand.equals("register"))
{
//实现页面的跳转
LoginFrame LF=new LoginFrame();
}
}问一下,这个没有显示can't connect,也没提示其他错误,但就是有错,补充下,这IP,和PORT不是本机的。
大家帮忙看看,这有什么错误。
还有就是跳转时怎么写?LoginFrame LF=new LoginFrame();再调用LF.SHOW()//有问题,这个方法过时了吗?还是我写错了!
应该没有问题的你可以把status打印输出不行的话输出测试或断点调试一下
如果这句创建失败的话.他会执行下一句嘛.还是会抛出一个can't connect异常,抛出异常下一句就没用了.被你的try catch 捕获了!
if(s==null)
{
System.out.println("can't connect");
throw new Exception("Host("+SERVER_IP+") or PORT ("+PORT+")not found");
}