|
-
June 6th, 2006, 08:56 PM
#1
recv returning WSAEOPNOTSUPP error
Hi,
I would like to use tcp connection to get some data from a client. I tried to use the following:
bytesReceived = recv(client,&CODE,1,MSG_WAITALL);
I figured that the recv will wait until it gets 1 byte before falling through but instead I get the error WSAEOPNOTSUPP. I looked at the msdn website and it seems that MSG_WAITALL would not work for non-blocking function but I thought that recv was a blocking function so what gives. Thanks for the help,
Amish
-
June 7th, 2006, 01:05 AM
#2
Re: recv returning WSAEOPNOTSUPP error
Try this:
Code:
bytesReceived = recv(client,&CODE,1,0);
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!
-
June 7th, 2006, 04:56 AM
#3
Re: recv returning WSAEOPNOTSUPP error
The socket will be in blocking mode, when you created it with the socket() function and did not call WSAAsyncSelect(), WSAEventSelect() on it or used ioctlsocket() to set it to nonblocking.
Please don't forget to rate users who helped you!
-
June 7th, 2006, 09:54 AM
#4
Re: recv returning WSAEOPNOTSUPP error
Here is the whole code. I never call WSAAsyncSelect(), WSAEventSelect() on it or used ioctlsocket() so it has to be in blocking mode. Yet I still get the error. As for using 0 instead of MSG_WAITALL, it works but it returns for every byte it gets instead of chunks of bytes:
Code:
cout << "Starting up TCP server\r\n";
int numBytesToReceive = 0;
int bytesReceived = 0;
char CODE = 0x00;
char CRC[4];
char fileSize[3];
int counter = 0;
char data;
int error = 0;
SOCKET server;/*Server side connection point of link*/
WSADATA wsaData;/*The data obtained from calling the winsock startup routine*/
sockaddr_in server_add; /*A structure that will contain the information of the server socket*/
int wsaret=WSAStartup(0x101,&wsaData);/*Initialize winsock*/
//WSAStartup returns zero on success.
//If it fails we exit.
if(wsaret!=0)
{
return -1;
}
//the socket function creates our SOCKET
server=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//If the socket() function fails we exit
if(server==INVALID_SOCKET)
{
return -1;
}
//Now we populate the sockaddr_in structure
server_add.sin_addr.s_addr=INADDR_ANY; //Wild card IP address
server_add.sin_family=AF_INET; //Address family
server_add.sin_port=htons((u_short)20248); //port to use
//bind links the socket we just created with the sockaddr_in
//structure. Basically it connects the socket with
//the local address and a specified port.
//If it returns non-zero quit, as this indicates error
if(bind(server,(sockaddr*)&server_add,sizeof(server_add))!=0)
{
return -1;
}
//listen instructs the socket to listen for incoming
//connections from clients. The second arg is the backlog
if(listen(server,10)!=0)
{
return 0;
}
//we will need variables to hold the client socket.
//thus we declare them here.
SOCKET client;
sockaddr_in client_add;
int fromlen=sizeof(client_add);
while(true)//we are looping endlessly
{
// char temp[512];
//accept() will accept an incoming
//client connection
printf("Waiting for connection\n");
client=accept(server,(struct sockaddr*)&client_add,&fromlen);
bytesReceived = recv(client,&CODE,1,0);
if(bytesReceived > 0) {
switch(CODE) {
case '1': // File will be sent
bytesReceived = recv(client,CRC,4,0); // Receive CRC
bytesReceived = recv(client,fileSize,3,0); // Receive file size
while(counter != (int)fileSize) { // Receive data and create a file
bytesReceived = recv(client,&data,1,MSG_WAITALL); // Receive file data
counter++;
printf("Counter: %d, Data: %c\n",counter,data);
}
counter = 0;
break;
case 0x09:
break;
default:
break;
}
}
else {
error = WSAGetLastError();
}
/*Create a thread to handle info from this client*/
//AfxBeginThread(ClientThread,0);
printf("Your IP is %s\r\n",inet_ntoa(client_add.sin_addr));
//we simply send this string to the client
//send(client,temp,strlen(temp),0); rcv(server,...)
//cout << "Connection from " << inet_ntoa(client_add.sin_addr) <<"\r\n";
//close the client socket
closesocket(client);
}
//closesocket() closes the socket and releases the socket descriptor
closesocket(server);
//originally this function probably had some use
//currently this is just for backward compatibility
//but it is safer to call it as I still believe some
//implementations use this to terminate use of WS2_32.DLL
WSACleanup();
return 0;
Thanks for your time,
Amish
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|