WinSock Function Locking up...

This is a discussion on WinSock Function Locking up... within the Windows Programming forums, part of the Platform Specific Boards category; For some reason this function completely locks up... if someoen could help I would really appriciate it: void NETCLASS::_SendInfo(char *data,unsigned ...

  1. #1
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    WinSock Function Locking up...

    For some reason this function completely locks up... if someoen could help I would really appriciate it:

    void NETCLASS::_SendInfo(char *data,unsigned int size,char *destip,unsigned int port)
    {
    int retval, loopflag=0;
    int maxloop=-1;
    unsigned int addr;
    int socket_type = DEFAULT_PROTO;
    struct sockaddr_in server;
    struct hostent *hp;
    WSADATA wsaData;
    SOCKET conn_socket;



    if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR)
    {
    WSACleanup();
    return;
    }

    /* use gethostbyname() or gethostbyaddr()? I think I'll write some code to check....*/

    if (isalpha(destip[0]))
    { /* server address is a name */
    hp = gethostbyname(destip);
    }
    else
    { /* Convert destip address to a usable one */
    addr = inet_addr(destip);
    hp = gethostbyaddr((char *)&addr,4,AF_INET);
    }
    if (hp == NULL ) {

    WSACleanup();
    return;
    }


    // Copy resolved information into sockaddr_in structure

    memset(&server,0,sizeof(server));
    memcpy(&(server.sin_addr),hp->h_addr,hp->h_length);
    server.sin_family = hp->h_addrtype;
    server.sin_port = htons(port);

    conn_socket = socket(AF_INET,socket_type,0); /* Open a socket */
    if (conn_socket <0 )
    {
    WSACleanup();
    return;
    }

    if (connect(conn_socket,(struct sockaddr*)&server,sizeof(server)) == SOCKET_ERROR)
    {
    WSACleanup();
    return;
    }

    // string to send
    retval = send(conn_socket,data,size,0);
    if (retval == SOCKET_ERROR)
    {
    WSACleanup();
    return;
    }

    /*retval = recv(conn_socket,data,size,0 );
    if (retval == SOCKET_ERROR)
    {
    closesocket(conn_socket);
    WSACleanup();
    return;
    }*/


    // We are not likely to see this with UDP, since there is no
    // 'connection' established.
    //
    if (retval == 0)
    {

    closesocket(conn_socket);
    WSACleanup();
    return;
    }
    closesocket(conn_socket);
    WSACleanup();
    return;
    }

    SPH

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    22

    function locking

    Hi minime6696,

    Well, the winsock API was build on the Berkerly Sockets' model (some UNIX guys) and in this model all functions pertaining to connection-oriented sockets "block" (I would suppose this is what you mean by the function "locking") until they either return some kind of error or the process is killed, more especially the "send" and "recv" functions. Do you think you could try to figure out which of the function calls in your code causes it to lock? You could do this by probably placing statements e.g
    exit(0); after the call to see whether the program executes past the call.

    Check the server running on the other end. And see to it that it actually sends the replys your program would expect (when it calls recv). If everything seems okay then it just may be the normal problems of the physical layer (collisions etc...) and if you are using the reliable protocol (TCP), it won't give up(!) so you would probably need to give it time. I usually create a thread to call these socket functions (hence circumvent the locking) and prevent any other connections until the first one either times out or returns.
    -------------------------
    Gerald.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Function problem (Winsock)
    By Pandora in forum Windows Programming
    Replies: 13
    Last Post: 04-03-2003, 01:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21