为什么我第二次输入内容发送到服务器,服务器却不能收到?

源代码:
ChatServer.java
import java.io.DataInputStream;
import java.io.IOException;
import java.net.*;
public class ChatServer {

public static void main(String[] args) {

    boolean  started = false;
       try {
        ServerSocket ss = new ServerSocket(8888);
        started = true;
        while(true)
        {
        Socket s = new Socket();

        s = ss.accept();
        System.out.println("a client connected!!!");
        DataInputStream dis = new DataInputStream (s.getInputStream());
        String str = dis.readUTF();
        System.out.println(str);
        //dis.close();
        }
    } catch (IOException e) {

        e.printStackTrace();
    }

}

}
ChatClient.java
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class ChatClient extends Frame {
//成员变量
Socket s = null;
TextField tfInput = new TextField();
TextArea taDisplay = new TextArea();
DataOutputStream dos = null;
public static void main(String[] args) {
new ChatClient().launchFrame();
}
//新建launchFrame方法建立窗口
private void launchFrame()
{
setLocation(400,300);
setSize(300,300);
tfInput.addActionListener(new InputListener());
add(tfInput,BorderLayout.SOUTH);
add(taDisplay,BorderLayout.NORTH);
addWindowListener(new WindowAdapter(){

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

    });
    pack();
    setVisible(true);
    Connect();
}
//断开连接方法
public void Disconnect()
{
    try {
        dos.close();
        s.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

//定义输入框监听器类
private class InputListener implements ActionListener
{

    public void actionPerformed(ActionEvent e) {
                 String str = null;
                 str = tfInput.getText();
                 taDisplay.append(str+"\n");
                 tfInput.setText("");
                 try {
                    dos.writeUTF(str);
                    dos.flush();
                    //dos.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
    }

}

//单独建立一个方法,用于连接到服务器
private void Connect()
{
try {
s = new Socket ("127.0.0.1",8888);
dos = new DataOutputStream(s.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

[code="java"]
while(true)
{
Socket s = new Socket();

s = ss.accept();
System.out.println("a client connected!!!");
DataInputStream dis = new DataInputStream (s.getInputStream());
String str = dis.readUTF();
System.out.println(str);
}
[/code]
服务端ss.accept每次接收都是一个新的socket客户端连接, 但是你客户端只创建了一次连接, 所以第二次自然就接收不到, 如果你只是为了做单线程的处理的话, 代码这样改就行了.
[code="java"]
Socket s = new Socket();
s = ss.accept();
System.out.println("a client connected!!!");
DataInputStream dis = new DataInputStream (s.getInputStream());
while(true)
{
String str = dis.readUTF();
System.out.println(str);
}
[/code]

[quote][code="java"]while(true)
{

Socket s = new Socket();

s = ss.accept();
System.out.println("a client connected!!!");
DataInputStream dis = new DataInputStream (s.getInputStream());
String str = dis.readUTF();
System.out.println(str);
//dis.close();
}
} catch (IOException e) {

e.printStackTrace();
} [/code][/quote]
连接没有保持,丢失了。
如果你要多线程例子,我这边有一个:
[url]http://bbjava.iteye.com/blog/1122006[/url],