_socket = new Socket (host, port);
            System.out.println(new BufferedReader(new InputStreamReader(_socket.getInputStream())).readLine());
就最后一句出错了

解决方案 »

  1.   

    是对方关闭了连接,看看socket server的代码如何实现的
      

  2.   

    /**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    import java.net.Socket;
    import java.io.*;public class MSNConnection {
    private long            _transactionID = 0;
    private Socket        _socket;
    //private NetworkStream   _stream;
    private BufferedReader    _reader;
    private BufferedWriter   _writer;    public MSNConnection() {
        }    public void connect (String host,int port){
            _transactionID = 0;
            try {
                System.out.println("host+port=" + host + port);
                _socket = new Socket(host, port);
                System.out.println("==encoding==="+new InputStreamReader(_socket.getInputStream(),"ascii").getEncoding());
                _reader = new BufferedReader(new InputStreamReader(_socket.
                        getInputStream(),"ascii"));
                _writer = new BufferedWriter(new OutputStreamWriter(_socket.
                        getOutputStream(),"ascii"));
                System.out.println("connect finish!!!");
            } catch (Exception e) {
                System.err.println("Err - " + e);
            }
        }    public void writeCommand(String command, String parameters, boolean bSendId) {
            String line;
            // check what type of format it should be
            if (bSendId){
                line = String.format("{0} {1} {2}", command, _transactionID,
                                     parameters);
            }else{
                line = String.format("{0} {1}", command, parameters);
            }
            // Write the line
            writeLine(line, true);
        }    public void writeLine(String line, boolean writeNewLine) {
            System.out.println("Writing: " + line);
            //if (writeNewLine)
                try {
                    _writer.write(line);
                    System.out.println(_reader.readLine());
                } catch (IOException ex) {
                    System.out.println("erro="+ex.getMessage());
                    System.out.println("erro="+ex.toString());
                }
            //else
                //_writer.write(line);
            // raise the transactionId
            _transactionID++;    }    public ServerCommand readCommand(){
            String line = null;
            try {
                //System.out.println("redLine="+_reader.readLine());
                //System.out.println("read="+_reader.read());
                line = _reader.readLine();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
            System.out.println("Reading: " + line);
            if (line == null) {
                System.out.println("Nothing received");
                //return "";
                return new ServerCommand();
            } else {
                //return "";
                return new ServerCommand(line);
            }
        }
        
        public void dispose() {
            if (_socket != null) {
                try {
                    _reader.close();
                    _writer.close();
                    //_stream.Close();
                    _socket.close();
                } catch (IOException ex) {
                }
                _socket = null;
            }
        }
    }
      

  3.   

    上面是我的代码,帮我看看.不知道为什么readLine老是出错
      

  4.   

    你是Server是怎么接的?
    这个看不出有什么问题.
    听听楼下的看法
      

  5.   

    不好意思,写错。
    按字节来取。不要取行,因为如果readline的话,必定要求对方发送的数据中带有\r或者\n