首页>代码>java swing单人网络聊天室客户端>/简单的聊天室程序/Liaotian/src/cn/dom/ServerChat.java
package cn.dom;


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

public class ServerChat extends JFrame implements ActionListener{
private JButton btnSend;
private JTextArea jtaRecord;
private JTextField jtfMessage;

private ObjectOutputStream out;//输出流对象
private ObjectInputStream in;//输入流对象

private String msg="";
private Socket connect;
private ServerSocket server;
private int count;

public  ServerChat() {
	super("服务器端");
	Container container=getContentPane();;
	setSize(400,320);
	setLocation(100,100);
	setResizable(false);

	JPanel pnlTop=new JPanel();
	pnlTop.setLayout(new FlowLayout(FlowLayout.LEFT));
	container.add(pnlTop,BorderLayout.NORTH);
	JPanel pnlRecord=new JPanel();
	pnlTop.add(new JLabel("聊天内容"));
	pnlTop.add(pnlRecord);
	
	jtaRecord=new JTextArea(12,30);
	jtaRecord.setEditable(false);
	jtaRecord.setLineWrap(true);
	pnlRecord.add(jtaRecord);
	JScrollPane scroll=new JScrollPane(jtaRecord);
	scroll.setSize(336,113);
	pnlRecord.add(scroll);
	
	JPanel pnlBottom=new JPanel();
	pnlBottom.setLayout(new FlowLayout(FlowLayout.LEFT));
	container.add(pnlBottom,BorderLayout.SOUTH);
	pnlBottom.add(new JLabel("消息"));
	jtfMessage=new JTextField(25);
	pnlBottom.add(jtfMessage);
	btnSend=new JButton("发送");
	pnlBottom.add(btnSend);
	btnSend.addActionListener(this);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setVisible(true);
}

//运行服务器
public void runServer()
{
	try {
		server=new ServerSocket(9999);
		while(true)
		{
			try {
				listenConnection();
				getStream();
				communication();
			}catch(EOFException e){
				System.err.println("服务器终止了连接!!!");
			}finally {
				closeConnection();
				count++;
			}
		}
	}catch(IOException e) {
		e.printStackTrace();
	}
}

//发送数据
private void sendData(String msg) {
	try {
		out.writeObject("服务端:"+msg);
		out.flush();
		displayMessage("\n服务端:"+msg);
	}catch(IOException e) {
		jtaRecord.append("\n发生写入错误!!!");
	}
}


//显示消息
private void displayMessage(final String msg) {
	SwingUtilities.invokeLater(new Runnable()
	{
		public void run() {
			jtaRecord.append(msg);
			jtaRecord.setCaretPosition(jtaRecord.getText().length());
		}
	});
}
//读取流信息

private void getStream() throws IOException{
	out=new ObjectOutputStream(connect.getOutputStream());
	out.flush();
	in=new ObjectInputStream(connect.getInputStream());
}

//连接成功进行聊天
private void communication() throws IOException{
	setTextFieldEditable(true);
	do {
		try {
			msg=(String) in.readObject();
			displayMessage("\n"+msg);
		}catch(ClassNotFoundException e){
			displayMessage("\n收到异常对象类型!\n");
		}
	}while(!msg.equals("服务器:stop"));
}

//监听连接
private void listenConnection() throws IOException{
	displayMessage("服务器已启动等待连接.....\n");
	connect=server.accept();
	displayMessage("收到连接"+count+"从"+connect.getInetAddress().getHostName()+"\n");
}


//关闭连接
private void closeConnection()
{
	displayMessage("\n关闭连接!!!\n");
	setTextFieldEditable(false);
	try {
		out.close();
		in.close();
		server.close();
		connect.close();
	}catch(IOException e)
	{
		e.printStackTrace();
	}
}


//设置文本框状态
private void setTextFieldEditable(final boolean editable) {
	SwingUtilities.invokeLater(new Runnable() {
	public void run()
	{
	jtfMessage.setEditable(editable);
	}
	});
	}

@Override
public void actionPerformed(ActionEvent event) {
	sendData(jtfMessage.getText());
	String label=event.getActionCommand();
    switch (label) {
        case "发送":
       	 jtfMessage.setText("");
            break;
    }
}

public static void main(String[] args) {
	new ServerChat().runServer();
}
}
最近下载更多
zeng1206  LV5 2023年12月28日
testuser1234567  LV24 2022年5月31日
g1679456349  LV1 2022年5月24日
liys1234  LV9 2022年4月22日
1265260263  LV4 2022年4月8日
sfdasdgdgaf  LV2 2022年3月19日
1612769136  LV1 2021年12月31日
Demo1111  LV30 2021年12月10日
胡萝北呀  LV3 2021年11月26日
曹思辰  LV6 2021年11月21日
最近浏览更多
taoshen95  LV14 1月19日
zeng1206  LV5 2023年12月28日
lshlsh 2023年12月25日
暂无贡献等级
暂无贡献等级
jkjfdgbkl  LV2 2023年11月1日
fuyouou  LV5 2023年6月29日
2017143155  LV12 2023年6月24日
lclclclccc 2023年6月2日
暂无贡献等级
青柠编程  LV13 2023年4月20日
your namelll 2023年2月20日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友