package com.lee.socket;
import com.lee.jdbc.Record;
import com.lee.jdbc.RecordDao;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TalkServer extends JFrame {
private JPanel contentPane;
private JTextField ipText;
private JTextField portText;
final JButton startBut;
final JTextArea textArea;
private JTextField msgText;
BufferedWriter bw = null;
/**
* Create the frame.
*/
public TalkServer() {
setTitle("聊天服务器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 650, 441);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblip = new JLabel("本机IP");
lblip.setFont(new Font("宋体", Font.PLAIN, 14));
ipText = new JTextField();
ipText.setColumns(10);
ipText.setText("127.0.0.1");
JLabel label = new JLabel("监听端口");
label.setFont(new Font("宋体", Font.PLAIN, 14));
portText = new JTextField();
portText.setColumns(10);
portText.setText("9999");
startBut= new JButton("开启服务器");
JLabel label_2 = new JLabel("输入消息");
msgText = new JTextField();
msgText.setColumns(10);
JButton send = new JButton("发送");
JScrollPane scrollPane = new JScrollPane();
GroupLayout gl_contentPane = new GroupLayout(contentPane);
//自动空隙
//gl_contentPane.setAutoCreateGaps(true);
//gl_contentPane.setAutoCreateContainerGaps(true);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblip, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(ipText, GroupLayout.PREFERRED_SIZE, 138, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
//.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(portText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(46)
.addComponent(startBut)
.addContainerGap(23, Short.MAX_VALUE))
//新增
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label_2,GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
.addGap(20)
.addComponent(msgText, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE)
.addGap(20)
.addComponent(send, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
.addGap(20))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(scrollPane)
.addGap(23))))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(28)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblip, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addComponent(ipText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addComponent(portText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(startBut))
.addGap(18)
//新增发送消息框
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addComponent(msgText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(send))
.addGap(22)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
.addContainerGap())
);
textArea = new JTextArea();
textArea.setFont(new Font("Monospaced", Font.PLAIN, 20));
scrollPane.setViewportView(textArea);
contentPane.setLayout(gl_contentPane);
startBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ServerOpen serverOpen = new ServerOpen(Integer.parseInt(portText.getText()),textArea);
new Thread(serverOpen).start();
while(true) {
if(serverOpen.getSocket() != null) {
try{
bw = new BufferedWriter(new OutputStreamWriter(serverOpen.getSocket().getOutputStream()));
}catch (IOException ex) {
System.out.println("没能得到BufferedWriter");
}
break;
}
}
}
});
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg = msgText.getText();
if(msg != "") {
try{
send(msg);
msgText.setText("");
msgText.requestFocus();
textArea.append("servei " + new Date() + "\n" + msg+ "\n");
}catch (IOException ex) {
System.out.println("服务器端发送消息失败");
textArea.append("客户端发送信息错误\n");
}
}
}
});
}
//发送信息
void send(String msg) throws IOException{
bw.write(msg);
bw.newLine();
bw.flush();
//server发送的信息
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = format.format(new Date());
Record record = new Record(1, dateStr, msg, 2);
RecordDao.insertSendRecord(record);
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TalkServer frame = new TalkServer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
})
没有合适的资源?快使用搜索试试~ 我知道了~
communication-software-server_java_tcp_源码
共29个文件
class:11个
java:8个
xml:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 8 浏览量
2021-10-02
02:21:22
上传
评论
收藏 30KB ZIP 举报
温馨提示
实现聊天软件服务器端,使用TCP协议,java socker来实现发送消息的功能。
资源推荐
资源详情
资源评论
收起资源包目录
communication-software-server.zip (29个子文件)
src
com
lee
socket
TestServer.java 175B
ServerSend.java 4KB
TalkServer.java 8KB
Close.java 1KB
ServerOpen.java 899B
jdbc
DBUtils.java 929B
RecordDao.java 3KB
Record.java 1KB
communications-oftware-server.iml 721B
out
production
communications-oftware-server
META-INF
communications-oftware-server.kotlin_module 16B
com
lee
socket
TalkServer$2.class 2KB
Close.class 1KB
TalkServer$1.class 2KB
TalkServer$3.class 760B
ServerSend.class 5KB
ServerOpen.class 2KB
TestServer.class 535B
TalkServer.class 6KB
jdbc
Record.class 1KB
DBUtils.class 1KB
RecordDao.class 2KB
.idea
project-template.xml 91B
description.html 97B
uiDesigner.xml 9KB
workspace.xml 9KB
misc.xml 396B
modules.xml 305B
.gitignore 184B
encodings.xml 164B
共 29 条
- 1
资源评论
心若悬河
- 粉丝: 51
- 资源: 3956
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功