×

Discussion Board

Results 1 to 2 of 2

Hybrid View

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    34

    socket read hangs until socket is closed

    I'm sure someone has run into this but I don't know what is wrong!

    I have a simple piece of code that implements an HTTP server. It runs on other machines (PC's) fine and MOSTLY runs on the Nokia 9500 but hangs on a 'read' statement until I stop the client browser.

    The code is as follows

    ...<snipped for brevity>

    Socket connection = server.accept();
    System.out.println("Connection accepted");
    System.out.println(request.getInetAddress());
    System.out.println("Got new Process Request");
    }
    try {
    String filename;
    String contentType;
    OutputStream raw = new BufferedOutputStream(connection
    .getOutputStream());
    Writer out = new OutputStreamWriter(raw);
    Reader in = new InputStreamReader(new BufferedInputStream(
    connection.getInputStream()));
    StringBuffer requestLine = new StringBuffer();
    int c;
    System.out.println(1);
    while (true) {
    c = in.read();
    System.out.println("Char Read: " + c + " : " + (char) c);
    if (c == '\r' || c == '\n')
    break;
    requestLine.append((char) c);
    }
    String get = requestLine.toString();
    // log the request
    System.out.println(get);

    <snip>

    On the PC I get the output

    Connection accepted
    192.168.1.103/192.168.1.103
    Got new Process Request
    1
    Char Read: 71 : G
    Char Read: 69 : E
    Char Read: 84 : T
    Char Read: 32 :
    Char Read: 47 : /
    Char Read: 105 : i
    Char Read: 110 : n
    Char Read: 100 : d
    Char Read: 101 : e
    Char Read: 120 : x
    Char Read: 46 : .
    Char Read: 104 : h
    Char Read: 116 : t
    Char Read: 109 : m
    Char Read: 108 : l
    Char Read: 32 :
    Char Read: 72 : H
    Char Read: 84 : T
    Char Read: 84 : T
    Char Read: 80 : P
    Char Read: 47 : /
    Char Read: 49 : 1
    Char Read: 46 : .
    Char Read: 49 : 1
    Char Read: 13 :

    GET /index.html HTTP/1.1

    On the Nokia I get


    Connection accepted
    192.168.1.103/192.168.1.103
    Got new Process Request
    1

    ..... and that's it!

    I put some more messages in the while (true) loop and it hangs on the read! Anyone have any ideas as to what I'm doing wrong. As soon as I close the browser connection on the client side, the nokia log file matches the PC log, so it seems to be waiting until the connection is closed before returning from the read for some reason. I've tried IE, and Mozialla and Netscape and the results are consistant between those 3 so I don't think it's the client side. Any ideas would be greatly appreciated.

    Cheers

    Cam

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    34
    In general, socket reading hangs until data is available.

    So be sure you have some data available. Study the HTTP protocol a bit more carefully.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×