开发者社区> 问答> 正文

java Socket编程 为什么起多个客户端之后就会卡死在那(源码如下)? 400 报错

java Socket编程 为什么起多个客户端之后就会卡死在那(源码如下)? 400 报错

服务器端:


package chat_01;

import java.awt.BorderLayout;
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;
import javax.swing.JTextArea;

public class ChatServer extends JFrame {
private static final long serialVersionUID = 1L;

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH����ss");
private JTextArea jta;
private DataInputStream dis;
private DataOutputStream dos;
private ServerSocket serverSocket;
private String dateString;

public static void main(String[] args) {
ChatServer server = new ChatServer();
server.showServer();
server.send();
}

public void showServer() {
jta = new JTextArea();
this.setLayout(new BorderLayout());
this.add(jta, BorderLayout.CENTER);

this.setLocation(300, 100);
this.setSize(500, 400);
this.setTitle("服务器端");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void send() {
try {
serverSocket = new ServerSocket(8007);

while (true) {
Socket socket = serverSocket.accept();
System.out.println("一个用户连接上了。。。");
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
new Thread(new ClientThread()).start();
}

} catch (IOException e) {
e.printStackTrace();
}
}

private class ClientThread implements Runnable {
public void run() {
while (true) {
try {
Date currentTime = new Date();
dateString = format.format(currentTime);
String str = dis.readUTF();
dos.writeUTF(str);
jta.append(dateString + "\n" + str + "\n");
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
}




客户端:

package chat_01;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.*;

public class ChatClient extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;

private JTextArea jta;
private JTextField jtf;
private JButton jbt;
DataOutputStream dos;
DataInputStream dis;

public static void main(String[] args) {
ChatClient client = new ChatClient();
client.connected();
client.showClient();
}

public void showClient() {
jta = new JTextArea();
jta.setEditable(false);
jtf = new JTextField();
jbt = new JButton("发送");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(jtf, BorderLayout.CENTER);
panel.add(jbt, BorderLayout.EAST);

this.setLayout(new BorderLayout());
this.add(jta, BorderLayout.CENTER);
this.add(panel, BorderLayout.SOUTH);

jtf.addActionListener(this);
jbt.addActionListener(this);

this.setLocation(300, 100);
this.setSize(500, 400);
this.setTitle("客户端");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH����ss");
Date currentTime = new Date();
String dateString = format.format(currentTime);

String text = jtf.getText();
jta.append(dateString + "\n");
jta.append(text + "\n");
jtf.setText("");
jtf.grabFocus();// 文本框获取光标(焦点)
try {
dos.writeUTF(text);
dos.flush();
jta.append(dis.readUTF());
System.out.println(text + "client");
} catch (IOException e1) {
e1.printStackTrace();
}
}

public void connected() {
try {
Socket socket = new Socket("127.0.0.1", 8007);
dos = new DataOutputStream(socket.getOutputStream());
dis = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

展开
收起
爱吃鱼的程序员 2020-05-30 23:43:57 861 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    服务器端明显存在问题吧?你看一下你那个发送线程######我属菜鸟一枚 不懂啊 请帮我改一下好不######

    服务端每次接受的都是同一个对象,当然会有问题啦

    	private static final long serialVersionUID = 1L;
    
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
    
    	private JTextArea jta;
    
    	private ServerSocket serverSocket;
    
    	private String dateString;
    
    	public static void main(String[] args) {
    		ChatServer server = new ChatServer();
    		server.showServer();
    		server.send();
    	}
    
    	public void showServer() {
    		jta = new JTextArea();
    		this.setLayout(new BorderLayout());
    		this.add(jta, BorderLayout.CENTER);
    
    		this.setLocation(300, 100);
    		this.setSize(500, 400);
    		this.setTitle("服务器端");
    		this.setVisible(true);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    	public void send() {
    		try {
    			serverSocket = new ServerSocket(8007);
    
    			while (true) {
    				Socket socket = serverSocket.accept();
    				System.out.println("一个用户连接上了。。。");
    				new Thread(new ClientThread(socket)).start();
    			}
    
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	private class ClientThread implements Runnable {
    
    		private Socket s;
    
    		public ClientThread(Socket s) {
    			this.s = s;
    		}
    
    		public void run() {
    			while (true) {
    				try {
    					Date currentTime = new Date();
    					dateString = format.format(currentTime);
    					DataInputStream dis = new DataInputStream(s.getInputStream());
    					String str = dis.readUTF();
    					DataOutputStream dos = new DataOutputStream(s.getOutputStream());
    					dos.writeUTF(str);
    					jta.append(dateString + "\n" + str + "\n");
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    	}


    2020-05-30 23:43:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载