// JP opened flex table

Click to See Complete Forum and Search --> : Problem with setsockopt( , , SO_SNDBUF, 0, sizeof(int)) in windows 2000


swathi_k
February 5th, 2007, 10:37 AM
I has written simple client and server code ( in VC++ 6.0). In server select( , ,wfd,) is not getting activated if I use setsockopt( , , SO_SNDBUF, 0,) for windows 2000 but it is working perfectly in windows XP

Server code

#include <winsock2.h>
#include <windows.h>
#include <stddef.h>
#include <mswsock.h>
#include<stdio.h>
#pragma comment(lib,"ws2_32")
void main()
{
WSADATA wsa;
WSAStartup(MAKEWORD(2,0),&wsa);

int listenfd=0,len=0;
SOCKET acceptfd;
int i=0,nrec=0,bytes=0;
char serverbuff[1024]={0};
struct sockaddr_in serv;
len=sizeof(serv);

FD_SET wfd;
FD_ZERO(&wfd);

struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;

listenfd=socket(AF_INET,SOCK_STREAM,0);
serv.sin_family=AF_INET;
serv.sin_port=htons(9393);
serv.sin_addr.s_addr=INADDR_ANY;

bind(listenfd,(struct sockaddr *)&serv,len);
listen(listenfd,5);
printf("listen fd %d\n",listenfd);

printf("wait for accept\n");

acceptfd=accept(listenfd,(struct sockaddr *)&serv,&len);

if(!acceptfd)
{
printf(" error in accept sock \n");
return ;
}
printf("SERVER accept %d \n ",acceptfd);

int one = 0;
setsockopt(acceptfd, SOL_SOCKET, SO_SNDBUF, (char *)&one, sizeof(one));
//if I remove this it is working in windows 2000 also

FD_SET(acceptfd, &wfd);

printf("Waiting for select ...\n");
int ret = select(-1, NULL, &wfd, NULL, &tv);
if(ret == 0 || ret == SOCKET_ERROR)
{
int error = WSAGetLastError(); // 10038 - WSAENOTSOCK
printf("\nselect error %d\n", error);

}
else
{
i=strlen("+OK IncoreProxy POP3 proxy\r\n");
bytes=send(acceptfd,"+OK IncoreProxy POP3 proxy\r\n",i,0);
printf(" send bytes %d \n",bytes);
}

}

MikeAThon
February 5th, 2007, 07:13 PM
What does setsockopt() return? Zero means success; otherwise, SOCKET_ERROR means failure, and then please tell us the WSAGetLastError() code.

Please tell us if select() is returning zero or SOCKET_ERROR (the code allows for either). Per the comment in the code, is WSAENOTSOCK the error you see? Note that in the context of select(), this error means that one of the sockets in the fd_set's is not a valid socket.

Mike

MikeAThon
February 5th, 2007, 07:52 PM
Incidentally, it was my understanding the setting the send buffer to zero was helpful only for sockets with overlapped IO. Your socket is working in blocking mode, so it's hard to see any benefit to SO_SNDBUF of zero.

Mike

PS: Please use [ code ][ /code ] tags to format your code

swathi_k
February 5th, 2007, 11:22 PM
What does setsockopt() return? Zero means success; otherwise, SOCKET_ERROR means failure, and then please tell us the WSAGetLastError() code.

Please tell us if select() is returning zero or SOCKET_ERROR (the code allows for either). Per the comment in the code, is WSAENOTSOCK the error you see? Note that in the context of select(), this error means that one of the sockets in the fd_set's is not a valid socket.

Mike

in windows 2000
setsockopt() is returning 0
select() is returning 0
WSAGetLastError() is returning 0

but in windows XP
setsockopt() is returning 0
select() is returning 1

and sorry to say you by mistake I copied // 10038 - WSAENOTSOCK

MikeAThon
February 5th, 2007, 11:46 PM
in windows 2000
setsockopt() is returning 0
select() is returning 0
WSAGetLastError() is returning 0

If select() returns 0, then there's no error. Zero means that the function timed out. With the parameters in your code, zero means that nothing is ready for writing within the 5 second timeout parameter.

Reset the content of wfd and call select() again.

swathi_k
February 6th, 2007, 04:58 AM
If select() returns 0, then there's no error. Zero means that the function timed out. With the parameters in your code, zero means that nothing is ready for writing within the 5 second timeout parameter.

Reset the content of wfd and call select() again.

i tried all the ways but it didn't work but if i remove setsockopt(, , SO_SNDBUF, 0, ) it works . so what i have to do in windows 2000 to make to work

MikeAThon
February 6th, 2007, 10:37 AM
Omit the call to setsockopt(.., SO_SNDBUF, 0, ...).

Seriously, why do you have it anyway? As indicated above, it was my understanding that setting the send buffer to zero was helpful only for sockets with overlapped IO. And even in those cases, it was not as helpful as a recv buffer of zero. Your socket is working in blocking mode, so it's hard to see any benefit to SO_SNDBUF of zero.

Mike

//JP added flex table