服务器端用java线程写Socket 备注:其他无关代码不发了,主要部分发下 System.out.println("Socket["+this.server_id+"] connection success."); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( this.server_socket.getOutputStream())),true); out.println("Socket["+this.server_id+"] welcome client"); BufferedReader in = new BufferedReader( new InputStreamReader( this.server_socket.getInputStream()));
String ss; ss = in.readLine(); out.println("test_outputs");
下面是j2se文档资料 int java.io.InputStream.read() Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
Returns: the next byte of data, or -1 if the end of the stream is reached. Throws: IOException if an I/O error occurs.
这句话非常重要 Returns: the next byte of data, or -1 if the end of the stream is reached.
当没有发送end of the stream 标识的话,那么程序属于等待状态。 也就是java端一直处于读的状态。 解决这个问题的话。 要么终端手动发送eof.要么终端发送每一次交互的字节长。服务器端根据这个字节长来读取相应长度的字节。