⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logindialog.java

📁 JAVA
💻 JAVA
字号:
package chatSystem;
import java.awt.event.*;
import java.awt.*;
import java.io.IOException;
import java.net.*;
import java.util.StringTokenizer;
import javax.swing.*;
/**
 * 这个类是用来处理用户登录的,这个类要与服务程序通信
 * 发送信息给服务程序,当服务程序收到并回复相应的处理结果
 * 然后这个类才根据收到信息做相应的处理
 * @author 黄祖光
 */
public class LoginDialog extends JDialog implements ActionListener,Runnable
{
	JPasswordField pw;
	JTextField field;
	JPanel panel,bp;
	JButton ok,cancel,register,getIp;
	DatagramSocket socket=null,nsocket;
	boolean read=false;
	String name="测试";
	String str,friendname;
	InetAddress address;
	String information;
	Clients clients;
    public static String serverIp=	"192.168.1.100";
	
    LoginDialog(JFrame frame,String str,boolean bo,Clients clients)
    {
    	super(frame,str,bo);
    	this.clients=clients;
    	JLabel port=new JLabel("用户名:");
		field=new JTextField(6);
		JLabel password=new JLabel("密码:");
		pw=new JPasswordField(6);
		panel=new JPanel();
		panel.setBackground(Color.pink);
		panel.add(port);
		panel.add(field);
		panel.add(password);
		panel.add(pw);
		bp=new JPanel();
		bp.setBackground(Color.pink);
		ok=new JButton("确定");
		ok.addActionListener(this);
		register=new JButton("注册");
		register.addActionListener(this);
		cancel=new JButton("取消");
		cancel.addActionListener(this);
		/*getIp=new JButton("换IP");
		getIp.addActionListener(this);
		*/
		bp.add(ok);bp.add(register);
		bp.add(cancel);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
		add(panel,BorderLayout.CENTER);
		add(bp,BorderLayout.SOUTH);
		Toolkit tool=Toolkit.getDefaultToolkit();
		Dimension dim=tool.getScreenSize();
		setBounds((dim.width-250)/2,(dim.height-150)/2,250,100);
	    setResizable(false);
		setVisible(true);
    }
	
    /**
     * 监听按钮事件
     */
    public void actionPerformed(ActionEvent ae)
    {
    	if(ae.getSource()==ok)
    	{
    		str=getMessage();
    		new Thread(this).start();
    		ok.setEnabled(false);
    	}
    	else if(ae.getSource()==cancel)
    	{
    		System.exit(0);//当用户选择这个按钮时直接退出整个程序
    		setVisible(false);
    	}
    	else if(ae.getSource()==register)
    	{
    		new RegisterDialog(this,"register",false).setVisible(true);//生成一个处理用户注册的一个对象
    	}
    }
    /**
     * 取得用户的登录所需要的作息
     */
    public String getMessage()
    {
    	String str=field.getText().trim()+","+pw.getText();//
    	return str;
    }
    /**
     * 取得用户的用户名
     */
    public String getName()
    {
    	return name=field.getText().trim();
    }
   
    /**
     * 用来与服务程序通信,处理用户身份验证
     */
    public void run()
    {
    	try
    	{
    		address=InetAddress.getByName(serverIp);//这个是服务器的IP地址
    		socket=new DatagramSocket();
 			while(!str.equals(""))//如果str为空时给服务器发送登录的信息,其中LOGIN是登录的标识字符串,服务程序用来区分不同的操作,
			{ 
				byte[] by=("LOGIN#"+str).getBytes();
                DatagramPacket packet=new DatagramPacket(by,by.length,address,5566);
                socket.send(packet);
                str="";//str用来控制发送的次数,当用户点一回确定按钮时,就发一次信息给服务程序,因为UDP不能保证一次就能发送成功,点一回按钮就给str赋一次值
			}         //然后就执行一回发送
			while(!read)
			{
				byte[] rec=new byte[800];
				DatagramPacket packet=new DatagramPacket(rec,rec.length);
				socket.receive(packet);//接收服务程序发回的确认信息
				String stri=new String(packet.getData(),0,packet.getLength());
				System.out.println(name+": loginDialog received from server "+stri);
				StringTokenizer stk=new StringTokenizer(stri,"*");
				name=stk.nextToken();
				if("OK".equals(name))//如果收到的是OK则说明用户是合法用户
				{
					setVisible(false);
				    information=stk.nextToken();//在线用户的账号和IP
				    System.out.println("login:"+information+" end");
				}
				
				
				else if(stri.equals("WRONG"))//如果收到的是WRONG刚说明用户不是合法用户
				{
					new WarmDialog(this,"用户名或密码错!",true);
					System.exit(0);
				}
				else if(stri.equals("QUIT"))//当收到服务器发回确定退出信息时,关闭socket,并使read=true;
				{
					read=true;
					socket.close();
				}
			}
    	}
    	catch (SocketException e)
		{
			e.printStackTrace();
			new WarmDialog("error in socket","");
		}
		catch(UnknownHostException ue)
		{
			ue.printStackTrace();
			new WarmDialog("Error in InetAddress ","");
		}
		catch(IOException ie)
		{
			ie.printStackTrace();
			new WarmDialog("Exception in send packet","");
		}
    }
    /**
     * 取得用户个人信息
     * @return
     */
    public String getInformation()
    {
    	return information;
    }
    /**
     * 取得朋友的用户名
     * @return
     */
    public String getFriendName()
    {
    	return friendname;
    }
    
    /**
     * 用来给服务器发送退出的信息,如果服务器收到信息,刚发回确定信息,只有当收到确定信息时read才为真
     * @return
     */
    public boolean QuitChat()
    {
    	boolean ok=false;
    	try
    	{
    		byte[] by=("QUIT#"+name).getBytes();
    		DatagramPacket packet=new DatagramPacket(by,by.length,address,5566);
    		socket.send(packet);
    		ok=true;
    	}
    	catch(IOException ie)
    	{
    		ie.printStackTrace();
    	}
    	return ok;
    }
    
    /**
     * 这个方法是用来向服务程序发送要请求单聊的信息
     * @param str
     * @return
     */
    public boolean sendFriendInfo(String str)
    {
    	boolean ok=false;
    	try
    	{
    		byte[] by=("SINGLECHAT#"+str).getBytes();
            DatagramPacket packet=new DatagramPacket(by,by.length,address,5566);
            socket.send(packet);
            ok=true;
    	}
        catch(IOException e)
        {
        	e.printStackTrace();
        }
    	return ok;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -