943,581 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3979
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 24th, 2009
0

Winsock - weird problem with shutdown( socket, SD_SEND )

Expand Post »
First..Im very beginner with winsock and network programing..
Im doing a simple app that can act or as server or as client. What Im doing is that I open the app twice in the same computer, in one I choose server, and in the other client.

With the server part, everything is fine, but with the client, when I shutdown the socket for sending, shutdown returns SOCKET_ERROR, I cant figure out why, the most weird thing is that WSAGetLastError() returns 0(zero)! That doesnt make sense..

Also, what Im doing is that if an error occur, I close the socket and quit the app, but if I just ignore that shutdown error, everythig is fine..

It doesnt make sense, its like shutdown should not return any error..

Heres the client part of my code:
C++ Syntax (Toggle Plain Text)
  1. //send data:
  2. iResult = send( ConnectSocket, sendBuffer, (int)strlen(sendBuffer), 0 ); //return size of bytes sent
  3.  
  4. if( iResult == SOCKET_ERROR ){
  5.  
  6. MessageBox( NULL, TEXT("send() failed"),
  7. TEXT(" winsock error"), MB );
  8. closesocket( ConnectSocket );
  9. return;
  10. }
  11. TCHAR buffer[100];
  12. wsprintf( buffer, TEXT(" number of bytes sent: %i "), iResult );
  13.  
  14. TextOut( hdc, x, y, buffer, (int)strlen(buffer) );
  15. y+=20;
  16. //---------------------------------------------------
  17.  
  18.  
  19. //stop sending:
  20. iResult = shutdown( ConnectSocket, SD_SEND ); //it still receive!
  21. if( iResult = SOCKET_ERROR ){//*******HERES THE PROBLEM***************
  22.  
  23. iResult = WSAGetLastError();//****RETURNS ZERO????************
  24.  
  25. TextOut( hdc, x, y, TEXT(" shutdown() failed"), (int)strlen(TEXT(" shutdown() failed")) );
  26. y+=20;
  27.  
  28. //closesocket( ConnectSocket );//****COMMENTING THIS, WORKS FINE**
  29. //return;
  30. }
  31. //---------------------------------------------------
  32.  
  33.  
  34. //receive data until server close connection:
  35. do{
  36. iResult = recv( ConnectSocket, recvBuffer, 1000, 0 ); //return size of bytes received
  37.  
  38. if( iResult > 0 ){
  39.  
  40. wsprintf( buffer, TEXT(" number of bytes received: %i "), iResult );//display bytes received
  41. TextOut( hdc, x, y, buffer, (int)strlen(buffer) );
  42. y+=20;
  43.  
  44. TextOut( hdc, x, y, recvBuffer, iResult ); //display data received
  45. y+=20;
  46.  
  47. }else if( iResult == 0 ){
  48.  
  49. TextOut( hdc, x, y, TEXT("Connection Closed!"), (int)strlen(TEXT("Connection Closed!")) );
  50. y+=40;
  51.  
  52. }else{
  53.  
  54. TextOut( hdc, x, y, TEXT(" recv() failed"), (int)strlen(TEXT(" recv() failed")) );
  55. y+=40;
  56. }
  57.  
  58. }while( iResult > 0 );
  59. //---------------------------------------------------
  60.  
  61. ReleaseDC( hmainwnd, hdc );
  62.  
  63.  
  64. //stop receive and close socket:
  65. closesocket( ConnectSocket );
  66. }
Last edited by Icebone1000; Sep 24th, 2009 at 3:55 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Sep 25th, 2009
0

Re: Winsock - weird problem with shutdown( socket, SD_SEND )

The shutdown function disables sends or receives on a socket and you are using the ConnectSocket after the shutdown function to receive info.
The shutdown function is normally used before the close function so that you stop the stream flow before you close the socket.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Cosmin871 is offline Offline
23 posts
since Sep 2009
Sep 25th, 2009
0

Re: Winsock - weird problem with shutdown( socket, SD_SEND )

Click to Expand / Collapse  Quote originally posted by Cosmin871 ...
The shutdown function disables sends or receives on a socket and you are using the ConnectSocket after the shutdown function to receive info.
The shutdown function is normally used before the close function so that you stop the stream flow before you close the socket.
That doesnt explains anything man, Im following the msdn samples btw, and thats exactly what it does, you shutdown just for sending, but you still can receive, and Im get the error AT the function, not after.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Icebone1000 is offline Offline
50 posts
since Jan 2008
Sep 27th, 2009
0

Re: Winsock - weird problem with shutdown( socket, SD_SEND )

C++ Syntax (Toggle Plain Text)
  1. if( iResult = SOCKET_ERROR ){//*******HERES THE PROBLEM***************
try using
C++ Syntax (Toggle Plain Text)
  1. if( iResult == SOCKET_ERROR ){//*******HERES THE PROBLEM***************
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Cosmin871 is offline Offline
23 posts
since Sep 2009
Oct 21st, 2009
0
Re: Winsock - weird problem with shutdown( socket, SD_SEND )
how can I send messages in global. like skype does?
Reputation Points: 5
Solved Threads: 0
Light Poster
lashatt2 is offline Offline
36 posts
since Oct 2009
Oct 21st, 2009
0
Re: Winsock - weird problem with shutdown( socket, SD_SEND )
You must know the socket you want to send the message:
iResult = send( SOCKET s, char *buff, int len, int flag );
so if you want to send a message to multiple users you must send the message to every user -> socket.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Cosmin871 is offline Offline
23 posts
since Sep 2009
Oct 31st, 2009
0
Re: Winsock - weird problem with shutdown( socket, SD_SEND )
I can send messages on lan but I cann't send messages on global on internet. what should I do have you got a source of how can I send messages on global.
Reputation Points: 5
Solved Threads: 0
Light Poster
lashatt2 is offline Offline
36 posts
since Oct 2009
Oct 31st, 2009
0
Re: Winsock - weird problem with shutdown( socket, SD_SEND )
I can send messages on lan on local but I cann't send messages on internet on global. please help me. do you have a source of sending messages on global on internet?
Reputation Points: 5
Solved Threads: 0
Light Poster
lashatt2 is offline Offline
36 posts
since Oct 2009
Nov 1st, 2009
0
Re: Winsock - weird problem with shutdown( socket, SD_SEND )
if you are using sockets there should be no problem sending messages over the internet, and if it works over LAN it should work for everything, maybe the IP was not correct
Reputation Points: 10
Solved Threads: 2
Newbie Poster
Cosmin871 is offline Offline
23 posts
since Sep 2009
Nov 2nd, 2009
0
Re: Winsock - weird problem with shutdown( socket, SD_SEND )
What ip should I write? and what port?
Reputation Points: 5
Solved Threads: 0
Light Poster
lashatt2 is offline Offline
36 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: plz help me with this Error!! it too simple but i cant find it!!
Next Thread in C++ Forum Timeline: very basic problem in c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC