java小项目(聊天功能)

先说明一下哈;先运行服务器(server),再运行客户端(client)

server服务器:

package ChatTwo;

import java.io.*;
import java.net.*;
 
public class ChatTwoServer {
	
	public ChatTwoServer(int port,String name) throws IOException
	{
		ServerSocket server=new ServerSocket(port);//创建seversocket对象,提供tcp连接服务。指定端口port,等待tcp连接。
		System.out.print("正在等待连接,请勿操作!");
		Socket client=server.accept();//创建socket对象,它等待接收客户端的连接。
		new ChatTwoClient(name,client);//实现图形界面。
		server.close();
	}
 
	public static void main(String[] args) throws IOException {
		new ChatTwoServer(2001,"SQ");
 
	}
 
}

client客户端:

package ChatTwo;

import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
 
public class ChatTwoClient extends JFrame implements ActionListener {
	private String name;
	private JTextArea text_re;
	private JTextField text_se;
	private PrintWriter cout;
	private JButton buttons[];
	public ChatTwoClient(String name,Socket socket) throws IOException
	{
	
		super("我:"+name+InetAddress.getLocalHost().getHostAddress()+":"+socket.getLocalPort());
		this.setBounds(320, 240, 400, 240);
		this.text_re=new JTextArea();
		this.text_re.setEditable(false);
		this.getContentPane().add(new JScrollPane(this.text_re));
		
		JToolBar toolBar=new JToolBar();
		this.getContentPane().add(toolBar,"South");
		toolBar.add(this.text_se=new JTextField(30));
		buttons=new JButton[2];
		buttons[0]=new JButton("发送");
		buttons[1]=new JButton("下线");
		toolBar.add(buttons[0]);
		toolBar.add(buttons[1]);
		buttons[0].addActionListener(this);
		buttons[1].addActionListener(this);//给按钮添加事件监听,委托当前对象处理
		this.setVisible(true);
		this.name=name;
		this.cout=new PrintWriter(socket.getOutputStream(),true);//获得socket输出流
		this.cout.println(name);
		BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream())); //将socket的字节输入流转换为字符流,默认GBK字符集,再创建缓冲字符输入流
                String line="连接"+br.readLine()+"成功";
                while(line!=null&&!line.endsWith("bye"))
		{
			text_re.append(line+"\r\n");
			line=br.readLine();
		}//读取对方发送的内容并显示,直到内容为为空或对方下线
		br.close();
		this.cout.close();
		socket.close();
		buttons[0].setEnabled(false);
		buttons[1].setEnabled(false);
	}
	public ChatTwoClient(String name,String host,int port) throws IOException
	{
		this(name,new Socket(host,port));//调用另一个构造方法
	}
	public void actionPerformed(ActionEvent ev)
	{
		if(ev.getActionCommand().equals("发送"))
		{
			this.cout.println(name+":"+text_se.getText());
			text_re.append("我:"+text_se.getText()+"\n");
			text_se.setText("");
		}//按下发送按钮后,将内容发出,并更新自己聊天框的内容
		if(ev.getActionCommand().equals("下线"))
		{
			text_re.append("你已下线\n");
			this.cout.println(name+"离线\n"+"bye\n");
			buttons[0].setEnabled(false);
			buttons[1].setEnabled(false);
		}//下线按钮按下后,发送bye作为下线标记
	}
	
 
	public static void main(String[] args) throws IOException {
		new ChatTwoClient("mxl","127.0.0.1",2001); //ip地址和端口
 
	}
 
}

运行结果图:

 

运行server出现

 即可点击运行client

该项目是我们课堂上老师讲的练习

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值