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

小冷不冷 发布于 2012/08/27 22:27
阅读 1K+
收藏 0
服务器端:

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();
}
}
}

加载中
0
颠才泪狼空色
颠才泪狼空色
服务器端明显存在问题吧?你看一下你那个发送线程
小冷不冷
小冷不冷
我属菜鸟一枚 不懂啊 请帮我改一下好不
0
Aenlia
Aenlia

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

	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();
				}
			}
		}
	}


返回顶部
顶部