D
D
Dmitry Richter2014-05-05 21:40:34
C++ / C#
Dmitry Richter, 2014-05-05 21:40:34

C++.Sockets.RECV function

here is my question before that:

I do everything as it should be, I open a socket on the client and the server .. it comes to the moment when the server application should listen to the client (I use the recv function for this), the problem is that I threw it into the timer and when it has to execute it, it hangs stupidly and I’m waiting for something to come, I’m racking my brains, I don’t know how to solve this problem correctly, I’m already tired of looking in Google.

I asked around, they say you can make a separate thread so that it doesn’t freeze, but I don’t understand this at all. I found the select function, I thought it might help, but nothing. Here is the code that is in the timer:
fd_set read_set, error_set;
         struct timeval tv = {0,0};
           tv.tv_sec = 1;
           tv.tv_usec = 0;
           FD_ZERO(&read_set);
           FD_ZERO(&error_set);
           FD_SET(sa, &read_set);
           FD_SET(sa, &error_set);
           if (select(sa + 1, &read_set, NULL, &error_set, &tv) == 1)
           {
 if (FD_ISSET(sa, &read_set))
             {
               bytesRecv = recv(sa, recvbuf, 50, 0);
               err = WSAGetLastError();// 10057 = A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) 
               if (bytesRecv == 0 || bytesRecv == WSAECONNRESET) {

                 textBox1->Text = "Connection Closed.\n";
                 WSACleanup();
               }
               else{

                 textBox1->Text = gcnew String(recvbuf);

               }
             };

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-05-05
@followthemoney

I found the select function, I thought it might help, but nothing

I wrote to you in the answer to that question about fcntl and O_NONBLOCK, I thought maybe you know how to read, but something nifiga. In Windows, their counterparts are ioctlsocket and FIONBIO.
Alternatively, try ioctlsocket and FIONREAD to read as many bytes as there are to read, not 50.

Q
qRoC, 2014-05-05
@qRoC

unsigned long mode = 1;
ioctlsocket( sa, FIONBIO, &mode );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question