Chatserver.java
package server;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
*
* @author Administrator
*/
public class chatServer implements ActionListener,Runnable{
JTextArea showArea;
JTextField msgText;
JFrame mainJframe;
JButton sentBtn;
JScrollPane JSPane;
JPanel pane;
Container con;
Thread thread=null;
ServerSocket serversocket;
Socket connectToClient;
DataInputStream inFromClient;
DataOutputStream outToClient;
public chatServer(){
mainJframe=new JFrame("服务器端");
con=mainJframe.getContentPane();
showArea=new JTextArea();
showArea.setEditable(false);
showArea.setLineWrap(true);
JSPane=new JScrollPane(showArea);
msgText=new JTextField();
msgText.setColumns(30);
msgText.addActionListener(this);
sentBtn=new JButton("发送");
sentBtn.addActionListener(this);
pane= new JPanel();
pane.setLayout(new FlowLayout());
pane.add(msgText);
pane.add(sentBtn);
con.add(JSPane,BorderLayout.CENTER);
con.add(pane,BorderLayout.SOUTH);
mainJframe.setSize(500,400);
mainJframe.setVisible(true);
mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
serversocket=new ServerSocket(5500);
showArea.append("正在等待对话请求");
//倾听客户端的连接
connectToClient=serversocket.accept();
//读取来自客户端的内容
inFromClient=new DataInputStream(connectToClient.getInputStream());
//向客户端发送内容
outToClient=new DataOutputStream(connectToClient.getOutputStream());
//启动线程在后台接受来自客户端的消息
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}catch(IOException e){
showArea.append("对不起,不能创建服务器");
msgText.setEditable(false);
sentBtn.setEnabled(false);
}
}
public static void main(String[] args){
new chatServer();
}
public void actionPerformed(ActionEvent e){
String s=msgText.getText();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curdate=new Date();
String str3=formatter.format(curdate);
if(s.length()>0)
{
try{
outToClient.writeUTF(s);
outToClient.flush();
showArea.append(str3+"我说:"+msgText.getText()+"\n");
showArea.setForeground(Color.red);
msgText.setText(null);
}catch(IOException el){
showArea.append("你的消息:"+msgText.getText()+"不能发出去\n");
}
}
}
public void run(){
try{
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curdate=new Date();
String str3=formatter.format(curdate);
while(true){
showArea.append(str3+"对方说:"+ inFromClient.readUTF()+"\n");
showArea.setForeground(Color.blue);
Thread.sleep(1000);
}
}catch(IOException el){}catch(InterruptedException e){}
}
}
chatClient.java
package client;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
/**
*
* @author Administrator
*/
public class chatClient implements ActionListener,Runnable{
JTextArea showArea;
JTextField msgText;
JFrame mainJframe;
JButton sentBtn;
JScrollPane JSPane;
JPanel pane;
Container con;
Thread thread=null;
// ServerSocket serversocket;
Socket connectToServer;
DataInputStream inFromServer;
DataOutputStream outToServer;
public chatClient(){
mainJframe=new JFrame("客户端");
con=mainJframe.getContentPane();
showArea=new JTextArea();
showArea.setEditable(false);
showArea.setLineWrap(true);
JSPane=new JScrollPane(showArea);
msgText=new JTextField();
// msgText.setSize(100,200);
msgText.setColumns(30);
msgText.addActionListener(this);
sentBtn=new JButton("发送");
sentBtn.addActionListener(this);
pane= new JPanel();
pane.setLayout(new FlowLayout());
pane.add(msgText);
pane.add(sentBtn);
con.add(JSPane,BorderLayout.CENTER);
con.add(pane,BorderLayout.SOUTH);
mainJframe.setSize(500,400);
mainJframe.setVisible(true);
mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try{
connectToServer=new Socket("localhost",5500);
//读取来自客户端的内容
inFromServer=new DataInputStream(connectToServer.getInputStream());
//向客户端发送内容
outToServer=new DataOutputStream(connectToServer.getOutputStream());
//启动线程在后台接受来自客户端的消息
showArea.append("连接成功,说点什么吧...\n");
thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}catch(IOException e){
showArea.append("对不起,没有连接到服务器");
msgText.setEditable(false);
sentBtn.setEnabled(false);
}
}
public static void main(String[] args){
new chatClient();
}
public void actionPerformed(ActionEvent e){
String s=msgText.getText();
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curdate=new Date();
String str3=formatter.format(curdate);
// showArea.setBackground(Color.green);
if(s.length()>0)
{
try{
outToServer.writeUTF(s);
outToServer.flush();
showArea.append(str3+"我说:"+msgText.getText()+"\n");
showArea.setForeground(Color.blue);
msgText.setText(null);
}catch(IOException el){
showArea.append("你的消息:"+msgText.getText()+"不能发出去\n");
}
}
}
public void run(){
try{
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date curdate=new Date();
String str3=formatter.format(curdate);
while(true){
showArea.append(str3+"对方说:"+ inFromServer.readUTF()+"\n");
showArea.setForeground(Color.red);
Thread.sleep(1000);
}
}catch(IOException el){}catch(InterruptedException e){}
}
}