现在想用java写一个客户端,不停的向服务器(c开发的,unix下运行的)发送http请求包,当服务器收到这个请求包以后,会给我回一个消息,客户端接收此消息并计数。如此循环,请大侠们给我个例子 谢谢

解决方案 »

  1.   

    请求包的格式大概是    POST / HTTP/1.1                      Host: ip:port                      Connection:Keep-alive                      Content-Length:
    大侠给我个大概的框架就可以阿
      

  2.   

    这是连接一次的代码。因为不知道你怎么个“不停”法,所以没写这个,你自己搞定public class HttpTest {
        public static void main(String argv[]) throws Exception {
            final int HTTP_PORT = 80;        Socket socket = new Socket("192.168.11.11", HTTP_PORT);
            BufferedWriter out
                    = new BufferedWriter(new OutputStreamWriter(socket.
                    getOutputStream()));
            BufferedReader in
                    = new BufferedReader(new InputStreamReader(socket.
                    getInputStream()));
            out.write("POST / HTTP/1.1\n\n");
            out.flush();        String line;
            StringBuffer sb = new StringBuffer();
            while ((line = in.readLine()) != null) {
                sb.append(line+"\r\n");
            }
            out.close();
            in.close();
            System.out.println(sb.toString());
        }
    }