Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ail.comSocket not blocking

ail.comSocket not blocking

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shenthil
    wrote on last edited by
    #1

    Hi, I've created a Socket (TCP/IP - Synchronous Socket - SOCK_STREAM) program in which I've spawned a new thread to accept client connections. Again, for every accepted connection through a new client socket, i've spawned another new thread for message transaction. Here, the Receive function does not block until the data is filled in the buffer. Instead, the receive function tries to read data which is either NULL or junk, which leads to assertion failure. Is there any solution to make the client socket block untill receive? Or does a thread affect the state of the socket by any chance. shenthil senthilkkumar@rediffmail.com

    P G 2 Replies Last reply
    0
    • S Shenthil

      Hi, I've created a Socket (TCP/IP - Synchronous Socket - SOCK_STREAM) program in which I've spawned a new thread to accept client connections. Again, for every accepted connection through a new client socket, i've spawned another new thread for message transaction. Here, the Receive function does not block until the data is filled in the buffer. Instead, the receive function tries to read data which is either NULL or junk, which leads to assertion failure. Is there any solution to make the client socket block untill receive? Or does a thread affect the state of the socket by any chance. shenthil senthilkkumar@rediffmail.com

      P Offline
      P Offline
      Peter Molnar
      wrote on last edited by
      #2

      The receive function of a connected socket is very much blocking provided you use CSocket::Receive or the Winsock API' recv (and not CAsyncSocket). The reasons for a receive to not block might be: 1.The passed in data buffer is full with data 2.The connection went lost So, check for the above. Peter Molnar

      1 Reply Last reply
      0
      • S Shenthil

        Hi, I've created a Socket (TCP/IP - Synchronous Socket - SOCK_STREAM) program in which I've spawned a new thread to accept client connections. Again, for every accepted connection through a new client socket, i've spawned another new thread for message transaction. Here, the Receive function does not block until the data is filled in the buffer. Instead, the receive function tries to read data which is either NULL or junk, which leads to assertion failure. Is there any solution to make the client socket block untill receive? Or does a thread affect the state of the socket by any chance. shenthil senthilkkumar@rediffmail.com

        G Offline
        G Offline
        Gabriel P G
        wrote on last edited by
        #3

        I have never had problems with blocking sockets and threads. Anyway, I assume you are just using plain socket, accept and recv. If then the only thing that comes to mind is that you are mistaken the length of the buffer you are passing (the buffer you expect filled ) with the actual number of bytes read. if you try something like this do you get to DebugBreak()? int received = recv( s, buf, sizeof buf, 0 ); if( ( SOCKET_ERROR == received ) && (WSAEWOULDBLOCK == WSAGetLastError() ) ) { //something has changed your socket to non-blocking mode DebugBreak(); } else { CopyMemory( workerBuf, buf, received ); //work with worker buf using the n received bytes } Hope this helps Gabriel Old C programmers never die. They just cast into void

        S 1 Reply Last reply
        0
        • G Gabriel P G

          I have never had problems with blocking sockets and threads. Anyway, I assume you are just using plain socket, accept and recv. If then the only thing that comes to mind is that you are mistaken the length of the buffer you are passing (the buffer you expect filled ) with the actual number of bytes read. if you try something like this do you get to DebugBreak()? int received = recv( s, buf, sizeof buf, 0 ); if( ( SOCKET_ERROR == received ) && (WSAEWOULDBLOCK == WSAGetLastError() ) ) { //something has changed your socket to non-blocking mode DebugBreak(); } else { CopyMemory( workerBuf, buf, received ); //work with worker buf using the n received bytes } Hope this helps Gabriel Old C programmers never die. They just cast into void

          S Offline
          S Offline
          Shenthil
          wrote on last edited by
          #4

          Gabriel, receive function (CSocket::Receive) neither returns SOCKET_ERROR nor WASEWOULDBLOCK ), rather it simply crashes out. even while debuggin, when the receive function returns, the application crashes. shenthil shenthil

          G 1 Reply Last reply
          0
          • S Shenthil

            Gabriel, receive function (CSocket::Receive) neither returns SOCKET_ERROR nor WASEWOULDBLOCK ), rather it simply crashes out. even while debuggin, when the receive function returns, the application crashes. shenthil shenthil

            G Offline
            G Offline
            Gabriel P G
            wrote on last edited by
            #5

            Shenthil, I would like to help but i just know about plain sockets. I don´t have any idea about how they work in MFC, even less what the problem could be. About the subject of the original post (the socket not bloking,) CSocket inherits from CAsyncSocket which is asynchronous, and taking a quick glance at the code you can see CSocket simulates a blocking socket using a loop. But i don´t think this is the cause of the problem. If your threads are just pumping bytes from a network connection and you need to work directly with them you are better off using and alternative to CSocket, because there is a lot of overhead in the way they work (asyncrhonous notifications through a windows message queue) which was the only option when they appeared in win3.1 because the apps were single threaded and there was no real multitasking in the OS Gabriel Old C programmers never die. They just cast into void

            S 1 Reply Last reply
            0
            • G Gabriel P G

              Shenthil, I would like to help but i just know about plain sockets. I don´t have any idea about how they work in MFC, even less what the problem could be. About the subject of the original post (the socket not bloking,) CSocket inherits from CAsyncSocket which is asynchronous, and taking a quick glance at the code you can see CSocket simulates a blocking socket using a loop. But i don´t think this is the cause of the problem. If your threads are just pumping bytes from a network connection and you need to work directly with them you are better off using and alternative to CSocket, because there is a lot of overhead in the way they work (asyncrhonous notifications through a windows message queue) which was the only option when they appeared in win3.1 because the apps were single threaded and there was no real multitasking in the OS Gabriel Old C programmers never die. They just cast into void

              S Offline
              S Offline
              Shenthil
              wrote on last edited by
              #6

              Gabriel, How to stop the Thread from Pumping Messages. Shenthil Shenthil

              G 1 Reply Last reply
              0
              • S Shenthil

                Gabriel, How to stop the Thread from Pumping Messages. Shenthil Shenthil

                G Offline
                G Offline
                Gabriel P G
                wrote on last edited by
                #7

                Shenthil, If you refer to stop pumping the messages that are sent to you over the network, unless you call Receive or recv, the data is going to stand still until your winsock implementation runs out of network buffer space. If you refer to message pumping from the window queue as Receive does, this is not desirable because this is intrinsic to the mechanism CSocket uses. Here at CP in the Internet & Networking section you have plain sockets programming tutorials and classes, and also MFC CSocket derived classes. (Nish has written great tutorials on sockets) I´m biased but IMHO CSocket misuses resources and adds uneeded complexity. Hope this helps Gabriel Old C programmers never die. They just cast into void

                S 1 Reply Last reply
                0
                • G Gabriel P G

                  Shenthil, If you refer to stop pumping the messages that are sent to you over the network, unless you call Receive or recv, the data is going to stand still until your winsock implementation runs out of network buffer space. If you refer to message pumping from the window queue as Receive does, this is not desirable because this is intrinsic to the mechanism CSocket uses. Here at CP in the Internet & Networking section you have plain sockets programming tutorials and classes, and also MFC CSocket derived classes. (Nish has written great tutorials on sockets) I´m biased but IMHO CSocket misuses resources and adds uneeded complexity. Hope this helps Gabriel Old C programmers never die. They just cast into void

                  S Offline
                  S Offline
                  Shenthil
                  wrote on last edited by
                  #8

                  Thanks Gabriel. Now I've changed the algorithm, but the problem is still unsolved. Shenthil

                  G 1 Reply Last reply
                  0
                  • S Shenthil

                    Thanks Gabriel. Now I've changed the algorithm, but the problem is still unsolved. Shenthil

                    G Offline
                    G Offline
                    Gabriel P G
                    wrote on last edited by
                    #9

                    Is your app still crashing? What´s the error? Access violation? Gabriel Old C programmers never die. They just cast into void

                    S 1 Reply Last reply
                    0
                    • G Gabriel P G

                      Is your app still crashing? What´s the error? Access violation? Gabriel Old C programmers never die. They just cast into void

                      S Offline
                      S Offline
                      Shenthil
                      wrote on last edited by
                      #10

                      I've just changed my application as a peer-to-peer one, instead of supporting multi-clients simultaneously. shenthil

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups