求教编程问题
编了一个聊天系统的雏形 目前问题是在向客户端输入字符时,服务器端没输出同样的字符 编译是没错的
求大家帮我看看什么地方错了
客户端
package njyd.dsy.chat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient extends Frame{
DataOutputStream dos = null;
Socket s = null;
TextField tfText = new TextField();
TextArea taContent = new TextArea();
public static void main(String[] args) {
new ChatClient().launchFrame();
}
public void launchFrame() {
setLocation(300,300);
setSize(500,500);
add(tfText,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
pack();
this.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
disconnect();
System.exit(0);
}
});
tfText.addActionListener(new TFListener());
setVisible(true);
connect();
}
public void disconnect() {
try {
dos.close();
s.close();
} catch (
IOException e) {
e.printStackTrace();
}
}
public void connect() {
try {
s = new Socket("127.0.0.1",8888);
dos = new DataOutputStream(s.getOutputStream());
System.out.print("a Server connected");
} catch (
UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private class TFListener implements ActionListener{ /*响应enter 输入字符 */
@Override
public void actionPerformed(ActionEvent e) {
String str = tfText.getText().trim();
taContent.setText(str);
tfText.setText("");
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
服务器端
package njyd.dsy.chat;
import java.io.*;
import java.net.*;
public class ServerChat {
public static void main(String[] args) {
boolean started = false;
DataInputStream dis = null;
ServerSocket ss = null;
Socket s = null;
try {
ss = new ServerSocket(8888);
} catch(
BindException e){
System.out.println("端口使用中");
} catch(IOException e){
e.printStackTrace();
}
try{
started = true;
while(started) {
boolean flag = false;
s = ss.accept();
System.out.print("a Client connected!");
flag = true;
dis = new DataInputStream(s.getInputStream());
while(flag) {