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 .
// 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
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)?
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.
Originally Posted by OReubens
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
Originally Posted by OReubens
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
:-\\
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..
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
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.