package com.qyl.TCP;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
public class Client extends JFrame{
private TextArea ta;
private TextField tf;
private Button bt;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
public DataInputStream getIn() {
return in;
}
public TextArea getTa() {
return ta;
}
private void initUI() {
ta = new TextArea();
tf = new TextField();
bt = new Button("send");
addListener();
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(tf, BorderLayout.CENTER);
p.add(bt, BorderLayout.EAST);
this.add(p, BorderLayout.SOUTH);
this.add(ta, BorderLayout.CENTER);
this.setTitle("花花");
this.setSize(300, 400);
this.setLocation(300, 400);
this.setVisible(true);
}
private void addListener() {
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(null);
tf.requestFocus();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
ta.append("自 己 "+date+"\r\n\t"+ text+"\r\n");
try {
out.writeUTF(text);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
public void initConn() {
try {
socket = new Socket(InetAddress.getLocalHost(),8888);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public Client() {
initUI();
initConn();
new ClientThread(this).start();
}
public static void main(String[] args) {
new Client();
}
}
package com.qyl.TCP;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
public class Service extends JFrame{
private TextArea ta;
private TextField tf;
private Button bt;
private ServerSocket serverSocket;
private Socket accept;
private DataOutputStream out;
private DataInputStream in;
public TextArea getTa() {
return ta;
}
public DataInputStream getIn() {
return in;
}
private void initUI() {
ta = new TextArea();
tf = new TextField();
bt = new Button("send");
addListener();
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(tf, BorderLayout.CENTER);
p.add(bt, BorderLayout.EAST);
this.add(p, BorderLayout.SOUTH);
this.add(ta, BorderLayout.CENTER);
this.setTitle("草草");
this.setSize(300, 400);
this.setLocation(1100, 400);
this.setVisible(true);
}
private void addListener() {
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = tf.getText();
tf.setText(null);
tf.requestFocus();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
ta.append("自 己 "+date+"\r\n\t"+ text+"\r\n");
try {
out.writeUTF(text);
} catch (IOException e2) {
e2.printStackTrace();
}
}
});
}
private void initConn(){
try {
serverSocket = new ServerSocket(8888);
accept = serverSocket.accept();
out = new DataOutputStream(accept.getOutputStream());
in = new DataInputStream(accept.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public Service() {
initUI();
initConn();
new ServiceThread(this).start();
}
public static void main(String[] args) {
new Service();
}
}
package com.qyl.TCP;
import java.awt.TextArea;
import java.io.DataInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClientThread extends Thread {
private Client client;
public ClientThread(Client client){
this.client = client;
}
@Override
public void run() {
try {
while (true) {
DataInputStream in = client.getIn();
String msg = in.readUTF();
TextArea ta = client.getTa();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
ta.append("草草 "+date+"\r\n\t"+ msg+"\r\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.qyl.TCP;
import java.awt.TextArea;
import java.io.DataInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ServiceThread extends Thread{
private Service service;
public ServiceThread(Service service) {
this.service = service;
}
@Override
public void run() {
try {
while (true) {
DataInputStream in = service.getIn();
String msg = in.readUTF();
TextArea ta = service.getTa();
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
ta.append("花花 " + date + "\r\n\t" + msg + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}