package Chat;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class chatClient implements ActionListener , Runnable{
JTextArea showArea;
JTextField msgText;
JFrame mainJframe;
JButton senBtn;
JScrollPane JSPane;
JPanel pane;
Container con;
Thread thread = null;
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.setColumns(30);
msgText.addActionListener(this);
senBtn = new JButton("发送");
senBtn.addActionListener(this);
pane = new JPanel();
pane.setLayout(new FlowLayout());
pane.add(msgText);
pane.add(senBtn);
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("对不起,没能连接服务器\n");
msgText.setEditable(false);
msgText.setEnabled(false);
}
}
public static void main(String[]args){
new chatClient();
}
public void run(){
try{
while(true){
showArea.append("赵冠文说: "+inFromserver.readUTF()+"\n");
Thread.sleep(1000);
}
}catch(IOException e){
}catch(InterruptedException E){}
}
public void actionPerformed(ActionEvent e){
String s = msgText.getText();
if(s.length()>0){
try {
outToServer.writeUTF(s);
outToServer.flush();
showArea.append("王阔说: "+msgText.getText()+"\n");
msgText.setText(null);
} catch (IOException e1) {
// TODO Auto-generated catch block
showArea.append("你的消息"+msgText.getText()+"未能发送出去\n");
}
}
}
}
package Chat;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
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("正在等待对话请求\n");
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("对不起,不能创建服务器\n");
msgText.setEditable(false);
sentBtn.setEnabled(false);
}
}
public static void main(String[]args){
new chatServer();
}
public void actionPerformed(ActionEvent e){
String s = msgText.getText();
if(s.length()>0){
try{
outToClient.writeUTF(s);
outToClient.flush();
showArea.append("赵冠文说: "+msgText.getText()+"\n");
msgText.setText(null);
}catch(IOException E){
showArea.append("你的消息"+msgText.getText()+"未能发送");
}
}
}
public void run(){
try{
while(true){
showArea.append("王阔说: "+inFromClient.readUTF()+"\n");
Thread.sleep(1000);
}
}catch(IOException E){}
catch(InterruptedException E){}
}
}
<img src="https://img-blog.csdn.net/20160913223715935?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);" alt="" />