please review: simple winsock example for sending data over a nonblocking socket

From: Daniel (softwareengineer98037_at_yahoo.com)
Date: 02/26/05


Date: Fri, 25 Feb 2005 17:17:02 -0800

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;
}



Relevant Pages

  • 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. ... int genericstream; ...
    (microsoft.public.vc.language)
  • Re: Error using P/Invoke with Winsock API using CF.NET
    ... I'm attempting to call ioctlsocket based from WINSOCK.H. ... public static extern int ioctlsocket(int s, uint controlCode, byte ... > System.Net.Sockets wraps the Winsock APIs and the Handle exposed is actually ... > a valid socket handle. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Error using P/Invoke with Winsock API using CF.NET
    ... ..NET CF socket class. ... > I'm attempting to call ioctlsocket based from WINSOCK.H. ... > inBuffer, int inBufferLen, byteoutBuffer, int outBufferLen, out int ... >> System.Net.Sockets wraps the Winsock APIs and the Handle exposed is ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Linux 2.6.29.4
    ... static int is_efer_nx ... if (ret) ... struct ftdi_private { ... the specific security attributes of the socket ...
    (Linux-Kernel)
  • [PATCH] paccept, socket, socketpair w/flags
    ... there is a helper function to convert the new socket flags into ... socket() and socketpair. ... static int status; ... int fd1, fd2, err; ...
    (Linux-Kernel)

Loading