package com.m;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatFrmA extends JFrame{
private JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JTextField txtMsg;
private JButton btnSend; //发送按钮
private JLabel lblIP;
private JLabel lblPort;
private JTextField txtIP;
private JTextField txtPort;
private Calendar cal;
public ChatFrmA(){
initGUI();
//创建服务器端
startServer();
}
//开始服务器
public void startServer(){
ServerThread st = new ServerThread();
new Thread(st).start(); //启动线程
}
/**
* 内部类: 服务器端的线程
*/
class ServerThread implements Runnable{
boolean isStarted=false;
public void run(){
try {
ServerSocket server =new ServerSocket(5000);
isStarted=true;
while(isStarted){
//等待客户端连接
Socket sk = server.accept();
//System.out.println("A Client is Connected...");
//获取客户端的输入
BufferedReader br =
new BufferedReader(new InputStreamReader(sk.getInputStream()));
String str = br.readLine();
cal = Calendar.getInstance();
String time = (cal.get(Calendar.HOUR_OF_DAY)>10 ? cal.get(Calendar.HOUR_OF_DAY):"0"+cal.get(Calendar.HOUR_OF_DAY))+":"+
(cal.get(Calendar.MINUTE)>10 ? cal.get(Calendar.MINUTE):"0"+cal.get(Calendar.MINUTE))+":"+
(cal.get(Calendar.SECOND)>10 ? cal.get(Calendar.SECOND):"0"+cal.get(Calendar.SECOND));
String msg = sk.getInetAddress().getHostAddress()+" ["+time+"] 说: "+str;
//关闭
br.close();
sk.close();
//将消息显示在文本框中
jTextArea1.append(msg+"\r\n");
}
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void initGUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("简易聊天工具 - A");
this.setLayout(null);
{
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(5, 5, 300, 300);
{
jTextArea1 = new JTextArea();
jTextArea1.setEditable(false);
jScrollPane1.setViewportView(jTextArea1);
}
this.getContentPane().add(jScrollPane1);
}
{
txtMsg = new JTextField();
txtMsg.setBounds(5, 320, 300, 30);
this.getContentPane().add(txtMsg);
}
{
btnSend = new JButton("发送(S)");
btnSend.setMnemonic('S');
btnSend.setBounds(320, 320, 80, 30);
this.getContentPane().add(btnSend);
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
}
{
lblIP = new JLabel("对方IP:");
lblIP.setBounds(320, 5, 80, 30);
this.getContentPane().add(lblIP);
}
{
txtIP = new JTextField("127.0.0.1");
txtIP.setBounds(320,35,150,30);
this.getContentPane().add(txtIP);
}
{
lblPort = new JLabel("对方端口:");
lblPort.setBounds(320, 150, 80, 30);
this.getContentPane().add(lblPort);
}
{
txtPort = new JTextField("6000");
txtPort.setBounds(320,180,150,30);
this.getContentPane().add(txtPort);
}
this.setResizable(false);
this.setSize(500, 400);
}
public static void main(String[] args) {
ChatFrmA inst = new ChatFrmA();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
//发送
private void btnSendActionPerformed(ActionEvent evt){
//获取对方的IP和端口
String ip=txtIP.getText().trim();
if(ip==null||"".equals(ip)){
JOptionPane.showMessageDialog(this,"请指定对方的IP!");
return;
}
String port = txtPort.getText().trim();
if(port==null || "".equals(port)){
JOptionPane.showMessageDialog(this,"请指定对方的端口!");
return;
}
String msg = txtMsg.getText().trim();
if(msg==null || "".equals(msg)){
JOptionPane.showMessageDialog(this,"不能发送空消息!");
return;
}
try{
//连接对方的服务器
Socket sk=new Socket(ip,Integer.parseInt(port));
//创建客户端的输出流
PrintStream pout = new PrintStream(sk.getOutputStream());
pout.println(msg);//发送消息
//显示消息到当前聊天记录的文本框中
String time=getCurrTimeStr();
this.jTextArea1.append("我说["+time+"]:"+msg+"\r\n");
txtMsg.setText(""); //清空自己发的消息
//关闭
pout.close();
sk.close();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this,"对方主机不存在!","错误",JOptionPane.WARNING_MESSAGE);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取当前时间的字符串
* @return
*/
private String getCurrTimeStr() {
cal = Calendar.getInstance(); //获取当前系统时间
String time = (cal.get(Calendar.HOUR_OF_DAY)<10 ? "0"+cal.get(Calendar.HOUR_OF_DAY) : cal.get(Calendar.HOUR_OF_DAY))+":"+
(cal.get(Calendar.MINUTE)<10 ? "0"+cal.get(Calendar.MINUTE) : cal.get(Calendar.MINUTE))+":"+
(cal.get(Calendar.SECOND)<10 ? "0"+cal.get(Calendar.SECOND) : cal.get(Calendar.SECOND));
return time;
}
}