解决方法:
将收发放到两个不同的线程
1. SendThread 发送消息线程
2. RecieveThread 接受消息线程
3. Server一旦接受到连接,就启动收发两个线程
4. Client 一旦建立了连接,就启动收发两个线程
1 SendThread
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;
}
@Override
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) {
e.printStackTrace();
}
}
}2 RecieveThread
package socket;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
public class RecieveThread extends Thread {
private Socket s;
public RecieveThread(Socket s) {
this.s = s;
}
@Override
public void run() {
InputStream is = null;
try {
is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
while(true){
String msg = dis.readUTF();
System.out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}3 Server
本书以培养高级网站建设与管理人才为目标,内容循序渐进,由浅入深,通过大量的实例系统全面地介绍了Linux+PHP+MySQL环境下的网络后台开发技术。本书详尽分析了近30个典型案例。包括计数器、网站流量统计、留言板、论坛系统、聊天室、投票与调查、用户管理、新闻发布系统、广告轮播、购物系统等等,力求让读者通过对案例的学习,轻松掌握PHP和MySQL的编程精要,迅速掌握网络后台开发技巧。 本书适
466
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) {
e.printStackTrace();
}
}
}4 Client
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}Server
package socket;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
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();
}
}
});
}
}Client
package socket;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
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();
}
}
});
}
}以上就是Java网络编程:多线程聊天的实现的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号