58,446
社区成员
发帖
与我相关
我的任务
分享
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JTextArea;
import javax.swing.JEditorPane;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.JScrollPane;
public class server_frame
{
public JFrame frame;
public DefaultListModel listModel1;
boolean Connection = true;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
server_frame window = new server_frame();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public server_frame()
{
initialize();
}
public void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
panel.setLayout(null);
JEditorPane editorArea = new JEditorPane();
editorArea.setBounds(14, 387, 602, 124);
panel.add(editorArea);
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(14, 13, 602, 361);
panel.add(textArea);
JButton sendButton = new JButton("Send");
sendButton.setFont(new Font("Consolas", Font.BOLD, 20));
sendButton.setBounds(508, 516, 108, 27);
panel.add(sendButton);
JTextPane txtpnNowSendingTo = new JTextPane();
txtpnNowSendingTo.setBackground(SystemColor.menu);
txtpnNowSendingTo.setFont(new Font("Consolas", Font.BOLD, 15));
txtpnNowSendingTo.setEditable(false);
txtpnNowSendingTo.setEnabled(false);
txtpnNowSendingTo.setText("Now Sending to");
txtpnNowSendingTo.setBounds(637, 427, 120, 27);
panel.add(txtpnNowSendingTo);
JTextPane ip_address = new JTextPane();
ip_address.setForeground(Color.BLACK);
ip_address.setFont(new Font("Consolas", Font.BOLD, 15));
ip_address.setText("192.255.255.255");
ip_address.setEditable(false);
ip_address.setBounds(636, 454, 125, 27);
panel.add(ip_address);
JTextPane Host_IP = new JTextPane();
Host_IP.setBackground(SystemColor.menu);
Host_IP.setText(" Current IP");
Host_IP.setFont(new Font("Consolas", Font.BOLD, 15));
Host_IP.setEnabled(false);
Host_IP.setEditable(false);
Host_IP.setBounds(648, 287, 120, 27);
panel.add(Host_IP);
JTextPane txHost_IP = new JTextPane();
txHost_IP.setBackground(SystemColor.menu);
txHost_IP.setText("198.255.255.255");
txHost_IP.setFont(new Font("Consolas", Font.BOLD, 15));
txHost_IP.setEnabled(false);
txHost_IP.setEditable(false);
txHost_IP.setBounds(640, 308, 131, 27);
panel.add(txHost_IP);
JTextPane txHost_Port = new JTextPane();
txHost_Port.setBackground(SystemColor.menu);
txHost_Port.setText("Current Port");
txHost_Port.setFont(new Font("Consolas", Font.BOLD, 15));
txHost_Port.setEnabled(false);
txHost_Port.setEditable(false);
txHost_Port.setBounds(648, 327, 120, 27);
panel.add(txHost_Port);
JTextPane Host_Port = new JTextPane();
Host_Port.setText("8080");
Host_Port.setFont(new Font("Consolas", Font.BOLD, 15));
Host_Port.setBounds(671, 347, 54, 27);
panel.add(Host_Port);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(630, 13, 138, 271);
panel.add(scrollPane);
JList connecters_list = new JList();
scrollPane.setRowHeaderView(connecters_list);
listModel1 = new DefaultListModel(); // 设置动态客户端列表
connecters_list.setModel(listModel1); // 设置数据
scrollPane.setViewportView(connecters_list);
JButton Host = new JButton("Open Port");
Host.setFont(new Font("Consolas", Font.BOLD, 15));
Host.setBounds(644, 387, 113, 27);
panel.add(Host);
JButton DisconnectButton = new JButton("Disconnect");
DisconnectButton.setFont(new Font("Consolas", Font.BOLD, 15));
DisconnectButton.setBounds(635, 484, 130, 27);
panel.add(DisconnectButton);
Host.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Integer IpPort = Integer.parseInt(Host_Port.getText());
ServerSocket server = new ServerSocket(IpPort);
// 建立ServerSocket并等待连接请求
while (true)
{
Socket socket = server.accept();// 连接建立
String ip = socket.getLocalAddress().getHostAddress();// 获取IP地址
listModel1.addElement(ip);// 列表添加客户端IP地址
ip_address.setText(ip);// 修改当前对话IP地址
BufferedReader in = new BufferedReader( // 通过Socket获取连接上的输入/输出流。
new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (Connection)
{
textArea.append("客户端IP:" + ip + " [数据长度:" + "n" + "]" + "\r\n" + in.readLine() + "\r\n");
// send 按钮功能
sendButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String text = editorArea.getText();
textArea.append(text + "\r\n");
editorArea.setText("");
out.print(text + "\r\n");
out.flush();
}
});
// 断开连接按钮功能
DisconnectButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
out.close();
in.close();
listModel1.removeElement(ip);// 删除列表数据
Connection = false; // 跳出循环
} catch (IOException err)
{
err.printStackTrace();
}
}
});
}
}
} catch (IOException e1)
{
e1.printStackTrace();
}
}
});
}
}