你在客户端没有处理好罢了。
下面是Client端:
import java.net.*;
import java.io.*;
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
/**
 * @author administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SockFrm  extends JFrame{
private JTextArea Txtaa = new JTextArea(4,20);
private JTextField Txtfd = new JTextField(20);
private JButton JBon = new JButton("发送");
private String IPstr;
private SockFrm SF;
private Socket soc=null;
private PrintStream out=null;
private BufferedReader in;
private BufferedReader stdIn;

public SockFrm(){
super("网络聊天实例");
Container cont = getContentPane();
cont.setLayout(new FlowLayout());
JLabel lab1 = new JLabel("聊天记录:");
JLabel lab2 = new JLabel("你的发言:");
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
JMenu FileMenu = new JMenu("文件");
JMenuItem ConSerMenu = new JMenuItem("连接服务器");
ConSerMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent Event){
IPFrame IPF = new IPFrame();
IPF.setParent(SF);
//IPF.setDefaultCloseOperation(IPFrame.EXIT_ON_CLOSE);
}
});
FileMenu.add(ConSerMenu);
bar.add(FileMenu);
cont.add(lab1);
cont.add(Txtaa);
cont.add(lab2);
cont.add(Txtfd);
cont.add(JBon);
setSize(280,260);
setVisible(true);
SF=this;
JBon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
out.println(Txtfd.getText()+"\n");
out.flush();
}
});
}

public void ConServer(String IP){
String inputLine;
IPstr=IP;
int Num=0;
//JOptionPane.showMessageDialog(null,IPstr,"系统提示",JOptionPane.INFORMATION_MESSAGE);
try{
soc = new Socket(IPstr,9494);
out = new PrintStream(soc.getOutputStream());
in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
//stdIn = new BufferedReader(new InputStreamReader(System.in));
while((Num=in.read())!=0){
char[] cBuf = new char[Num];
in.read(cBuf);
inputLine= new String(cBuf);
Txtaa.setText(Txtaa.getText()+inputLine);
}
}catch (IOException e){
System.out.println(e.getMessage());
}
}

public static void main(String args[]){
SockFrm SF= new SockFrm();
SF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

解决方案 »

  1.   

    用serverSocket建立连接就是了,网上这样资料很多的
      

  2.   

    这个是我之前写的client,不过是一次把三个arg都写进去
    我现在就想分开写
    就是一次交互之后写一个,要怎么改啊?
    import java.io.*;
    import java.net.*;public class Client {
      public static void main(String[] args) {
        try {
          if (args.length != 3) {
            System.out.println("need 3 arguments");
            System.exit(1);
          }
          String host = args[0];
          String seatno = args[1];
          String name=args[2];
          Socket socket = new Socket(host,8000);
         
          BufferedReader from_server =
            new BufferedReader(new InputStreamReader(socket.getInputStream()));
          PrintWriter to_server = new PrintWriter(socket.getOutputStream());
          
            to_server.println(seatno);
            to_server.println(name);
            to_server.flush();      String line;
          while ((line = from_server.readLine()) != null) {
            System.out.println(line);
          }
          
        }
        catch (Exception e) {    
          System.err.println(e);
        }
      }
    }
      

  3.   

    PrintWriter to_client= new PrintWriter(socket.getOutputStream(),true);
    PrintWriter to_server = new PrintWriter(socket.getOutputStream(),true);
      

  4.   

    EverythingMaster(众物之主) 我现在是因为限定了arg的数目,所以只能输入一次也就是我输入进去,然后等待回应。就完了怎么样实现我得到回应之后,我可以继续输入呢?
      

  5.   

    客户端的接收,发送要包在while(true) {}中,这样收到消息,才不会关闭socket连接。