Deadlock situation while reading from sockets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • javastudent

    Deadlock situation while reading from sockets

    Hi,

    I am implementing a server that reads for socket connections at a
    port, and processes the socket on a separate thread. We do not control
    the client implementation. Here is the scenario.

    0. Client connects to the socket.
    1. Client writes to the socket.
    2. Server reads from the socket.
    3. Client waits for the response on the socket
    4. Server writes to the socket.
    5. Socket is closed.

    Incoming request on the socket is not guaranteed to be terminated with
    any particular sequence of characters. How does the server know how
    many bytes to read, and when to stop reading?

    Here are couple of things I tried.

    1. readln on a buffered reader on the server side would block till it
    receives the "eol" character.
    2. I have tried reading x number of characters at a time, and thought
    if number of characters read are less than the attempted number of
    characters to read (or the read method returns -1) then read all the
    data sent by the client. This works if the attempted characters to
    read initially, are greater than the number of characters sent by the
    client, but not if it is the other way.

    I have used BufferedReader, and the PrintWriter classes.

    I appreciate your help in resolving this issue.

    Thanks.
  • Chris Rohr

    #2
    Re: Deadlock situation while reading from sockets

    When the client connects to the socket it only writes the one time then
    waits, so you can use the BufferedReader to read all of the data on the
    socket. Each communication from the client has a new socket, so you
    don't have to maintain a connection and wonder how much data is being
    sent. When the socket is flushed to the server, it will append the eof
    to the stream. Hopes this helps at all.

    -C

    javastudent wrote:[color=blue]
    > Hi,
    >
    > I am implementing a server that reads for socket connections at a
    > port, and processes the socket on a separate thread. We do not control
    > the client implementation. Here is the scenario.
    >
    > 0. Client connects to the socket.
    > 1. Client writes to the socket.
    > 2. Server reads from the socket.
    > 3. Client waits for the response on the socket
    > 4. Server writes to the socket.
    > 5. Socket is closed.
    >
    > Incoming request on the socket is not guaranteed to be terminated with
    > any particular sequence of characters. How does the server know how
    > many bytes to read, and when to stop reading?
    >
    > Here are couple of things I tried.
    >
    > 1. readln on a buffered reader on the server side would block till it
    > receives the "eol" character.
    > 2. I have tried reading x number of characters at a time, and thought
    > if number of characters read are less than the attempted number of
    > characters to read (or the read method returns -1) then read all the
    > data sent by the client. This works if the attempted characters to
    > read initially, are greater than the number of characters sent by the
    > client, but not if it is the other way.
    >
    > I have used BufferedReader, and the PrintWriter classes.
    >
    > I appreciate your help in resolving this issue.
    >
    > Thanks.[/color]

    Comment

    • javastudent

      #3
      Re: Deadlock situation while reading from sockets

      Chris Rohr <cjrohr@verizon .net> wrote in message news:<OAZOc.516 3$Je5.4308@nwrd dc03.gnilink.ne t>...[color=blue]
      > When the client connects to the socket it only writes the one time then
      > waits, so you can use the BufferedReader to read all of the data on the
      > socket. Each communication from the client has a new socket, so you
      > don't have to maintain a connection and wonder how much data is being
      > sent. When the socket is flushed to the server, it will append the eof
      > to the stream. Hopes this helps at all.[/color]

      Thanks for your reply. I really appreciate it.

      What method in the BufferedReader class can I use to read all the data
      on the socket? Here are the methods I looked at.

      1. readLine(): Looks like this method waits till it receives an end of
      line, or the socket connection is closed.
      2. read(char[] cbuf, int off, int len): Reads only specified number of
      bytes, so it works as long as we specify at least as many bytes as
      the client sent.

      Thank you.

      [color=blue]
      >
      > -C
      >
      > javastudent wrote:[color=green]
      > > Hi,
      > >
      > > I am implementing a server that reads for socket connections at a
      > > port, and processes the socket on a separate thread. We do not control
      > > the client implementation. Here is the scenario.
      > >
      > > 0. Client connects to the socket.
      > > 1. Client writes to the socket.
      > > 2. Server reads from the socket.
      > > 3. Client waits for the response on the socket
      > > 4. Server writes to the socket.
      > > 5. Socket is closed.
      > >
      > > Incoming request on the socket is not guaranteed to be terminated with
      > > any particular sequence of characters. How does the server know how
      > > many bytes to read, and when to stop reading?
      > >
      > > Here are couple of things I tried.
      > >
      > > 1. readln on a buffered reader on the server side would block till it
      > > receives the "eol" character.
      > > 2. I have tried reading x number of characters at a time, and thought
      > > if number of characters read are less than the attempted number of
      > > characters to read (or the read method returns -1) then read all the
      > > data sent by the client. This works if the attempted characters to
      > > read initially, are greater than the number of characters sent by the
      > > client, but not if it is the other way.
      > >
      > > I have used BufferedReader, and the PrintWriter classes.
      > >
      > > I appreciate your help in resolving this issue.
      > >
      > > Thanks.[/color][/color]

      Comment

      • IchBin

        #4
        Re: Deadlock situation while reading from sockets

        neelima1@comcas t.net (javastudent) wrote in
        news:d4358d61.0 408011427.7763e 290@posting.goo gle.com:
        [color=blue]
        > Chris Rohr <cjrohr@verizon .net> wrote in message
        > news:<OAZOc.516 3$Je5.4308@nwrd dc03.gnilink.ne t>...[color=green]
        >> When the client connects to the socket it only writes the one time
        >> then waits, so you can use the BufferedReader to read all of the data
        >> on the socket. Each communication from the client has a new socket,
        >> so you don't have to maintain a connection and wonder how much data
        >> is being sent. When the socket is flushed to the server, it will
        >> append the eof to the stream. Hopes this helps at all.[/color]
        >
        > Thanks for your reply. I really appreciate it.
        >
        > What method in the BufferedReader class can I use to read all the data
        > on the socket? Here are the methods I looked at.
        >
        > 1. readLine(): Looks like this method waits till it receives an end of
        > line, or the socket connection is closed.
        > 2. read(char[] cbuf, int off, int len): Reads only specified number of
        > bytes, so it works as long as we specify at least as many bytes as
        > the client sent.
        >
        > Thank you.[/color]


        This is what I did to request HTML data back:

        Socket socket = new Socket(URL, HTTP_PORT);
        BufferedWriter out = new BufferedWriter( new OutputStreamWri ter(socket
        .getOutputStrea m()));

        BufferedReader in = new BufferedReader( new InputStreamRead er(socket
        .getInputStream ()));
        out.write(outTa rgetrequest +" HTTP/1.0\n\n");
        out.flush();

        String line;
        while ((line = in.readLine()) != null) {
        System.out.prin tln(line);
        }
        out.close();
        in.close();

        --
        Thanks in Advance...
        IchBin
        _______________ _______________ _______________

        'Black holes are where God divided by zero.'
        -Steven Wright, comedian (1955- )

        Comment

        • Nate Smith

          #5
          Re: Deadlock situation while reading from sockets

          IchBin wrote:
          [color=blue]
          > while ((line = in.readLine()) != null) {
          > System.out.prin tln(line);[/color]


          call me dumb, but isnt this Syste.out different from
          your buffered output stream? e.g. should be just
          [color=blue]
          > out.println(lin e);[/color]


          [color=blue]
          > }
          > out.close();
          > in.close();
          >[/color]


          i am still trying to cope with all the magic hat tricks
          java is unloading on newbie coders...


          - nate

          Comment

          • IchBin

            #6
            Re: Deadlock situation while reading from sockets

            Yes, you are right.

            This is not my final code. I had never done this before and wanted to test
            the see the input right to the console. I now have other components using
            this data, in my application. You have to start somewhere. Look up the
            System.out.prin tln() as opposed to my instantiated BufferedWriter out. I am
            witting out to a different place that what I Buffered in.on... LOL...

            Sorry it's late for me.

            --
            Thanks in Advance...
            IchBin
            _______________ _______________ _______________

            'Black holes are where God divided by zero.'
            -Steven Wright, comedian (1955- )


            "Nate Smith" <greystone@NET1 Plus.com> wrote in message
            news:u4-dnYDiWfqHKpDcRV n-rA@net1plus.com ...[color=blue]
            > IchBin wrote:
            >[color=green]
            > > while ((line = in.readLine()) != null) {
            > > System.out.prin tln(line);[/color]
            >
            >
            > call me dumb, but isnt this Syste.out different from
            > your buffered output stream? e.g. should be just
            >[color=green]
            > > out.println(lin e);[/color]
            >
            >
            >[color=green]
            > > }
            > > out.close();
            > > in.close();
            > >[/color]
            >
            >
            > i am still trying to cope with all the magic hat tricks
            > java is unloading on newbie coders...
            >
            >
            > - nate
            >[/color]


            Comment

            • javastudent

              #7
              Re: Deadlock situation while reading from sockets

              IchBin <weconsul@ptd.n et> wrote in message news:<Xns9538EC A5DA6AAIchBin@2 04.186.200.105> ...[color=blue]
              > neelima1@comcas t.net (javastudent) wrote in
              > news:d4358d61.0 408011427.7763e 290@posting.goo gle.com:
              >[color=green]
              > > Chris Rohr <cjrohr@verizon .net> wrote in message
              > > news:<OAZOc.516 3$Je5.4308@nwrd dc03.gnilink.ne t>...[color=darkred]
              > >> When the client connects to the socket it only writes the one time
              > >> then waits, so you can use the BufferedReader to read all of the data
              > >> on the socket. Each communication from the client has a new socket,
              > >> so you don't have to maintain a connection and wonder how much data
              > >> is being sent. When the socket is flushed to the server, it will
              > >> append the eof to the stream. Hopes this helps at all.[/color]
              > >
              > > Thanks for your reply. I really appreciate it.
              > >
              > > What method in the BufferedReader class can I use to read all the data
              > > on the socket? Here are the methods I looked at.
              > >
              > > 1. readLine(): Looks like this method waits till it receives an end of
              > > line, or the socket connection is closed.
              > > 2. read(char[] cbuf, int off, int len): Reads only specified number of
              > > bytes, so it works as long as we specify at least as many bytes as
              > > the client sent.
              > >
              > > Thank you.[/color]
              >
              >
              > This is what I did to request HTML data back:
              >
              > Socket socket = new Socket(URL, HTTP_PORT);
              > BufferedWriter out = new BufferedWriter( new OutputStreamWri ter(socket
              > .getOutputStrea m()));
              >
              > BufferedReader in = new BufferedReader( new InputStreamRead er(socket
              > .getInputStream ()));
              > out.write(outTa rgetrequest +" HTTP/1.0\n\n");
              > out.flush();
              >
              > String line;
              > while ((line = in.readLine()) != null) {
              > System.out.prin tln(line);
              > }
              > out.close();
              > in.close();[/color]


              You are relying on your clients terminating the string with "\n",
              hence your readLine works. My clients wouldn't be ending the string
              with "\n". However, I have just found out that we have a gate way that
              terminates the string with '\004', so how do I use BufferedReader to
              read until I find a termination character?

              Thanks.

              Comment

              • IchBin

                #8
                Re: Deadlock situation while reading from sockets

                neelima1@comcas t.net (javastudent) wrote in
                news:d4358d61.0 408021032.766ac c4d@posting.goo gle.com:
                [color=blue]
                > IchBin <weconsul@ptd.n et> wrote in message
                > news:<Xns9538EC A5DA6AAIchBin@2 04.186.200.105> ...[color=green]
                >> neelima1@comcas t.net (javastudent) wrote in
                >> news:d4358d61.0 408011427.7763e 290@posting.goo gle.com:
                >>[color=darkred]
                >> > Chris Rohr <cjrohr@verizon .net> wrote in message
                >> > news:<OAZOc.516 3$Je5.4308@nwrd dc03.gnilink.ne t>...[/color][/color][/color]
                [color=blue]
                > You are relying on your clients terminating the string with "\n",
                > hence your readLine works. My clients wouldn't be ending the string
                > with "\n". However, I have just found out that we have a gate way that
                > terminates the string with '\004', so how do I use BufferedReader to
                > read until I find a termination character?
                >
                > Thanks.
                >[/color]

                Right off the top, not sure. Look at the InputStreamRead er,
                OutputStreamWri ter for adding, changing or reconizing a termiation or
                custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
                Beta 2 API Specification), Online or download it. Maybe even socket?

                out.write(outTa rgetrequest +" terination char? )

                Another good place to post this question is:comp.lang.ja va.developer or
                comp.lang.java. programmer and maybe comp.lang.java. help. Don't cross post
                and maybe Roedy Green or Andrew Thompson could help you out.

                Sorry for now...
                --
                Thanks in Advance...
                IchBin
                _______________ _______________ _______________

                'Black holes are where God divided by zero.'
                -Steven Wright, comedian (1955- )

                Comment

                • IchBin

                  #9
                  Re: Deadlock situation while reading from sockets

                  Look at this documentation, I happen to bump into.. It's in the "Big Index"
                  at Sun. This page has most all common java information! Not as detailed
                  specifc as the java API specs. You should aways look here first.. anyway

                  Go here (http://java.sun.com/docs/books/tutor...ybigindex.html)

                  This link will take you to the instructions...
                  (http://java.sun.com/docs/books/tutor...ngWriting.html
                  )


                  Other good ones just in case... and should be your bible's, so to speak..
                  (just downlod the lastest SDK document zip.)

                  JavaTM 2 SDK, Standard Edition Documentation
                  JavaTM 2 Platform, Standard Edition, v 1.5.0 Beta 2 API Specification
                  Java Language Specification


                  --
                  Hope this helps.. I think it will
                  IchBin
                  _______________ _______________ _______________

                  'Black holes are where God divided by zero.'
                  -Steven Wright, comedian (1955- )
                  "IchBin" <weconsul@ptd.n et> wrote in message
                  news:Xns95399C5 7E8AF9IchBin@20 4.186.200.105.. .[color=blue]
                  > neelima1@comcas t.net (javastudent) wrote in
                  > news:d4358d61.0 408021032.766ac c4d@posting.goo gle.com:
                  >[color=green]
                  > > IchBin <weconsul@ptd.n et> wrote in message
                  > > news:<Xns9538EC A5DA6AAIchBin@2 04.186.200.105> ...[color=darkred]
                  > >> neelima1@comcas t.net (javastudent) wrote in
                  > >> news:d4358d61.0 408011427.7763e 290@posting.goo gle.com:
                  > >>
                  > >> > Chris Rohr <cjrohr@verizon .net> wrote in message
                  > >> > news:<OAZOc.516 3$Je5.4308@nwrd dc03.gnilink.ne t>...[/color][/color]
                  >[color=green]
                  > > You are relying on your clients terminating the string with "\n",
                  > > hence your readLine works. My clients wouldn't be ending the string
                  > > with "\n". However, I have just found out that we have a gate way that
                  > > terminates the string with '\004', so how do I use BufferedReader to
                  > > read until I find a termination character?
                  > >
                  > > Thanks.
                  > >[/color]
                  >
                  > Right off the top, not sure. Look at the InputStreamRead er,
                  > OutputStreamWri ter for adding, changing or reconizing a termiation or
                  > custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
                  > Beta 2 API Specification), Online or download it. Maybe even socket?
                  >
                  > out.write(outTa rgetrequest +" terination char? )
                  >
                  > Another good place to post this question is:comp.lang.ja va.developer or
                  > comp.lang.java. programmer and maybe comp.lang.java. help. Don't cross post
                  > and maybe Roedy Green or Andrew Thompson could help you out.
                  >
                  > Sorry for now...
                  > --
                  > Thanks in Advance...
                  > IchBin
                  > _______________ _______________ _______________
                  >
                  > 'Black holes are where God divided by zero.'
                  > -Steven Wright, comedian (1955- )[/color]


                  Comment

                  • javastudent

                    #10
                    Re: Deadlock situation while reading from sockets

                    "IchBin" <weconsultants@ pdt.net> wrote in message news:<YGudnSEAZ-gXqJLcUSdV9g@pt d.net>...[color=blue]
                    > Look at this documentation, I happen to bump into.. It's in the "Big Index"
                    > at Sun. This page has most all common java information! Not as detailed
                    > specifc as the java API specs. You should aways look here first.. anyway
                    >
                    > Go here (http://java.sun.com/docs/books/tutor...ybigindex.html)
                    >
                    > This link will take you to the instructions...
                    > (http://java.sun.com/docs/books/tutor...ngWriting.html
                    > )
                    >
                    >
                    > Other good ones just in case... and should be your bible's, so to speak..
                    > (just downlod the lastest SDK document zip.)
                    >
                    > JavaTM 2 SDK, Standard Edition Documentation
                    > JavaTM 2 Platform, Standard Edition, v 1.5.0 Beta 2 API Specification
                    > Java Language Specification
                    >
                    >
                    > --
                    > Hope this helps.. I think it will
                    > IchBin[/color]


                    Yes, it did. Thanks for taking time.

                    Regards.[color=blue]
                    > _______________ _______________ _______________
                    >
                    > 'Black holes are where God divided by zero.'
                    > -Steven Wright, comedian (1955- )
                    > "IchBin" <weconsul@ptd.n et> wrote in message
                    > news:Xns95399C5 7E8AF9IchBin@20 4.186.200.105.. .[color=green]
                    > > neelima1@comcas t.net (javastudent) wrote in
                    > > news:d4358d61.0 408021032.766ac c4d@posting.goo gle.com:
                    > >[color=darkred]
                    > > > IchBin <weconsul@ptd.n et> wrote in message
                    > > > news:<Xns9538EC A5DA6AAIchBin@2 04.186.200.105> ...
                    > > >> neelima1@comcas t.net (javastudent) wrote in
                    > > >> news:d4358d61.0 408011427.7763e 290@posting.goo gle.com:
                    > > >>
                    > > >> > Chris Rohr <cjrohr@verizon .net> wrote in message
                    > > >> > news:<OAZOc.516 3$Je5.4308@nwrd dc03.gnilink.ne t>...[/color][/color]
                    >[color=green][color=darkred]
                    > > > You are relying on your clients terminating the string with "\n",
                    > > > hence your readLine works. My clients wouldn't be ending the string
                    > > > with "\n". However, I have just found out that we have a gate way that
                    > > > terminates the string with '\004', so how do I use BufferedReader to
                    > > > read until I find a termination character?
                    > > >
                    > > > Thanks.
                    > > >[/color]
                    > >
                    > > Right off the top, not sure. Look at the InputStreamRead er,
                    > > OutputStreamWri ter for adding, changing or reconizing a termiation or
                    > > custom termination char, via (Java 2 Platform, Standard Edition, v 1.5.0
                    > > Beta 2 API Specification), Online or download it. Maybe even socket?
                    > >
                    > > out.write(outTa rgetrequest +" terination char? )
                    > >
                    > > Another good place to post this question is:comp.lang.ja va.developer or
                    > > comp.lang.java. programmer and maybe comp.lang.java. help. Don't cross post
                    > > and maybe Roedy Green or Andrew Thompson could help you out.
                    > >
                    > > Sorry for now...
                    > > --
                    > > Thanks in Advance...
                    > > IchBin
                    > > _______________ _______________ _______________
                    > >
                    > > 'Black holes are where God divided by zero.'
                    > > -Steven Wright, comedian (1955- )[/color][/color]

                    Comment

                    Working...