nicovv
nicovv
2009-11-16 17:44
浏览 280
已采纳

socket 不能向服务端write 第二次

socket并无关闭,为何第二次write() 之后 无读到数据的?

import java.io.*;
import java.net.Socket;

public class MianTest {
public static void main(String args0[]) throws Exception {
Socket socket = new Socket("localhost", 80);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

    String header1 = "GET /hello.txt HTTP/1.1\r\nHost: localhost\n\r\n";
    String header2 = "GET /hi.txt HTTP/1.1\r\nHost: localhost\r\n\r\n";

    out.write(header1);

    out.flush();
    int i = 0;
    while ((i = in.read()) != -1) {
        System.out.print((char) i);
    }

            out.write(header2);
    out.flush();
    int c = 0;
    while ((c = in.read()) != -1) {
        System.out.print((char) c);

    }
         socket.close();
}

}
[b]问题补充:[/b]
to liuyu220

我是用socket 向tomcat通讯

服务端完成一次请求完后就关闭了inputstream,output数据回来 这很正常,
而第2次write(),其实有无去到服务端呢?还是服务端认为inputstream关闭了
之后来的数据不需要再读?

有无方法,不 new一个socket ,而发出第2次通讯

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • xxxxxxxxxxxxxxxxx
    xxxxxxxxxxxxxxxxx 2009-11-27 16:11
    已采纳

    [code="java"]int i = 0;
    while ((i = in.read()) != -1) {
    System.out.print((char) i);
    }[/code]

    尽管http 1.1 支持长连接, 但还是由上面的代码引起的,它被阻碍了。
    只有等待server side阻塞(如timeout),才会跳出循环。

    稍微简单修改一下。

    [code="java"]Socket socket = new Socket("localhost", 80);
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String header1 = "HEAD /  HTTP/1.1\r\nHost: $host\r\n\r\n";
        String header2 = "HEAD /  HTTP/1.1\r\nHost: $host\r\n\r\n";
    
        out.write(header1);
    
    
        out.flush();
    
        StringBuffer sb = new StringBuffer();
            while (true) {
                    char c = (char) in.read();
                    System.out.print(c);
                    if(c=='\r' || c=='\n') {
                        sb.append(c);
                    }
                    else {
                        sb.delete(0, sb.length());
                    }
                    if(sb.toString().equals("\r\n\r\n")) {
                        break;
                    }
            }
    
    
    
            out.write(header2);
    
    
            out.flush();
            sb.delete(0, sb.length());
                while (true) {
                        char c = (char) in.read();
                        System.out.print(c);
                        if(c=='\r' || c=='\n') {
                            sb.append(c);
                        }
                        else {
                            sb.delete(0, sb.length());
                        }
                        if(sb.toString().equals("\r\n\r\n")) {
                            break;
                        }
                }
    
            socket.close();
    

    [/code]

    点赞 评论
  • liuiyu220
    liuiyu220 2009-11-16 18:20

    你的serversocket的inputstream没有东西你当然读不出来。
    ps:你绑定80端口。不知道你要干嘛

    点赞 评论
  • yzx3323
    yzx3323 2009-11-17 11:42

    好像这么写会有问题。 你可以吧输入输出流关闭了,再次打开。

    点赞 评论
  • liuiyu220
    liuiyu220 2009-11-17 13:46

    客户端的inputstream,outputstream与服务端的inputstream,outputstream是两端通信的通道,通道关了就不能通信了。

    点赞 评论

相关推荐