Re: please review: simple winsock example for sending data over a nonblocking socket
- From: "Alex Passos" <bz@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 4 Apr 2005 14:06:44 -0500
try
{
closesocket(sock);
WSACleanup();
}
catch(...)
{
>>> What happens if you get here? No WSACleanup()
}
Thats the only thing I could see in your code that really stood out. I
would recommend that you do the socket initialization stuff only once when
your program starts up and shut it down only once when it terminates, I know
you do this now with a simple test program but if you continue work with it
and use the function many times it would be best to have those calls outside
that loop.
The Win32 calls are generally not going to throw exceptions unless its a
division by zero or hardware failure of some kind so instead of using
try-catch blocks wrap them in if-statements and check the results.
Also I think there are WSA calls for asynchronous sockets, check this link:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsacleanup_2.asp
Then look over all of the WSA calls and samples so you can see how they
work.
"Daniel" <softwareengineer98037@xxxxxxxxx> wrote in message
news:%23MxaQA6GFHA.2428@xxxxxxxxxxxxxxxxxxxxxxx
> please review: simple winsock example for sending data over a nonblocking
> socket
>
> I put together a simple winsock example for sending data over a
> nonblocking
> socket because i could not find such a simple example on the internet.
> please tell me if you see any MEMORY LEAKS, RESOURSE MISUSE, or other
> critical issues with this code:
> http://rafb.net/paste/results/mZAzaI55.html
> I would like this to be perfect as i will use it over and over. Any
> meaningfull and focused suggestions appreciated. In no case should this
> code
> hang because of any kind of network or remote server issue. And in no case
> should it leak memory or resources:
>
> #include "stdafx.h"
> #include <winsock2.h>
> #pragma comment(lib, "ws2_32.lib")
>
> int main(void);
> int genericstream(char*, int, char*, int);
>
> int main(void)
> {
> char* pData = new char[12];
> char* pIp = new char[11];
> memcpy((void*)pData, (const void*)"hello world", 11);
> pData[11] = ' ';
> memcpy((void*)pIp, (const void*)"3.3.3.222", 10);
> pIp[10] = '\0';
> int ret = genericstream(pData, 12, pIp, 11);
> pData[4] = '\0';
> delete[] pData;
> delete[] pIp;
> return 0;
> }
>
> int genericstream(char* a, int iLen, char* pIp, int iIpLen)
> {
> int sock;
> try
> {
> WSADATA wsaData;
>
> //load winsock dlls if not already loaded
> if(WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
> {
> return 1;
> }
>
> //create a socket
> sock = socket(AF_INET, SOCK_STREAM, 0);
> if (sock == INVALID_SOCKET)
> {
> WSACleanup();
> return 1;
> }
> unsigned long ulMode = 1;
>
> //make the socket connect operation
> //return right away even if the
> //remote computer hangs on connect
> //e.g. nonblocking socket
> ioctlsocket(sock, FIONBIO, &ulMode);
>
> sockaddr_in clientService;
> clientService.sin_family = AF_INET;
> clientService.sin_addr.s_addr = inet_addr( pIp );
> clientService.sin_port = htons(8888);
> memset(clientService.sin_zero, 0, 8);
>
> //try to connect, should return SOCKET_ERROR most of the time as the
> connect
> //is on a nonblocking socket
> if(connect( sock, (SOCKADDR*) &clientService, sizeof(clientService) ) ==
> SOCKET_ERROR)
> {
> if(WSAGetLastError() == WSAEWOULDBLOCK)
> {
> fd_set fsConnect;
> FD_ZERO(&fsConnect);
> FD_SET(sock, &fsConnect);
> timeval sTimeoutVal;
> sTimeoutVal.tv_sec = (long)30;
> sTimeoutVal.tv_usec = (long)0;
>
> //wait up to 30 seconds for the socket to complete connecting, unless
> the target computer, network or
> //internet is hanging this should return with 1 right away
> int retval = select(FD_SETSIZE, (fd_set *) NULL, &fsConnect, (fd_set
> *)
> NULL, &sTimeoutVal);
> if(retval != 1)
> {
>
> //connect timed out
> //close socket and remove a winsock reference count
> closesocket(sock);
> WSACleanup();
> return 1;
> }
> }
> else
> {
>
> //connect failed right away, no need to select status of socket
> //close socket and remove a winsock reference count
> closesocket(sock);
> WSACleanup();
> return 1;
> }
> }
>
> //send data
> int iBytes = send(sock, a, iLen-1, 0);
> if(iBytes == -1 || iBytes != iLen-1)
> {
>
> //send failed, perhaps target application or machine went down
> //close socket and remove a winsock reference count
> closesocket(sock);
> WSACleanup();
> return 1;
> }
> //close socket and remove a winsock reference count
> closesocket(sock);
> WSACleanup();
> }
> catch(...)
> {
> try
> {
> closesocket(sock);
> WSACleanup();
> }
> catch(...)
> {
> }
> return 1;
> }
> return 0;
> }
>
>
.
- Prev by Date: Getting Table Modied time in C++
- Next by Date: Re: Date time stamp shows different
- Previous by thread: Getting Table Modied time in C++
- Next by thread: Re: Date time stamp shows different
- Index(es):
Relevant Pages
|