📄 frmserver.java~22~
字号:
package talk;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.net.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.Color;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class FrmServer extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JTextField txtPort = new JTextField();
JButton btnListen = new JButton();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea txtMessage = new JTextArea();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea txtContent = new JTextArea();
JButton btnSend = new JButton();
JButton btnExit = new JButton();
//客户端套接字
Socket s= null;
//服务器套接字
ServerSocket ss;
//输入流
DataInputStream in;
//输出流
DataOutputStream out;
//接收信息
String s1="";
//客户套接字数组
Socket[] sockets;
//记录上线客户
static int count=0;
//流套接字
DataOutputStream[] stream;
//判断是否连接
static boolean IsListenging=false;
JButton btnEx = new JButton();
public FrmServer() {
try {
sockets=new Socket[100];
stream=new DataOutputStream[100];
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 410));
setTitle("服务端");
this.addWindowListener(new FrmServer_this_windowAdapter(this));
jLabel1.setForeground(Color.blue);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("端口号:");
jLabel1.setBounds(new Rectangle(26, 42, 81, 15));
txtPort.setBounds(new Rectangle(128, 38, 99, 21));
btnListen.setBounds(new Rectangle(262, 36, 83, 25));
btnListen.setForeground(Color.blue);
btnListen.setText("监听");
btnListen.addActionListener(new FrmServer_btnListen_actionAdapter(this));
jScrollPane1.setBounds(new Rectangle(29, 70, 332, 169));
txtMessage.setEnabled(false);
txtMessage.setForeground(Color.blue);
txtMessage.setLineWrap(true);
jScrollPane2.setBounds(new Rectangle(29, 245, 331, 68));
txtContent.setForeground(Color.blue);
txtContent.setLineWrap(true);
btnSend.setBounds(new Rectangle(55, 342, 83, 25));
btnSend.setForeground(Color.blue);
btnSend.setText("发送");
btnSend.addActionListener(new FrmServer_btnSend_actionAdapter(this));
btnExit.setBounds(new Rectangle(156, 342, 83, 25));
btnExit.setForeground(Color.blue);
btnExit.setText("断开");
btnExit.addActionListener(new FrmServer_btnExit_actionAdapter(this));
btnEx.setBounds(new Rectangle(260, 342, 83, 25));
btnEx.setForeground(Color.blue);
btnEx.setText("退出");
btnEx.addActionListener(new FrmServer_btnEx_actionAdapter(this));
contentPane.add(jLabel1);
contentPane.add(txtPort);
contentPane.add(btnListen);
contentPane.add(jScrollPane1);
contentPane.add(jScrollPane2);
contentPane.add(btnSend);
contentPane.add(btnExit);
contentPane.add(btnEx);
jScrollPane2.getViewport().add(txtContent);
jScrollPane1.getViewport().add(txtMessage);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
}
public void btnListen_actionPerformed(ActionEvent e) {
try{
this.IsListenging=true;
int port = Integer.parseInt(txtPort.getText().trim());
ss=new ServerSocket(port);
txtMessage.setText("监听的端口号:"+port+"\n");
new Thread()
{
public void run()
{
try {
while(true)
{
s = ss.accept();
new Thread2(s).start();
sockets[count] = s;
}
} catch (IOException ex) {
}
}
}.start();
}catch(NumberFormatException ne)
{
JOptionPane.showMessageDialog(this,"端口号请输入数字!","错误!",0);
txtPort.setText(null);
txtPort.grabFocus();
}catch(IOException ioe)
{
JOptionPane.showMessageDialog(this,"对不起!无法连接!");
}
}
class Thread2 extends Thread
{
Socket s;
Thread2(Socket s)
{
this.s=s;
}
public void run()
{
try{
in=new DataInputStream(s.getInputStream());
out=new DataOutputStream(s.getOutputStream());
stream[count]=out;
count++;
txtMessage.append("连接成功!对方IP"+s.getRemoteSocketAddress()+"\n");
String time=null;
while((s1=in.readUTF())!=null)
{
time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
txtMessage.append("客户端说: "+time+"\n"+ s1 + "\n");
}
}catch(IOException eee)
{
JOptionPane.showMessageDialog(null,"客户端断开!");
}
}
}
public void btnExit_actionPerformed(ActionEvent e) {
try {
if(this.IsListenging)
{
for(int i=0;i<count;i++)
stream[i].writeUTF("服务器退出!");
in.close(); //关闭Socket输出流
out.close(); //关闭Socket输入流
s.close(); //关闭Socket
ss.close(); //关闭ServerSocket
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,"关闭失败!");
}
txtMessage.append("服务端关闭!");
}
public void btnSend_actionPerformed(ActionEvent e) {
if(txtContent.getText().trim().length()==0)
{
JOptionPane.showMessageDialog(this,"发送信息请不要为空!");
return;
}
try {
// in=new DataInputStream(s.getInputStream());
// out=new DataOutputStream(s.getOutputStream());
for(int i=0;i<count;i++)
stream[i].writeUTF(txtContent.getText());
String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
txtMessage.append("服务器说: "+time+"\n"+txtContent.getText()+"\n");
txtContent.setText(null);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,"发送失败!");
}catch(Exception eeeeee)
{
;
}
}
public void this_windowClosing(WindowEvent e) {
}
public void btnEx_actionPerformed(ActionEvent e) {
try {
for(int i=0;i<count;i++)
stream[i].writeUTF("服务器退出!");
} catch (IOException ex) {
}
dispose();
}
}
class FrmServer_btnEx_actionAdapter implements ActionListener {
private FrmServer adaptee;
FrmServer_btnEx_actionAdapter(FrmServer adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnEx_actionPerformed(e);
}
}
class FrmServer_this_windowAdapter extends WindowAdapter {
private FrmServer adaptee;
FrmServer_this_windowAdapter(FrmServer adaptee) {
this.adaptee = adaptee;
}
public void windowClosing(WindowEvent e) {
adaptee.this_windowClosing(e);
}
}
class FrmServer_btnSend_actionAdapter implements ActionListener {
private FrmServer adaptee;
FrmServer_btnSend_actionAdapter(FrmServer adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnSend_actionPerformed(e);
}
}
class FrmServer_btnExit_actionAdapter implements ActionListener {
private FrmServer adaptee;
FrmServer_btnExit_actionAdapter(FrmServer adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnExit_actionPerformed(e);
}
}
class FrmServer_btnListen_actionAdapter implements ActionListener {
private FrmServer adaptee;
FrmServer_btnListen_actionAdapter(FrmServer adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnListen_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -