这是个聊天室程序,服务器端是用图形界面的,客服端是用文本界面,我只想测试下,很粗造的.但是,在测试时,当服务器端发出信息后,客户端要过一个周期才可以收到.???????????????怎么才能弄成发一条收一条(客户端向服务器端发可以直接收到的)
服务器程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ActionDemo extends JFrame implements Runnable
{   static String msg;
    static String msg1="ycc";
    static boolean zj=false;
JTextField jtfName;
static JTextArea jtaChat;
JTextArea jtaInput;
JButton jbSend;
JButton jbClear;
public void run()
{

Container container=this.getContentPane();
JPanel p=new JPanel();
jtfName=new JTextField(10);
p.add(new JLabel("主题:Write once,Run Anywhere"));
p.add(new JLabel("昵称"));
p.add(jtfName);
container.add(p,BorderLayout.NORTH);
jtaChat=new JTextArea();
container.add(new JScrollPane(jtaChat),BorderLayout.CENTER);
Box box = new Box(BoxLayout.X_AXIS);
jtaInput=new JTextArea(3,20);
jbSend=new JButton();
jbClear=new JButton();
jbClear.setText("清除");
box.add(new JScrollPane(jtaInput));
box.add(jbClear);
box.add(jbSend);
container.add(box,BorderLayout.SOUTH);
jbClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtaInput.setText("");
}
});
Action sendMessage=new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
replaceMessage();
}
};
jtaInput.getInputMap().put(KeyStroke.getKeyStroke
("ENTER"),"send");//接受回车事件
jtaInput.getActionMap().put("send",sendMessage);
jbSend.setAction(sendMessage);
jbSend.setText("发送");
setSize(400,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void replaceMessage()
{
String message=jtfName.getText()+">"+jtaInput.getText()+"\n";
msg1=message;
jtaChat.insert(message,jtaChat.getDocument().getLength());
jtaInput.setText("");
}
private  static void replaceMessage_()
{
String message_=msg+"\n";
jtaChat.insert(message_,jtaChat.getDocument().getLength());
}
public static void judge()

    {
     if(zj==true)
     {
     replaceMessage_();
     zj=false;
     }
    }
public static void main(String[] args) throws UnknownHostException,IOException,ConnectException
{
    ActionDemo sc=new ActionDemo();
    Thread t=new Thread(sc);
    t.start();
ServerSocket aServerSocket=new ServerSocket(3434);
Socket aServer=null;
try
{
aServer=aServerSocket.accept();
try
{
 //  BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
BufferedReader in=new BufferedReader(new InputStreamReader(aServer.getInputStream()));
PrintWriter out=new PrintWriter(new OutputStreamWriter(aServer.getOutputStream()));String serverstring=null;
String clientstring=null;
//String aaa=input.readLine();
System.out.println("hello! enter the bye to exit.");
System.out.print("Server:wait client");
boolean done=false;
 while(!done)
 {
  serverstring=msg1;
    if(serverstring !=null)
   {
     out.println(serverstring);
     out.flush();
   
   }
    clientstring=in.readLine();
    if(clientstring !=null)
   {
System.out.println("client:"+clientstring);
msg=clientstring;
zj=true;
judge();

   }
     System.out.print("server:");
    serverstring=msg1;
    if(serverstring.equals("bye")) done=true;
   // msg1=null;
  }
}
catch(Exception e)
{
   System.out.println(e.getMessage());
}
finally
{
      aServer.close();
} }
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
    aServerSocket.close();
}}
}客户端程序:
import java.net.*;
import java.io.*;
public class SocketTest_SvrClit
{
public static void main(String[] args) throws UnknownHostException,IOException,ConnectException
{
Socket aClient=null;
aClient=new Socket("127.0.0.1",3434); //InetAddress.getLocalHost()
try
{
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
BufferedReader in=new BufferedReader(new InputStreamReader(aClient.getInputStream()));
PrintWriter out=new PrintWriter(new OutputStreamWriter(aClient.getOutputStream()));String clientString=null;
String serverString=null;
System.out.println("hello!enter bye to exit.");
boolean done=false;
while(!done)
{
serverString=in.readLine();
if(serverString !=null)
System.out.println("Server:"+serverString);
System.out.print("client:");
clientString=input.readLine();
if(clientString.equals("bye")) done=true;
if(clientString !=null) 
{
out.println(clientString);
out.flush();

}
in.close();
out.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
aClient.close();
}

}