/*******************************客户端****************************************/
package day13update;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.*;
public class ClientSwing extends JFrame implements ActionListener, Runnable {
JButton jb1 = null;
JButton jb2 = null;
JTextArea jArea = null;
JTextField jField = null;
Socket socket1 = null;
Socket socket2 = null;
BufferedReader bReader = null;
DataInputStream dStream = null;
public static void main(String[] args) {
ClientSwing cSwing = new ClientSwing();
}
public ClientSwing() {
super("客户端");
jb1 = new JButton("发送");
jb1.addActionListener(this);
jb1.setSize(20, 10);
jb2 = new JButton("接收");
jb2.addActionListener(this);
jArea = new JTextArea();
jField = new JTextField();
this.add(jArea);
this.add(jb1);
// this.add(jb2);
this.add(jField);
// 设置布局格式
this.setLayout(new GridLayout(3, 1));
this.setVisible(true);
this.setSize(200, 260);
this.setLocation(300, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// 一直读
try {
socket1 = new Socket("127.0.0.1", 3000);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Thread thread = new Thread(this);
thread.start();
}
@Override
public void actionPerformed(ActionEvent aEvent) {
if (aEvent.getActionCommand() == "发送") {
try {
OutputStream os = socket1.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
// 从jtextField中得到要发送的内容
String jtatext = jField.getText();
dos.writeUTF(jtatext);
jArea.append("我说:" + jtatext + "\n");
jField.setText("");
// 读数据
os.flush();
dos.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
} else if (aEvent.getActionCommand() == "接收") {
}
}
@Override
public void run() {
try {
// bReader = new BufferedReader(new
// InputStreamReader(socket1.getInputStream()));
// String receive=bReader.readLine();
while (true) {
dStream = new DataInputStream(socket1.getInputStream());
String receive = dStream.readUTF();
// System.out.println("从服务器端得到的消息" + receive);
jArea.append("服务器:" + receive + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// try {
// bReader.close();
// } catch (IOException e) {
//
// e.printStackTrace();
// }
}
}
}
/*****************************************服务器端***********************************************/
package day13update; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import java.net.*; import javax.swing.*; public class SerSwing extends JFrame implements ActionListener, Runnable { JButton jb1 = null; JButton jb2 = null; JTextField jField = null; JTextArea jta = null; ServerSocket server = null; ServerSocket server1 = null; DataInputStream dStream = null; InputStream is = null; Socket socket = null; Socket socket1 = null; String readString = null; DataOutputStream dStream1 = null; public static void main(String[] args) { SerSwing serSwing = new SerSwing(); } public SerSwing() { super("服务器端"); jb1 = new JButton("接收"); jb1.addActionListener(this); jb1.setSize(80, 90); jb2 = new JButton("发送"); jb2.addActionListener(this); jField = new JTextField(); jta = new JTextArea(); this.add(jta); //this.add(jb1); this.add(jb2); this.add(jField); // 设置布局格式 this.setLayout(new GridLayout(3,1)); this.setVisible(true); this.setSize(200, 260); this.setLocation(300, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); try { server = new ServerSocket(3000); socket = server.accept(); } catch (IOException e) { e.printStackTrace(); } Thread thread = new Thread(this); thread.start(); } @Override public void actionPerformed(ActionEvent aEvent) { if (aEvent.getActionCommand() == "接收") { } else if (aEvent.getActionCommand() == "发送") { /** * 在接收的时候socket已经关闭了 socket.notify(); 这样做不行,socket中的东西会丢失,新建一个socket */ // 向外面写数据 try { dStream1 = new DataOutputStream(socket.getOutputStream()); String str = jField.getText(); dStream1.writeUTF(str); jField.setText(""); jta.append("我说:" + str+"\n"); } catch (IOException e) { e.printStackTrace(); } finally { //socket最好不要关闭 // try { // dStream1.close(); // } catch (IOException e) { // // e.printStackTrace(); // } } } } @Override public void run() { try { while (true)// 不断的接收 为什么不能不断的接收 { is = socket.getInputStream(); dStream = new DataInputStream(is); readString = dStream.readUTF(); jta.append("客户说:" + readString + "\n"); // System.out.print(readString); // 这里 // is.close(); // dStream.close(); } } catch (Exception e) { } finally { } } }