开发者

how to use readline without and EOF character java

开发者 https://www.devze.com 2023-02-21 18:53 出处:网络
i am trying to implement a simple server application in java. all it does is read in a message on the tcp/ip and stores it as a string this is my code.

i am trying to implement a simple server application in java.

all it does is read in a message on the tcp/ip and stores it as a string this is my code.

    try{
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    } catch (IOException e) {
        System.out.println("cannot open input buffer");
        System.exit(-1);
    }

    clientSocket.setSoTimeout(5000);

    //read first bit of message
    message = in.readLine();
    System.out.println(message);
    //as message is an undefined length we need to loop and check for the springer miller 
    //end mark /Request
    while(message.contains("/Request") == false  )
    {
        try {
        message = in.readLine();
        System.out.println(message);
        }
        catch (IOException e) {
            System.out.println("cannot open input buffer");
            System.exit(-1);
        }
    }       

    //reply
    out.println(outputLine);

the problem i am having is that the message does not appear to have an EOF. it is another companies protocol i am translating into mine, thats the purpose of the program so i cannot add a EOF to the message

the information a get if i run the program is:

POST / HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: http://htng.org/1.1/Listener.Wsdl#ReceiveMessageAsync
User-Agent: Java/1.6.0_24
Host: 192.168.0.32:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 3009

then it hangs when it should read the message body.

i have never used java in my life before and do not want to write a binary socket readed to detect my own EOF.

is there a way to read for x seconds and then return

thank you for any help.

P.S have a开发者_StackOverflow中文版lready successfully built the program in C++ but need to port in to java because destined machine is unknown.


BufferedReader.readline will return null on EOF and not throw an exception.

Moreover, the "other companies protocol" seems to be SOAP over HTTP. Maybe you want to use a HTTP or SOAP library? Others here will be able to give pointers...

Otherwise you can use the following approach: readLine once to get check if the method is indeed POST (otherwise the Content-Length header might not be there) and the path is correct.
readLine until it either returns an empty line (or null), to read all the HTTP headers. While doing that look out for a line starting with Content-Length, to determine the length of the following XML data. create a char[] of the correct length and use in.read(cbuf, 0, cbuf.length) to read the xml into the created buffer cbuf.


Implementing protocols on top of TCP/IP is tricky, and requires quite a lot of understanding of how networking, sockets and your OS:s I/O work.

Further, implementing HTTP is surprisingly complex - on top of the network complexity.

I'm politely suggesting that you probably are in deep water, as you have to ask questions at this level, and probably need more help than you can get on SO.

...anyway.

If the server you are reading from is trying to talk http, use an existing component for it. Apache HttpComponents if probably a good choice. I don't really buy that it forges http headers, and I suggest that you skip your "lightweight" approach.

Here is some network-i/o basic facts.

Network writes are packet oriented. Tcp/ip generally tries to stuff as much as possible into every packet (using some smart algorithms). That means that if you write 4000 bytes, the message is split up into several packets that are arbitrarily sized, but normally less than 1500 byte - depending on the network equipment. It also means that if you write less than a packet, your writes may be merged into one packet. (Packets may also be split and merged along they way.)

In order to send messages over the stream ( which itself is transported in packets...) you need to know in advance how long the messages are, or, read a full packet (do a .read() into a large buffer), parse the contents, and extract and contruct complete messages in some smart way. Exactly what http does. (among things)

TCP/IP is certainly NOT line-oriented, so your newlines are totally ignored. HTTP uses the content-length (and some other tricks, as it may not always be defined) to send "messages" over a single tcp/ip stream, that may or may not be closed when a message is completely sent.

0

精彩评论

暂无评论...
验证码 换一张
取 消