日期:2014-05-20  浏览次数:20842 次

新手求助关于用Socket制作聊天室的问题
public class Talk_clinet extends JFrame implements ActionListener {
String name, output, input;
JTextField down;// 输入框
TextArea up;// 输出框
JButton send;
String word;
Socket socket;

public Talk_clinet(String name) {
this.socket = socket;
this.name = name;
setLayout(null);
setTitle(name);
setSize(380, 580);
setVisible(true);
up = new TextArea(null, 5, 5, TextArea.SCROLLBARS_NONE);
up.setBounds(10, 10, 280, 340);
up.setEditable(false);
down = new JTextField();
down.setBounds(10, 360, 180, 120);
send = new JButton("发送");
send.setBounds(200, 390, 80, 80);
add(up);
add(down);
add(send);
send.addActionListener(this);
}

String sendMsg()// 发送聊天
{
return output;
}

void getMsg(String a)// 获得聊天
{
up.append(name + ": " + a + "\n");
}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("发送")) {
this.output = down.getText();
up.append(name + ": " + output + "\n");
down.setText(null);
}
}
}
public class Server {
private ServerSocket server;
private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象
private ExecutorService exec = Executors.newCachedThreadPool();// 创建线程池

public Server() throws IOException {
try {
server = new ServerSocket(8888);
Socket client = null;
System.out.println("服务器运行....");
while (true) {
client = server.accept();
list.add(client);// 添加服务端
exec.execute(new ChatTask(client));// 运行服务器端线程
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static class ChatTask implements Runnable {
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
private String msg = null;

public ChatTask(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
}

public void run() {
try {
while ((msg = br.readLine()) != null) {
sendMsg();
}
} catch (IOException e) {
System.out.println("发送错误");
}
}

private void sendMsg() throws IOException // 向list中所有的客户端发送消息
{
System.out.println(msg);
for (Socket client : list) {
pw = new PrintWriter(client.getOutputStream(), true);
pw.println(msg);
}
}
}

public static void main(String[] args) throws IOException {
new Server();
}
}
public class Chat {
Talk_clinet client = new Talk_clinet("li");
private static ExecutorService exec = Executors.newCachedThreadPool();

public Chat() {
try {
Socket socket = new Socket("121.250.216.47", 8888);
exec.execute(new clinet(socket));
System.out.println("连接到服务器....");

BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));

String msg ;
while ((msg = br.readLine()) != null) {
System.out.println(msg);
client.getMsg(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}

public class clinet implements Runnable {
private String msg;
private Socket socket;
PrintWriter pw;

public clinet(Socket socket) {
this.socket = socket;
}

public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
pw = new PrintWriter(socket.getOutputStream(), true);
String msn;
while (true) {