CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 1999
    Location
    Romania
    Posts
    70

    winsock - send(...) return 0

    hi,
    i write an program which use sockets .
    i create a socket ( mySocket=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP) ).
    i connect it to remote server ( HTTP ).
    i try to send some data to remote server . it works fine when my connection to Net is not very busy otherwise the "send(...)" function return 0 - that means no data was sent ( to be sure i can send data i use "select(...)" function before i use "send(...)" ) .
    now , what should i do in this case ?
    what i actually do : close the socket , create another socket etc. but this means a waste of time . should i try to send data using the same socket ?
    since it is an HTTP server it may close the connection before i can send my data and it is important for me to know that this particular data reach to server .

    any idea will be appreciated .
    thanks in advance .



  2. #2
    Join Date
    Jan 2011
    Posts
    4

    Unhappy Re: winsock - send(...) return 0

    I have the same problem:

    char* SData(char* ipv4, unsigned short uPort, char* szSent, char* szOut, int iSent, int iRecv)
    {
    DWORD dwErr=0;
    int iResult=0;
    char hostname[NI_MAXHOST];
    char servInfo[NI_MAXSERV];
    SOCKADDR_IN sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(uPort);
    sin.sin_addr.s_addr = inet_addr(ipv4);

    dwErr = getnameinfo((SOCKADDR*)&sin, sizeof(SOCKADDR), hostname,
    NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV);

    if (dwErr != 0) return "getnameinfo failed with error#\n";
    else return "getnameinfo returned hostname = %s\n, hostname";

    SOCKET ConnectSocket = INVALID_SOCKET;
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    connect(ConnectSocket, (SOCKADDR*)&sin, sizeof(SOCKADDR_IN));

    iResult=send(ConnectSocket, szSent, sizeof(szSent), NULL);
    if (iResult == SOCKET_ERROR) {
    closesocket(ConnectSocket);
    return "send failed: \n";
    }
    else iSent=iResult;

    iResult=recv(ConnectSocket, szOut, sizeof(szOut), NULL);
    if (iResult == SOCKET_ERROR) {
    closesocket(ConnectSocket);
    return "recv failed: \n";
    }
    else iRecv=iResult;

    closesocket(ConnectSocket);
    return szOut;
    }

    // variables
    int s=0;
    int r=0;
    char szTmp[256]="127.0.0.1"; // any ip
    char szCmbSelect[255]="80"; // port
    char szPkt[1024]="let me be your client))";
    char szOut[1024]={0};
    // And then we call the function
    char* tmp=SData( szTmp, _atoi(szCmbSelect), szPkt, szOut, s, r);
    printf ("Bytes sent: %d\r\nBytes recieved: %d\r\n\r\n"),s,r);

    There are no errors!
    Data successfully has been sent and recieved. It's ok.
    But through send returns 0 we have output:
    Bytes sent: 0
    Bytes recieved: 0

    Why send() return 0 ???
    Any idea?

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: winsock - send(...) return 0

    Seems normal, the printf() is printing s and r, which have both been initialised to 0.
    if you want to return those from the function, you'll need to change them from by-value to by-reference.

    If you changed that:
    i'd expect it to return 4 on a Win32 build and 8 on a Win64 build since you are telling send() to send the sizeof(szSend) which is the size of a pointer, not the size of the text where that pointer is poiting to. You probably intended strlen(szSend) (or the unicode variant)?

  4. #4
    Join Date
    Jan 2011
    Posts
    4

    Re: winsock - send(...) return 0

    Codeguru forum is nice for it content, but so bad here you can't format your message with font size, code font, posting screens, tabs and other options.. All of it makes difficult to read somebody`s post. Also it has dynamicaly filling content by scripts. So uneasily always to push the button "Yes" or "No" if you have non-standart options of you browser. That is why I so thanks to you for answer!

    This line of code is usless (I'd removed it):
    else return "getnameinfo returned hostname = %s\n, hostname";

    after getnameinfo() succeeded it returns with hostname. No wonder all of code after it did not run. send and recv had not been called that is why r and s variables which shows total number of sent and recieved bytes are NULL.

    I'm also removed call of getnameinfo() from the code because numeric remote host ip already passing to SData through the (char* ipv4,,,) argument. And so I don't understand why we need it to connect to remote web-server.

    Quote Originally Posted by OReubens View Post
    i'd expect it to return 4 on a Win32 build and 8 on a Win64 build since you are telling send() to send the sizeof(szSend) which is the size of a pointer, not the size of the text where that pointer is poiting to.
    You was right! using sizeof(szSend) instead of strlen() or wcslen for the UNICODE it is a rough mistake although you can find some code examples in MSDN whith sizeof insted of strlen! So be careful with that!

    Now everything works fine. I can send the data to server and server answer me. For example if I sending the request "GET /automation/n09230945.asp HTTP/1.1\r\n host: www.whatismyip.com\r\n\r\n" to 72.233.89.197 ip (it is whatismyip.com domain) it response with http header and show me my public ip.

    I'm not strong in WEB-programming so judge no lest ye be judged.
    I know only one request GET If you know more requests please show me small examples. Or you can answer me what else I can talk about with any random server ?? Ah

    Quote Originally Posted by OReubens View Post
    Seems normal, the printf() is printing s and r, which have both been initialised to 0.
    if you want to return those from the function, you'll need to change them from by-value to by-reference.
    Yes, EXACTLY! not by-value, but by-reference. Don't know how. Show me please if you can. I want. And thank you very much (indeed) for your answer. Let the god bless you.

    iSent=iResult;
    iRecv=iReulst;

    r and s ARE STILL EQUAL 0 (((((((
    Bytes sent: 0
    Bytes recieved: 0
    :-\\
    Attached Images Attached Images  

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: winsock - send(...) return 0

    Quote Originally Posted by zzz7net View Post
    Codeguru forum is nice for it content, but so bad here you can't format your message with font size, code font, posting screens, tabs and other options..
    If you had read this http://www.codeguru.com/forum/announcement.php?f=7 it contains this link http://www.codeguru.com/forum/misc.php?do=bbcode where all formatting tags are described.

    Usually the link just above the NewThread button is Before you post... but since JPnyc has posted the Live... now it's not. Can't blame you for not finding it...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: winsock - send(...) return 0

    Change
    Code:
    char* SData(char* ipv4, unsigned short uPort, char* szSent, char* szOut, int iSent, int iRecv)
    {
      // your code here
    }
    to
    Code:
    char* SData(char* ipv4, unsigned short uPort, char* szSent, char* szOut, int & iSent, int & iRecv)
    {
      // your code here
    }
    which will change the function from pass-by-value to pass-by-reference.

    Mike

  7. #7
    Join Date
    Jan 2011
    Posts
    4

    Re: winsock - send(...) return 0

    Thanks a lot California. Perfect! With your help we've puzzled it out!

    If somebody will expand the topic in the future with GET POST and other client requests or small examples - you are wellcome!


    P.S.
    Forum has helped me. So short advertisement column:
    www.CodeGuru.com - The number one developer site!
    Last edited by zzz7net; January 28th, 2011 at 09:32 AM. Reason: typo

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured