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

📄 client.java

📁 一个基于java rmi和applet的messenger,和普通聊天室略有不同
💻 JAVA
字号:
/*

This source code is for a simple java based chat program. This file is one of
the two files needed for the chat program. One being the "server.java" and
the other "client.java".This code is free for all.No guarantees of any sort!
This code is developed by Syed Ahmed Junaidulla...enjoy!

steps:
------
1->javac server.java
2->javac client.java
3->java server
4->java client(on another dos prompt)

---------------
You should get the output "Hi! ASL plz?" on the client application,
as soon as you run it!

---------------
For further help & assistance in understanding the code you can always
contact me!

---------------
Thankx for taking time to look at my code though you don't have enough time
appreciate it!

You can always visit me @ http://mypal.20m.com
*/

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class client extends Frame implements ActionListener, Runnable
{
        Image Icon = Toolkit.getDefaultToolkit().getImage("hi.gif") ;
	Socket s;
	BufferedReader br;
	BufferedWriter bw;
	TextField text;
        Button sendBut, exitBut;
	List list;

        public client(String st)
	{
                super(st);
                setSize(300, 130);
		setIconImage(Icon);
		setLocation(300,0);
                setResizable(false);
                setBackground(new Color(192, 192, 192));
		this.setLayout(new GridLayout(2, 1));

                Panel panels[] = new Panel[2];
                panels[0] = new Panel();
                panels[1] = new Panel();
                panels[0].setLayout(new BorderLayout());
                panels[1].setLayout(new FlowLayout(FlowLayout.LEFT));

                sendBut = new Button("Send");
                exitBut = new Button("Exit");

                sendBut.addActionListener(this);
                exitBut.addActionListener(this);

                list = new List();
		text = new TextField(25);

                panels[0].add(list);
                panels[1].add(text);
                panels[1].add(sendBut);
                panels[1].add(exitBut);     


                add(panels[0]);
                add(panels[1]);

		setVisible(true);

                try
                {
                        /* Assuming that this application is run on single
                        machine I've used the default ip i.e., 127.0.0.1. If
                        you want to use it on 2 different machines use the
                        ip that is assigned to the machine on which server
                        applicatin is residing*/

                        s = new Socket("127.0.0.1", 1053);
                        br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
			Thread th;
			th = new Thread(this);
			th.start();
			
		}catch(Exception e){}
		
	}

        public static void main(String arg[])
	{
                // create an object instance of the class
                // by sending the title as parameter
                new client("Junaid's Chat Client Application");
		
	}

        public void run()
	{
                while (true)
		{
			try
                        {
                                list.addItem(br.readLine());
			}catch (Exception h){}
		}
	}
	

        public void actionPerformed(ActionEvent ae)
	{
                 if(ae.getSource().equals(exitBut))
			 System.exit(0);
		 else
                 {
                        try
                        {
                                bw.write(text.getText());
                                bw.newLine();
                                bw.flush();
                                text.setText("");
                        }catch(Exception m){}
		 }
				  
	}
	
}

⌨️ 快捷键说明

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