| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 647 人关注过本帖
标题:咋就不对劲呢? 咋就不对劲呢?
只看楼主 加入收藏
落落
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2005-9-23
收藏
 问题点数:0 回复次数:1 
咋就不对劲呢? 咋就不对劲呢?
这两个程序的功能是:客户机与服务器相连后,在客户机上输入用户名和密码并按“登录”按钮,服务器收到用户名及密码并返回“恭喜你!你已登录成功。”但我运行时竟出不来效果,问题出在哪儿呢?请各位给我个答案吧!谢谢!谢谢!!
客户端:
import javax.swing.*;
import *;
import *;
import java.awt.event.*;
import java.awt.*;
public class Client extends JApplet
{  JLabel       usename;
   JLabel       password;
   JTextField   user;
   JPasswordField    pass;
   JButton      login;
   JPanel      p;
   GridBagLayout layout;
   GridBagConstraints con;
   public void  init()
   {  usename = new JLabel("用户名:");
      password =new JLabel("密码:");
      user =new JTextField(10);
      pass =new JPasswordField(10);
      login=new JButton("登录");
      layout=new GridBagLayout();
      con =new GridBagConstraints();
      p=(JPanel)getContentPane();
      p.add(usename);
      p.add(user);
      p.add(password);
      p.add(pass);
      p.add(login);
      p.setLayout(layout);
      con.anchor=GridBagConstraints.WEST;
      con.gridx=1;
      con.gridy=1;
      layout.setConstraints(usename,con);
      con.gridy=2;
      layout.setConstraints(user,con);
      con.gridy=3;
      layout.setConstraints(password,con);   
      con.gridy=4;
      layout.setConstraints(pass,con);
      con.anchor=GridBagConstraints.CENTER;
      con.gridx=1;
      con.gridy=5;
      layout.setConstraints(login,con);   
   }
}
class panel extends Client   
{  public void init()
   {  
      loginAction log=new loginAction();
      login.addActionListener(log);
      
   }
 
  class loginAction implements ActionListener
  { public void actionPerformed(ActionEvent e)
    { String message;
      Socket client;
      if(e.getSource()==login)
      {
        try
        {message=user.getText()+":"+pass.getPassword();
         client=new Socket(InetAddress.getLocalHost(),8080);
         //getAppletContext().showStatus("客户开始!");
         DataOutputStream toServer = new DataOutputStream(client.getOutputStream());
         DataInputStream fromServer = new DataInputStream(client.getInputStream());
         toServer.writeUTF(message);
         getAppletContext().showStatus((String)fromServer.readUTF());
         user.setText(null) ;
         pass.setText(null);
         toServer.close();
         fromServer.close();
       }
       catch(IOException et)
       {getAppletContext().showStatus("客户不能发送到服务器!"+":"+et);}
      }
    }
  }
}

服务器端:

import java.util.*;
import *;
import *;
public class createServer
{  ServerSocket linkSocket=null;
   Socket netSocket=null;
   public createServer()
   {  while(true)
     {   try
          {  linkSocket=new ServerSocket(8080);
             netSocket=linkSocket.accept();
             doServer serverThread=new doServer(netSocket);
             serverThread.start();
          }
       catch(IOException e1)
       { System.out.println( e1.getMessage());
       }
     }
  }
  public static void main(String args[])   
   {new createServer();}
}
class doServer extends Thread
 {    Socket netSocket;
      DataInputStream  fromClient;
      DataOutputStream   toClient;
    doServer(Socket netSocket)
    {  
       try
       {  
         DataInputStream fromClient= new DataInputStream(netSocket.getInputStream());
         DataOutputStream toClient = new DataOutputStream(netSocket.getOutputStream());
       }  
      catch(IOException e2)
       {System.out.println(e2.getMessage());}
    }
   public void run()
   { String message=null;
      
     try
       { while(true)
          { message=fromClient.readUTF();}
       }
     catch(IOException e3)
       {System.out.println(e3.getMessage());}
     
     try{
        toClient.writeUTF("恭喜你!你已登录成功。");}
     catch(IOException e4)
        {System.out.println(e4.getMessage());}
     System.out.println(message);
   
     try{
         netSocket.close();
         fromClient.close();
         toClient.close();
        }
     catch(IOException e5)
        {System.out.println(e5.getMessage());}
    }                 
 }
搜索更多相关主题的帖子: import 服务器 java 不对劲 awt 
2005-09-29 17:10
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
import javax.swing.*;
import *;
import *;
import java.awt.event.*;
import java.awt.*;

public class Client extends JApplet
{  
  JLabel       usename;
  JLabel       password;
  JTextField   user;
  JPasswordField    pass;
  JButton      login;
  JPanel      p;
  GridBagLayout layout;
  GridBagConstraints con;
  public void  init()
  {  
    usename = new JLabel("用户名:");
    password =new JLabel("密码:");
    user =new JTextField(10);
    pass =new JPasswordField(10);
    login=new JButton("登录");
    layout=new GridBagLayout();
    con =new GridBagConstraints();
    p=(JPanel)getContentPane();
    p.add(usename);
    p.add(user);
    p.add(password);
    p.add(pass);
    p.add(login);
    p.setLayout(layout);
    con.anchor=GridBagConstraints.WEST;
    con.gridx=1;
    con.gridy=1;
    layout.setConstraints(usename,con);
    con.gridy=2;
    layout.setConstraints(user,con);
    con.gridy=3;
    layout.setConstraints(password,con);   
    con.gridy=4;
    layout.setConstraints(pass,con);
    con.anchor=GridBagConstraints.CENTER;
    con.gridx=1;
    con.gridy=5;
    layout.setConstraints(login,con);
    loginAction log=new loginAction();
    login.addActionListener(log);   
  }
  class loginAction implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      String message;
      Socket client;
      if(e.getSource()==login)
      {
        try
        {
          message=user.getText()+":"+pass.getPassword();
          client=new Socket(InetAddress.getLocalHost(),8080);
          //getAppletContext().showStatus("客户开始!");
          DataOutputStreamtoServer = new DataOutputStream(client.getOutputStream());
          DataInputStreamfromServer = new DataInputStream(client.getInputStream());
          toServer.writeUTF(message);
          getAppletContext().showStatus((String)fromServer.readUTF());
          user.setText(null) ;
          pass.setText(null);
          toServer.close();
          fromServer.close();
        }
        catch(IOException et)
        {
          getAppletContext().showStatus("客户不能发送到服务器!"+":"+et);
        }
      }
    }
  }
}


// 显示一个Exception, 至少也有点东西显示啊。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-30 00:06
快速回复:咋就不对劲呢? 咋就不对劲呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022706 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved