📄 server.java
字号:
package TCPchat;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Title: Server类<br>
* Description: 基于TCP的聊天程序的服务器端程序,用于泸州职业技术学院电信系Java课程教学示范。
* Copyright:(c)2007 www.luzhou.net<br>
* Company: 泸州职业技术学院电信系
* @author 华卫
* @version 1.00
*/
public class Server extends JFrame{
static final int PORT = 8136;
JPanel panel1,panel2;
JTextArea txtContent,txtInput,txtInputIP;
JScrollPane panContent,panInput;
JButton btnClose,btnSend;
ServerSocket serverSocket;
Socket socket;
DataInputStream netIn;
DataOutputStream netOut;
public static void main(String[] args) {
new Server();
}
public Server(){
super("服务器");
//创建组件
panel1=new JPanel();
panel2=new JPanel();
txtContent=new JTextArea(15,60);
txtInput=new JTextArea(3,60);
panContent=new JScrollPane(txtContent,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panInput=new JScrollPane(txtInput,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
btnClose=new JButton("关闭");
btnSend=new JButton("发送");
//添加组件
getContentPane().add(panContent,"Center");
getContentPane().add(panel1,"South");
panel1.setLayout(new GridLayout(0,1));
panel1.add(panInput);
panel1.add(panel2);
panel2.add(btnSend);
panel2.add(btnClose);
//设置组件属性
txtContent.setEditable(false);
txtContent.setFont(new Font("宋体",Font.PLAIN,13));
txtInput.setFont(new Font("宋体",Font.PLAIN,15));
txtContent.setLineWrap(true);
txtInput.setLineWrap(true);
txtInput.requestFocus();
setSize(450,350);
setLocation(50,200);
setResizable(false);
setVisible(true);
//等候客户请求
try {
txtContent.append("服务器已启动...\n");
serverSocket = new ServerSocket(PORT);
txtContent.append("等待客户请求...\n");
socket = serverSocket.accept();
txtContent.append("连接一个客户。\n"+socket+"\n");
netIn = new DataInputStream(socket.getInputStream());
netOut = new DataOutputStream(socket.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
/////////////////////////
//注册监听器,编写事件代码
txtContent.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
displayClientMsg();
}
});
txtInput.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
displayClientMsg();
}
});
panel2.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseMoved(MouseEvent e) {
displayClientMsg();
}
});
txtInput.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
displayClientMsg();
}
});
txtInput.addFocusListener(new FocusAdapter(){
public void focusGained(FocusEvent e) {
displayClientMsg();
}
});
btnSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
String serverMsg = txtInput.getText();
if(!serverMsg.trim().equals("")){
txtContent.append("服务器>"+serverMsg+"\n");
netOut.writeUTF(serverMsg);
}else{
JOptionPane.showMessageDialog(null,"不能发送空信息!","服务器",JOptionPane.WARNING_MESSAGE);
}
txtInput.setText("");
txtInput.requestFocus();
} catch (IOException ie) {
ie.printStackTrace();
}
}
});
btnClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try {
netIn.close();
netOut.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
try {
netIn.close();
netOut.close();
socket.close();
serverSocket.close();
} catch (IOException ie) {
ie.printStackTrace();
}
System.exit(0);
}
public void windowActivated(WindowEvent e){
txtInput.requestFocus();
}
});
}
//显示客户端信息
void displayClientMsg(){
try {
if(netIn.available()>0){
String clientMsg = netIn.readUTF();
txtContent.append("客户端>"+clientMsg+"\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -