Winsock - weird problem with shutdown( socket, SD_SEND )

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

Winsock - weird problem with shutdown( socket, SD_SEND )

 
0
  #1
Sep 24th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Cosmin871 is an unknown quantity at this point 
Solved Threads: 0
Cosmin871 Cosmin871 is offline Offline
Newbie Poster

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

 
0
  #2
Sep 25th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 45
Reputation: Icebone1000 is an unknown quantity at this point 
Solved Threads: 0
Icebone1000's Avatar
Icebone1000 Icebone1000 is offline Offline
Light Poster

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

 
0
  #3
Sep 25th, 2009
Originally Posted by Cosmin871 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Cosmin871 is an unknown quantity at this point 
Solved Threads: 0
Cosmin871 Cosmin871 is offline Offline
Newbie Poster

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

 
0
  #4
Sep 27th, 2009
  1. if( iResult = SOCKET_ERROR ){//*******HERES THE PROBLEM***************
try using
  1. if( iResult == SOCKET_ERROR ){//*******HERES THE PROBLEM***************
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: lashatt2 is an unknown quantity at this point 
Solved Threads: 0
lashatt2 lashatt2 is offline Offline
Newbie Poster
 
0
  #5
Oct 21st, 2009
how can I send messages in global. like skype does?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Cosmin871 is an unknown quantity at this point 
Solved Threads: 0
Cosmin871 Cosmin871 is offline Offline
Newbie Poster
 
0
  #6
Oct 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: lashatt2 is an unknown quantity at this point 
Solved Threads: 0
lashatt2 lashatt2 is offline Offline
Newbie Poster
 
0
  #7
25 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: lashatt2 is an unknown quantity at this point 
Solved Threads: 0
lashatt2 lashatt2 is offline Offline
Newbie Poster
 
0
  #8
25 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: Cosmin871 is an unknown quantity at this point 
Solved Threads: 0
Cosmin871 Cosmin871 is offline Offline
Newbie Poster
 
0
  #9
24 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 15
Reputation: lashatt2 is an unknown quantity at this point 
Solved Threads: 0
lashatt2 lashatt2 is offline Offline
Newbie Poster
 
0
  #10
23 Days Ago
What ip should I write? and what port?
Reply With Quote Quick reply to this message  
Reply

Tags
network, shutdown(), win32, winsock

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC