how2j.cn


工具版本兼容问题
如果使用单线程开发Socket应用,那么同一时间,要么收消息,要么发消息,不能同时进行。

为了实现同时收发消息,就需要用到多线程


本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频



4分36秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器



步骤 1 : 同时收发消息   
步骤 2 : 练习-有图形界面的聊天程序   
步骤 3 : 答案-有图形界面的聊天程序   

步骤 1 :

同时收发消息

edit
练习-服务端和客户端互聊 中,只能一人说一句,说了之后,必须等待另一个人的回复,才能说下一句。

这是因为接受和发送都在主线程中,不能同时进行。 为了实现同时收发消息,基本设计思路是把收发分别放在不同的线程中进行

1. SendThread 发送消息线程
2. RecieveThread 接受消息线程
3. Server一旦接受到连接,就启动收发两个线程
4. Client 一旦建立了连接,就启动收发两个线程
package socket; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class SendThread extends Thread{ private Socket s; public SendThread(Socket s){ this.s = s; } public void run(){ try { OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); while(true){ Scanner sc = new Scanner(System.in); String str = sc.next(); dos.writeUTF(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package socket; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class RecieveThread extends Thread { private Socket s; public RecieveThread(Socket s) { this.s = s; } public void run() { try { InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); while (true) { String msg = dis.readUTF(); System.out.println(msg); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package socket; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); System.out.println("监听在端口号:8888"); Socket s = ss.accept(); //启动发送消息线程 new SendThread(s).start(); //启动接受消息线程 new RecieveThread(s).start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package socket; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 8888); // 启动发送消息线程 new SendThread(s).start(); // 启动接受消息线程 new RecieveThread(s).start(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
步骤 2 :

练习-有图形界面的聊天程序

edit  姿势不对,事倍功半! 点击查看做练习的正确姿势
练习-有图形界面的聊天程序
步骤 3 :

答案-有图形界面的聊天程序

edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费4个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
查看本答案会花费4个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 中级总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频

6分5秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器


package socket; 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.util.Scanner; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class GUIServer { public static void main(String[] args) throws Exception { JFrame f = new JFrame(); f.setTitle("server"); f.setSize(400, 300); f.setLocation(100, 200); f.setLayout(null); JButton b = new JButton("send"); b.setBounds(10, 10, 80, 30); f.add(b); final JTextField tf = new JTextField(); tf.setBounds(10, 110, 80, 30); f.add(tf); final JTextArea ta = new JTextArea(); ta.setBounds(110,10, 200, 300); f.add(ta); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); ServerSocket ss = new ServerSocket(8888); System.out.println("listenning on port:8888"); final Socket s = ss.accept(); new Thread() { public void run() { while (true) { try { DataInputStream dis = new DataInputStream( s.getInputStream()); String text = dis.readUTF(); ta.append(text+"\r\n"); } catch (Exception e) { e.printStackTrace(); } } } }.start(); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = tf.getText(); ta.append(text+"\r\n"); try { DataOutputStream dos = new DataOutputStream( s.getOutputStream()); dos.writeUTF(text); } catch (Exception ex) { ex.printStackTrace(); } } }); } }
package socket; 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.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; public class GUIClient { public static void main(String[] args) throws Exception { JFrame f = new JFrame(); f.setTitle("client"); f.setSize(400, 300); f.setLocation(600, 200); f.setLayout(null); JButton b = new JButton("send"); b.setBounds(10, 10, 80, 30); f.add(b); final JTextField tf = new JTextField(); tf.setBounds(10, 110, 80, 30); f.add(tf); final JTextArea ta = new JTextArea(); ta.setBounds(110,10, 200, 300); f.add(ta); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); final Socket s = new Socket("127.0.0.1", 8888); new Thread() { public void run() { while (true) { try { DataInputStream dis = new DataInputStream( s.getInputStream()); String text = dis.readUTF(); ta.append(text+"\r\n"); } catch (Exception e) { e.printStackTrace(); } } } }.start(); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = tf.getText(); ta.append(text+"\r\n"); try { DataOutputStream dos = new DataOutputStream( s.getOutputStream()); dos.writeUTF(text); } catch (Exception ex) { ex.printStackTrace(); } } }); } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2024-07-17 GUI界面完善客户端与服务器通信
虚心求学




开始监听,端口号:8888 监听到连接ServerSocket[addr=0.0.0.0/0.0.0.0,localport=8888] 你好 你好,我是服务器 收到,我是客户端 好的,你可以退出了 收到,准备退出 end end 已断开连接,停止接收消息
加载中
-----------------------------客户端-------------------------------
 
package testNet;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
 
public class Client {
    public static void main(String[] args) {
        JFrame f = new JFrame("网络信息收发器");
        f.setSize(800, 600);
        f.setLocation(200, 100);
        f.setLayout(new BorderLayout());
 
        // 发信文本框
        JTextArea txtMsg = new JTextArea();
        txtMsg.setLocation(0, 0);
        txtMsg.setFont(new Font("宋体", Font.PLAIN, 16));
 
        // 发信按钮
        JButton btnSend = new JButton("Send");
        btnSend.setBounds(500, 300, 100, 60);
 
        // 发信容器
        JPanel p = new JPanel();
        p.setSize(new Dimension(600, 100));
        p.add(txtMsg);
        p.add(btnSend);
 
        // 聊天面版
        JTextArea txtbox = new JTextArea();
        txtbox.setFont(new Font("宋体", Font.PLAIN, 16));
        JScrollPane sPane = new JScrollPane(txtbox);
        txtbox.setPreferredSize(new Dimension(600, 400));
        txtMsg.setPreferredSize(new Dimension(600, 100));
        btnSend.setPreferredSize(new Dimension(100, 100));
        f.add(sPane, BorderLayout.NORTH);
        f.add(p, BorderLayout.SOUTH);
        // txtbox.setSize(new Dimension(600,80));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
 
        // 初始线程
        try {
            Socket s = new Socket("127.0.0.1", 8888);
            System.out.println(s);
            System.out.println("成功连接服务器!可以开始发送消息");
            SendThread sender = new SendThread("用户端发信线程", s);
            RecieveThread receiver = new RecieveThread("用户端收信线程", s);
            sender.start();
            receiver.start();
            new Thread(() -> {
                while (!receiver.isShutdown()) {
                    String receiveMsg = receiver.getMsg();
                    if (receiveMsg != null && !receiveMsg.isEmpty()) {
                        txtbox.append("服务端:\r\n");
                        txtbox.append(receiveMsg + "\r\n\r\n");
                    }
                }
            }).start();
 
            // 事件监视
            btnSend.addActionListener(new ActionListener() {
 
                @Override
                public void actionPerformed(ActionEvent e) {
                    String msg = txtMsg.getText();
                    if (msg != null && !msg.isEmpty()) {
                        txtMsg.setText(null);
                        sender.addMsg(msg);
                        txtbox.append("客户端:\r\n");
                        txtbox.append(msg + "\r\n\r\n");
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
    public static void getOrPOST() {
 
    }
}
 
-------------------------------服务端-----------------------------------
 
 
package testNet;
 
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
 
public class Server {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(System.in);
            ServerSocket server = new ServerSocket(8888);
            System.out.println("开始监听,端口号:8888");
            Socket client = server.accept();
            System.out.println("监听到连接" + server);
             
            RecieveThread receiver = new RecieveThread("服务端收信线程", client);
            receiver.start();
            new Thread(()->{
                while(!receiver.isShutdown())
                {
                    String receiveMsg = receiver.getMsg();
                    if(receiveMsg!=null&&!receiveMsg.isEmpty())
                        System.out.printf("\t\t%s%n",receiveMsg);
                }
            }).start();
            SendThread sender = new SendThread("服务端发信线程", client);
            sender.start();
            while(!sender.lastMsg.equals("end"))
            {
                String msg = scanner.nextLine();
                sender.addMsg(msg);
            }
            sender.shutdown();
            receiver.shutdown();
            //关闭资源
            scanner.close();
            server.close();
            System.out.println("已断开连接,停止接收消息");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static String selectFromDB(String receive) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sql = "select * from conservation where receive = ?";
        try (Connection c = DriverManager
                .getConnection("jdbc:mysql://127.0.0.1:3306" + "/robot?characterEncoding = UTF-8", "root", "admin");
                PreparedStatement ps = c.prepareStatement(sql)) {
            ps.setString(1, receive);
            ps.execute();
            ResultSet set = ps.getResultSet();
            List<String> strs = new ArrayList<String>();
            while (set.next()) {
                strs.add(set.getString("reply"));
            }
            Random r = new Random();
            if (!strs.isEmpty())
                return strs.get(r.nextInt(strs.size()>0?strs.size():1 - 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
 
--------------------------------------线程相关-------------------------------------------
 
 
package testNet;
 
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
 
public class SendThread extends Thread{
    private DataOutputStream writer;
    private List<String>replys;
    private boolean isShutDown = false;
    public String lastMsg = "normal";
    public SendThread(String name,Socket s)
    {
        super(name);
        try {
            OutputStream out = s.getOutputStream();
            writer = new DataOutputStream(out);
            replys = new LinkedList<String>();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run()
    {
        while(true)
        {
            sendMsg();
            if(isShutDown)
            {
                break;
            }
        }
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public synchronized void sendMsg()
    {
        try {
            while(replys.isEmpty())
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(!replys.isEmpty())
        {
            try {
                writer.writeUTF(replys.remove(0));
            } catch (IOException e) {
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            }
        }
    }
    public synchronized void addMsg(String reply)
    {
        lastMsg = reply;
        replys.add(reply);
        this.notifyAll();
    }
    public boolean isShutdown()
    {
        return this.isShutDown;
    }
    public void shutdown()
    {
        isShutDown = true;
    }
}
 
package testNet;
 
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
 
public class RecieveThread extends Thread{
    private DataInputStream reader;
    public List<String>receives;
    private boolean isShutDown = false;
    public RecieveThread(String name,Socket s)
    {
        super(name);
        try {
            InputStream in = s.getInputStream();
            reader = new DataInputStream(in);
            receives = new LinkedList<>();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void run()
    {
        while(true)
        {
            receiveMsg();
            if(isShutDown)
            {
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public synchronized void receiveMsg()
    {
        String receive = null;
        try {
            if((receive =reader.readUTF())!= null)
            {
                this.receives.add(receive);
            }
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }
    public synchronized String getMsg()
    {
        if(!receives.isEmpty())
        {
            return receives.remove(0);
        }
        return null;
    }
    public boolean isShutdown()
    {
        return this.isShutDown;
    }
    public void shutdown()
    {
        isShutDown = true;
    }
 
}

							





回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2024-07-17 基于 多线程 和 网络 实现客户端和服务端通信
虚心求学




收发线程不冲突,独立运行。 当用户或服务端 发送 end 指令中断连接,关闭资源。
加载中
---------------------------客户端--------------------------------
package testNet;

import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Server {
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);
			ServerSocket server = new ServerSocket(8888);
			System.out.println("开始监听,端口号:8888");
			Socket client = server.accept();
			System.out.println("监听到连接" + server);
			
			RecieveThread receiver = new RecieveThread("服务端收信线程", client);
			receiver.start();
			new Thread(()->{
				while(!receiver.isShutdown())
				{
					String receiveMsg = receiver.getMsg();
					if(receiveMsg!=null&&!receiveMsg.isEmpty())
						System.out.printf("\t\t%s%n",receiveMsg);
				}
			}).start();
			SendThread sender = new SendThread("服务端发信线程", client);
			sender.start();
			while(!sender.lastMsg.equals("end"))
			{
				String msg = scanner.nextLine();
				sender.addMsg(msg);
			}
			sender.shutdown();
			receiver.shutdown();
			//关闭资源
			scanner.close();
			server.close();
			System.out.println("已断开连接,停止接收消息");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String selectFromDB(String receive) {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (Exception e) {
			e.printStackTrace();
		}
		String sql = "select * from conservation where receive = ?";
		try (Connection c = DriverManager
				.getConnection("jdbc:mysql://127.0.0.1:3306" + "/robot?characterEncoding = UTF-8", "root", "admin");
				PreparedStatement ps = c.prepareStatement(sql)) {
			ps.setString(1, receive);
			ps.execute();
			ResultSet set = ps.getResultSet();
			List<String> strs = new ArrayList<String>();
			while (set.next()) {
				strs.add(set.getString("reply"));
			}
			Random r = new Random();
			if (!strs.isEmpty())
				return strs.get(r.nextInt(strs.size()>0?strs.size():1 - 1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}


----------------------------服务端---------------------------------

package testNet;

import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Server {
	public static void main(String[] args) {
		try {
			Scanner scanner = new Scanner(System.in);
			ServerSocket server = new ServerSocket(8888);
			System.out.println("开始监听,端口号:8888");
			Socket client = server.accept();
			System.out.println("监听到连接" + server);
			
			RecieveThread receiver = new RecieveThread("服务端收信线程", client);
			receiver.start();
			new Thread(()->{
				while(!receiver.isShutdown())
				{
					String receiveMsg = receiver.getMsg();
					if(receiveMsg!=null&&!receiveMsg.isEmpty())
						System.out.printf("\t\t%s%n",receiveMsg);
				}
			}).start();
			SendThread sender = new SendThread("服务端发信线程", client);
			sender.start();
			while(!sender.lastMsg.equals("end"))
			{
				String msg = scanner.nextLine();
				sender.addMsg(msg);
			}
			sender.shutdown();
			receiver.shutdown();
			//关闭资源
			scanner.close();
			server.close();
			System.out.println("已断开连接,停止接收消息");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String selectFromDB(String receive) {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (Exception e) {
			e.printStackTrace();
		}
		String sql = "select * from conservation where receive = ?";
		try (Connection c = DriverManager
				.getConnection("jdbc:mysql://127.0.0.1:3306" + "/robot?characterEncoding = UTF-8", "root", "admin");
				PreparedStatement ps = c.prepareStatement(sql)) {
			ps.setString(1, receive);
			ps.execute();
			ResultSet set = ps.getResultSet();
			List<String> strs = new ArrayList<String>();
			while (set.next()) {
				strs.add(set.getString("reply"));
			}
			Random r = new Random();
			if (!strs.isEmpty())
				return strs.get(r.nextInt(strs.size()>0?strs.size():1 - 1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

--------------------------收发线程-------------------------------
package testNet;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

public class RecieveThread extends Thread{
	private DataInputStream reader;
	public List<String>receives;
	private boolean isShutDown = false;
	public RecieveThread(String name,Socket s)
	{
		super(name);
		try {
			InputStream in = s.getInputStream();
			reader = new DataInputStream(in);
			receives = new LinkedList<>();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void run()
	{
		while(true)
		{
			receiveMsg();
			if(isShutDown)
			{
				break;
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public synchronized void receiveMsg()
	{
		String receive = null;
		try {
			if((receive =reader.readUTF())!= null)
			{
				this.receives.add(receive);
			}
		} catch (IOException e) {
			//e.printStackTrace();
		}
	}
	public synchronized String getMsg()
	{
		if(!receives.isEmpty())
		{
			return receives.remove(0);
		}
		return null;
	}
	public boolean isShutdown()
	{
		return this.isShutDown;
	}
	public void shutdown()
	{
		isShutDown = true;
	}

}
------------------------------------------------------------------

package testNet;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

public class SendThread extends Thread{
	private DataOutputStream writer;
	private List<String>replys;
	private boolean isShutDown = false;
	public String lastMsg = "normal";
	public SendThread(String name,Socket s)
	{
		super(name);
		try {
			OutputStream out = s.getOutputStream();
			writer = new DataOutputStream(out);
			replys = new LinkedList<String>();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void run()
	{
		while(true)
		{
			sendMsg();
			if(isShutDown)
			{
				break;
			}
		}
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public synchronized void sendMsg()
	{
		try {
			while(replys.isEmpty())
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if(!replys.isEmpty())
		{
			try {
				writer.writeUTF(replys.remove(0));
			} catch (IOException e) {
			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
		}
	}
	public synchronized void addMsg(String reply)
	{
		lastMsg = reply;
		replys.add(reply);
		this.notifyAll();
	}
	public boolean isShutdown()
	{
		return this.isShutDown;
	}
	public void shutdown()
	{
		isShutDown = true;
	}
}



							





回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到





2022-02-11 想问一下站长 如果想要通过输入一个特定的消息来关闭这个程序 怎么可以实现。
2022-01-15 练习 - 有图形界面的聊天程序
2021-03-19 图形界面的聊天工具写出来了


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 20 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 中级-网络编程-多线程聊天 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图