一个聊天程序 求大神指点

importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;publicclassC... import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame{
Socket s = null;

TextField tfTxt = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().LaunchFrame();
}

public void LaunchFrame() {
setLocation(300, 400);
setSize(300, 300);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
tfTxt.addActionListener(new TFListener());
setVisible(true);
connect();

}

public void connect() {
try {
s = new Socket("127.0.0.1", 8888);
System.out.println("connect!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try {
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
dos.flush();
dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
这个是Client端 运行时出现
java.net.SocketException: Socket is closed
at java.net.Socket.getOutputStream(Socket.java:943)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
请问这样是为什么 上面一段有删
展开
 我来答
readclub读者
2014-09-01 · 超过30用户采纳过TA的回答
知道答主
回答量:104
采纳率:0%
帮助的人:42.6万
展开全部
服务端的呢?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
桃儿BDE
推荐于2016-01-03 · TA获得超过201个赞
知道答主
回答量:181
采纳率:25%
帮助的人:64.3万
展开全部
给个简单的客户端的例子,lz对比自己的码看看有什么出入 (注意:ClientHandler是inner class)
package chap15;
import java.io.*;
import java.net.*;
import java.util.*;

public class VerySimpleChatServer
{
ArrayList clientOutputStreams;

public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;

public ClientHandler(Socket clientSOcket) {
try {
sock = clientSOcket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);

} catch (Exception ex) { ex.printStackTrace(); }
}

public void run() {
String message;
try {
while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
tellEveryone(message);
}
} catch (Exception ex) { ex.printStackTrace(); }
}
}

public static void main(String[] args) {
new VerySimpleChatServer().go();
}

public void go() {
clientOutputStreams = new ArrayList();
try {
ServerSocket serverSock = new ServerSocket(5000);
while(true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);

Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch (Exception ex) { ex.printStackTrace(); }
}

public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch (Exception ex) { ex.printStackTrace(); }
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式