Click to See Complete Forum and Search --> : Send images to many users


Cooker
June 11th, 2007, 10:33 PM
Hi,
I write a server-client architecture application that is used to send live video to logined users. I create a class derived from CAsyncSocket:

class CUdpSendSocket : public CAsyncSocket
{
// Attributes
public:
bool m_bReadyToSend;

// Operations
public:
CUdpSendSocket();
virtual ~CUdpSendSocket();
virtual bool Send(const void *lpBuf, int nBufLen);
virtual void OnSend(int nErrorCode);
};
I create this as UPD socket:
CUdpSendSocket m_UdpSocket;
m_UdpSocket.Create(VIDEO_PORT, SOCK_DGRAM)
I use for-loop to send live images to all logined user

// IP_List stores the IP of logined user(s).
// IP_Count records the number of logined user(s)
for (index = 0 ; index < IP_Count ; ++index)
{
m_UdpSocket.m_strClientAddress = (char *)IP_List[index];
m_UdpSocket.Send(Stream, StreamLength);
}
If the number of user is greater than 1, I will get an error code "10035" and only the first logined user can receive image. So I solve this problem b this:

int index;
bool ret;
for (index = 0 ; index < IP_Count ; ++index)
{
m_UdpSocket.m_strClientAddress = (char *)IP_List[index];
ret = m_UdpSocket.Send(Stream, StreamLength);
if (ret == false)
--index;
}
But I don't know if this method is reasonable or not?

MikeAThon
June 12th, 2007, 12:19 PM
10035 is WSAWOULDBLOCK, and is a totally normal and expected result for a Send() opertion (and many other operations too, like recv()). It means that the socket is non-blocking, but the requested operation would cause the program to block while waiting for completion of the operation. Thus, the operation is not even commenced. Winsock will send a notification (like FD_SEND in the case of a blocked Send()) when the operation can be tried again and can be expected to complete successfully without blocking.

The notifications are the reason why CAsyncSocket is "asynchronous", i.e., the notifications come to you asynchronously.

Based on your code snippet, which is completely serialized and not ready for asynchronous operations, you probably would prefer for the socket to block. To do so, after creating the socket, call ioctlsocket() with FIONBIO, and set the socket into blocking mode.

Note that blocking operations should not be carried out in a UI thread, since they will block user interactions too.

Mike

Cooker
June 13th, 2007, 01:25 AM
Thanks for you help me understand asynchronous socket : CAsyncSocket is asynchronous, and the Send() operation is blocking and synchronous.
I think I need to modify my code set the socket into blocking mode. :)

10035 is WSAWOULDBLOCK, and is a totally normal and expected result for a Send() opertion (and many other operations too, like recv()). It means that the socket is non-blocking, but the requested operation would cause the program to block while waiting for completion of the operation. Thus, the operation is not even commenced. Winsock will send a notification (like FD_SEND in the case of a blocked Send()) when the operation can be tried again and can be expected to complete successfully without blocking.

The notifications are the reason why CAsyncSocket is "asynchronous", i.e., the notifications come to you asynchronously.

Based on your code snippet, which is completely serialized and not ready for asynchronous operations, you probably would prefer for the socket to block. To do so, after creating the socket, call ioctlsocket() with FIONBIO, and set the socket into blocking mode.

Note that blocking operations should not be carried out in a UI thread, since they will block user interactions too.

Mike