想写一个一人对一人的聊天程序可是遇到了些问题;
这是客户端代码,其中send是一个发送按钮,想按下后把文本域中的内容发出 
且定义了 TextField serverMessage;和TextArea messageDisplay想把从服务器端
的内容在messageDisplay上显示出来。可是老是失败。经过测试发现1,2处的打印语句始终不能运行。望高手能给我分析一下原因。谢谢先~~客户端代码
public void runClient()
{
  try
   {
    final Socket s=new Socket(InetAddress.getByName("localhost"),6000);
    final OutputStream os=s.getOutputStream();
    final InputStream is=s.getInputStream();

    send.addActionListener(new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
     {
      try
{ String str=serverMessage.getText();

os.write(str.getBytes());
         System.out.println(str);//-----------1

}
} catch(IOException ioe){}
}
});        os.write("This is Client".getBytes());
        byte[] buf=new byte[1024];
        int length=is.read(buf);
        messageDisplay.append(new String(buf,0,length));
os.close();
is.close();
s.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
服务器端代码
public void runServer()
{
 try
 {
 final ServerSocket ss=new ServerSocket(6000);//设置服务端口号
 final Socket s=ss.accept();
 final OutputStream os=s.getOutputStream();
 final InputStream is=s.getInputStream();

 send.addActionListener(new ActionListener()
{
 public void actionPerformed(ActionEvent e)
 {
 try
 {
   String str=clientMessage.getText();
   os.write(str.getBytes());
   System.out.println(clientMessage.getText());//-----------------2  }
  catch(IOException ioe){}
  }
  });

  os.write("This is Server".getBytes());
  byte[] buf=new byte[1024];
  int length=is.read(buf);
  messageDisplay.append(new String(buf,0,length));  os.close();
  is.close();
  s.close();
  ss.close();
  }
  catch(Exception ex)
  {
     ex.printStackTrace();
  }
}

解决方案 »

  1.   

    String str=clientMessage.getText();

       String str=serverMessage.getText();
    上面的clientMessage和serverMessage是如何获取的?
    最好全部贴出来吧……
      

  2.   

    恩,好的
    Server.java
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.net.*;
    import java.io.*;public class Server
    {
    JTextArea messageDisplay=new JTextArea(11,8);
    JTextField clientMessage;
    JLabel sendToLabel;
    JButton send;
    JButton close;
      JPanel myPanelCenter=new JPanel();
    JPanel myPanelDown=new JPanel();
    GridBagLayout gridBag=new GridBagLayout();
    GridBagConstraints gridBagCon=new GridBagConstraints();

      public void init()
    {
      messageDisplay.setLineWrap(true);
      
      myPanelCenter.setLayout(gridBag);
    gridBagCon.gridx=0;
    gridBagCon.gridy=0;
    gridBagCon.anchor=GridBagConstraints.CENTER;
    gridBagCon.insets=new Insets(3,3,3,3);
    sendToLabel=new JLabel("SendMessage:");
    gridBag.setConstraints(sendToLabel,gridBagCon);
    myPanelCenter.add(sendToLabel);

    gridBagCon.gridx=1;
    gridBagCon.gridy=0;
    clientMessage=new JTextField(25);
    gridBagCon.insets=new Insets(5,1,5,5);
    gridBagCon.anchor=GridBagConstraints.NORTH;
          gridBag.setConstraints(clientMessage,gridBagCon);
    myPanelCenter.add(clientMessage);

    myPanelDown.setLayout(gridBag);
    gridBagCon.gridx=0;
    gridBagCon.gridy=0;
    gridBagCon.gridwidth=2;
    gridBagCon.anchor=GridBagConstraints.NORTH;
    gridBagCon.insets=new Insets(3,3,3,20);
    send=new JButton("Send");
    gridBag.setConstraints(send,gridBagCon);
    myPanelDown.add(send);

    gridBagCon.gridx=3;
    gridBagCon.gridy=0;
    gridBagCon.gridwidth=2;
    gridBagCon.insets=new Insets(3,20,3,3);
    gridBagCon.anchor=GridBagConstraints.NORTH;
    close=new JButton("Close");
    gridBag.setConstraints(close,gridBagCon);
    myPanelDown.add(close);
    }

    public void runServer()
    {
    try
    {
    final ServerSocket ss=new ServerSocket(6000);//设置服务端口号
    final Socket s=ss.accept();
    final OutputStream os=s.getOutputStream();
    final InputStream is=s.getInputStream();

    send.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
      String str=clientMessage.getText();
                  System.out.println(str);
    os.write(str.getBytes());
                  System.out.println("Success");
    }    catch(IOException ioe){}
    }
    });
    os.write("This is Server".getBytes());
    //os.write("This is Server".getBytes());//参数是字节流

    //byte[] buf=new byte[100];
    //int length=is.read(buf);
    //System.out.println(new String(buf,0,length));

    byte[] buf=new byte[1024];
    int length=is.read(buf);
    messageDisplay.append(new String(buf,0,length));
    os.close();
    is.close();
    s.close();
    ss.close();
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    }

    public static void main(String[] args)
    {
    final JFrame f=new JFrame("Server");
    Server s=new Server();
    s.init();
    s.runServer();
    f.setLayout(new BorderLayout());
    s.messageDisplay.setEditable(false);
    f.getContentPane().add(s.messageDisplay,BorderLayout.NORTH);
    f.getContentPane().add(s.myPanelCenter,BorderLayout.CENTER);
    f.getContentPane().add(s.myPanelDown,BorderLayout.SOUTH);
        f.setSize(400,300);
        f.setResizable(false);
        f.pack();
        f.show();
        
        s.close.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    f.dispose();
    System.exit(0);
    }
    });

        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
     }
    }////////////////////////////////////////////////////////////////////////////////
    Client.javaimport javax.swing.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.net.*;
    import java.io.*;public class Client
    {
    JTextArea messageDisplay=new JTextArea(11,8);
    JTextField serverMessage;
    JLabel sendToLabel;
    JButton send;
    JButton close;
      JPanel myPanelCenter=new JPanel();
    JPanel myPanelDown=new JPanel();
    GridBagLayout gridBag=new GridBagLayout();
    GridBagConstraints gridBagCon=new GridBagConstraints();

      public void init()
    {
      messageDisplay.setLineWrap(true);
      
      myPanelCenter.setLayout(gridBag);
    gridBagCon.gridx=0;
    gridBagCon.gridy=0;
    gridBagCon.anchor=GridBagConstraints.CENTER;
    gridBagCon.insets=new Insets(3,3,3,3);
    sendToLabel=new JLabel("SendMessage:");
    gridBag.setConstraints(sendToLabel,gridBagCon);
    myPanelCenter.add(sendToLabel);

    gridBagCon.gridx=1;
    gridBagCon.gridy=0;
    serverMessage=new JTextField(25);
    gridBagCon.insets=new Insets(5,1,5,5);
    gridBagCon.anchor=GridBagConstraints.NORTH;
          gridBag.setConstraints(serverMessage,gridBagCon);
    myPanelCenter.add(serverMessage);

    myPanelDown.setLayout(gridBag);
    gridBagCon.gridx=0;
    gridBagCon.gridy=0;
    gridBagCon.gridwidth=2;
    gridBagCon.anchor=GridBagConstraints.NORTH;
    gridBagCon.insets=new Insets(3,3,3,20);
    send=new JButton("Send");
    gridBag.setConstraints(send,gridBagCon);
    myPanelDown.add(send);

    gridBagCon.gridx=3;
    gridBagCon.gridy=0;
    gridBagCon.gridwidth=2;
    gridBagCon.insets=new Insets(3,20,3,3);
    gridBagCon.anchor=GridBagConstraints.NORTH;
    close=new JButton("Close");
    gridBag.setConstraints(close,gridBagCon);
    myPanelDown.add(close);
    }

    public void runClient()
    {
    try
    {
    final Socket s=new Socket(InetAddress.getByName("localhost"),6000);//设置服务端口号
    final OutputStream os=s.getOutputStream();
    final InputStream is=s.getInputStream();

    send.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
      String str=serverMessage.getText();
      System.out.println(str);
      os.write(str.getBytes());

    System.out.println("Success");

    }    catch(IOException ioe){}
    }
    });
    os.write("This is Client".getBytes()); //os.write("This is Server".getBytes());//参数是字节流

    //byte[] buf=new byte[100];
    //int length=is.read(buf);
    //System.out.println(new String(buf,0,length));

    byte[] buf=new byte[1024];
    int length=is.read(buf);
    messageDisplay.append(new String(buf,0,length));
    os.close();
    is.close();
    s.close();
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    }

    public static void main(String[] args)

      Client c=new Client();
    final JFrame f=new JFrame("Client");
    c.init();
    c.runClient();
    f.setLayout(new BorderLayout());
    c.messageDisplay.setEditable(false);
    f.getContentPane().add(c.messageDisplay,BorderLayout.NORTH);
    f.getContentPane().add(c.myPanelCenter,BorderLayout.CENTER);
    f.getContentPane().add(c.myPanelDown,BorderLayout.SOUTH);
        f.setSize(400,300);
        f.setResizable(false);
        f.pack();
        f.show();
        
        c.close.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    f.dispose();
    System.exit(0);
    }
    });

        f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
     }
    }编译通过,但是不能互发消息阿~~求救各位高手~~~~
      

  3.   

    监听器都是多线程的,所以你两个程序都过早关闭Socket s 了最好用传参数的方法新建一个监听器类,这样就可以去掉那些完全没必要的“final”修饰了
    close()方法放进finally块里面,不要直接接在加载监听器的后面
      

  4.   

    错了,错了,你这个根本不是网络编程的路子,简易LZ烟酒烟酒java.net包的API