我记得\n才是换行符,\t好像是TAB键吧。
反正\t我试了还是不行的。

解决方案 »

  1.   

    有人可以解答一下吗,或者不用ReadLine方法,有没有别的读取数据的方法?
      

  2.   

    可以用
    ObjectInputStream ooi = new ObjectInputStream (netSocket.getInputStream())从套接字里面读ObjectOutpubStream oop = new ObjectInputStream(netSocket.getOutputStream())写到套接字里面
      

  3.   

    ObjectInputStream ooi = new ObjectInputStream (netSocket.getInputStream())Object obj = ooi.readObject();//从套接字里面读
    ObjectOutpubStream oop = new ObjectInputStream(netSocket.getOutputStream())oop.writeObject(obj);//写到套接字里面这里读来读去的全是对象
      

  4.   

    我以前也碰到你这样的问题,然后我就不用readLine了,而用read()就可以了.private  BufferedReader bfReader;bfReader = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
    char[] cBuf = new char[100];
    bfReader.read(cBuf);
      

  5.   

    受不了了,我加了flush(),也改用byte接收,可是还是出不来。我把代码贴出来给大家吧,请大家给我看看:
    Server端:
    import javax.swing.*;import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    /**
     * @author administrator
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Svr_frm extends JFrame{
    private JTextArea Txtaa = new JTextArea(4,20);
    private JTextField Txtfd = new JTextField(20);
    private JButton JBon = new JButton("发送");
    private ServerSocket Svrsoc = null;
    private PrintStream out=null; 

    public Svr_frm(){
    super("网络聊天实例");
    Container cont = getContentPane();
    cont.setLayout(new FlowLayout());
    JLabel lab1 = new JLabel("聊天记录:");
    JLabel lab2 = new JLabel("你的发言:");
    cont.add(lab1);
    cont.add(Txtaa);
    cont.add(lab2);
    cont.add(Txtfd);
    cont.add(JBon);
    setSize(280,260);
    setVisible(true);

    JBon.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
    out.println(Txtfd.getText()+"\n");
    out.flush();
    Txtaa.setText(Txtaa.getText()+"\n" +Txtfd.getText());
    Txtfd.setText("");
    }
    });
    try{
    Svrsoc = new ServerSocket(9494);
    int Num=0;
    while(true){
    Socket newSoc= Svrsoc.accept();
    out = new PrintStream( newSoc.getOutputStream( ) );
                BufferedReader in = new BufferedReader( new InputStreamReader( newSoc.getInputStream( ) ) );
                if((Num=in.read())!=0){
                 char[] cBuf = new char[Num];
                 in.read(cBuf);
                 String pass = new String(cBuf);
                 Txtaa.setText(Txtaa.getText()+pass);   
                }
    }
    }catch(IOException e){
    System.out.println(e.getMessage());
    }
    }

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

  6.   

    下面是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);
    }
    }还有一个窗口用来输入连接服务器地址:
    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 IPFrame extends JFrame{
    private String IPadd;
    private JTextField JTF = new JTextField(15);
    private JButton JBon1 = new JButton("确定");
    private JButton JBon2 =new JButton("取消");
    private SockFrm isPa;

    public IPFrame(){
    super("输入IP地址");
    Container Cont = getContentPane();
    Cont.setLayout(new FlowLayout());
    JLabel lab1 = new JLabel("服务器IP:");
    Cont.add(lab1);
    Cont.add(JTF);
    Cont.add(JBon1);
    Cont.add(JBon2);
    ButtonHandle handle = new ButtonHandle();
    JBon1.addActionListener(handle);
    JBon2.addActionListener(handle);
    setSize(300,100);
    setVisible(true);
    }

    public void setParent(SockFrm SF){
    isPa = SF;
    }

    private class ButtonHandle implements ActionListener{
    public void actionPerformed(ActionEvent event){
    if(event.getSource()==JBon1){
    IPadd = JTF.getText();
    dispose();
    isPa.ConServer(IPadd);
    }else if(event.getSource()==JBon2){
    dispose();
    }

    }
    }
    }