Bind Error 10038 with windows socket application in c - c

I am testing out a Socket Server application in c and I am getting an error on the bind function with code 10038. I looked this up and MSDN says it means:
An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket, or for select, a member of an fd_set was not valid.
Here is the code:
// I have the correct include files such as include , but stackoverflow displays it weird when i put #include winsock2.h
int main()
{
WSADATA wsaData;
SOCKET ListeningSocket;
SOCKET NewConnection;
SOCKADDR_IN ServerAddr;
int Port = 5150;
if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0)
{
printf("Server: WSAStartup failed with error %ld\n",WSAGetLastError());
return -1;
}
else
{
printf("Server: The Winsock dll found!\n");
printf("Server: The current status is: %s.\n",wsaData.szSystemStatus);
}
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("Server: The dll do not support Winsock version
%u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
WSACleanup();
return -1;
}
else
{
printf("Server: The dll supports the Winsock version %u.%u!\n",LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
printf("Server: The highest version this dll can support: %u.%u\n",LOBYTE(wsaData.wHighVersion),HIBYTE(wsaData.wHighVersion));
}
ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(ListeningSocket == INVALID_SOCKET)
{
printf("Server: Error at socket(), error code: %ld\n",WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Server: socket() is OK!\n");
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(ListeningSocket, (SOCKADDR *)&ServerAddr,sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("Server: bind() failed! Error code: %ld.\n",WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
else
printf("Server: bind() is OK!\n");
if(listen(ListeningSocket,5) == SOCKET_ERROR)
{
printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
return -1;
}
else
printf("Server: listen() is OK, I'm waiting for connections...\n");
printf("Server: accept() is ready...\n");
while(1)
{
NewConnection = SOCKET_ERROR;
while(NewConnection == SOCKET_ERROR)
{
NewConnection = accept(ListeningSocket, NULL, NULL);
}
printf("Server: accept() is OK...\n");
printf("Server: Client connected, ready for receiving and sending data...\n");
ListeningSocket = NewConnection;
break;
}
if(closesocket(NewConnection) != 0)
printf("Server: Cannot close \"NewConnection\" socket. Error code: %ld\n",
WSAGetLastError());
else
printf("Server: Closing \"NewConnection\" socket...\n");
if(WSACleanup() != 0)
printf("Server: WSACleanup() failed! Error code: %ld\n", WSAGetLastError());
else
printf("Server: WSACleanup() is OK...\n");
return 0;
}

I just got it, I put == for
ListeningSocket == socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
instea of =
ListeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);

Related

bind() returns error code 10038 using winsock.h

I am trying to implement a client-server architecture using sockets in Windows with winsock.h. When I call bind() function I get the error code 10038 and I don`t know why.
Here it is my code. This is only the server, created inside a thread where I initialice the socket and jump into an infinite loop to read data from client usng recvfrom function.
typedef struct
{
int sockfd;
struct sockaddr_in dir_client;
int long_dir_client;
struct sockaddr_in dir_server;
uint32_t receive_data;
}server_t;
void RX_thread_Client(void)
{
WSADATA wsaData;
server_t server;
int iResult;
int BytesReceived;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("[RxThreadClient] WSAStartup failed with error: %d\n", iResult);
return;
}
memset((char*)& server.dir_server, 0, sizeof(server.dir_server));
server.dir_server.sin_family = AF_INET;
server.dir_server.sin_addr.s_addr = inet_addr("10.128.169.46");
server.dir_server.sin_port = htons(6500);
server.long_dir_client = sizeof(server.dir_client);
// Create a new socket to make a client connection.
// AF_INET = 2, The Internet Protocol version 4 (IPv4) address family, TCP protocol
if ( server.sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) == INVALID_SOCKET)
{
printf("[RxThreadClient] Client: socket() failed! Error code: %ld\n", WSAGetLastError());
// Do the clean up
WSACleanup();
// Exit with error
return;
}
else
printf("[RxThreadClient] Client: socket() is OK!\n");
if ( bind(server.sockfd, (struct sockaddr*)&server.dir_server, sizeof(server.dir_server)) < 0) // ERROR IS HERE!!!
{
printf("[RxThreadClient] bind() failed! Error code: %ld\n", WSAGetLastError());
return;
}
else
printf("[RxThreadClient] Client: bind() is OK!\n");
while (1)
{
BytesReceived = recvfrom (server.sockfd, (char*)&server.receive_data,
sizeof(server.receive_data), 0,
(struct sockaddr*) & server.dir_client,
&server.long_dir_client);
if (BytesReceived == SOCKET_ERROR)
{
printf("[RxThreadClient] Client: send() error %ld.\n", WSAGetLastError());
break;
}
else
{
printf("[RxThreadClient] Client: send() is OK - bytes received: %ld\n", BytesReceived);
printf("[RxThreadClient] Word received: %d\n", server.receive_data);
}
Sleep(1000);
}
}
Thank you very much

How can I make my server continue to receive messages until client closes?

I am writing a simple client and server. I have my client written and I compared it to another server so it works perfectly. I am now trying to replicate the server, and for some reason, when my client sends a message "GET 2", the server reaches the recv function and it returns -1. Then it prints on the screen recv failed then it tries to do a shutdown but it also says shutdown failed. Does anyone know why?? I would greatly appreciate the help. Thank you!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFFLEN 1024
const unsigned int LISTENING_PORT = 21001;
const char ipAddress[15] = "127.0.0.1";
SOCKADDR_IN ServerAddr;
int main()
{
/*Declare and initialize variables*/
int return_Code = 10;
char recvBuff[DEFAULT_BUFFLEN] = "";
char storedQuotes[20][DEFAULT_BUFFLEN];
WSADATA wsaData;
SOCKET Socket;
SOCKET AcceptSocket;
SOCKADDR_IN ServerAddr;
/*Initialize Winsock*/
WSAStartup(MAKEWORD(2, 2), &wsaData);
/*Create New Socket*/
Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (Socket == INVALID_SOCKET)
{
printf("Server: Socket() failed! Error code: %d.\n", WSAGetLastError());
WSACleanup();
system("PAUSE");
return -1;
}
/*Specify what address to listen on*/
ServerAddr.sin_family = AF_INET; //IPv4
ServerAddr.sin_addr.s_addr = inet_addr(ipAddress); //IP Address
ServerAddr.sin_port = htons(LISTENING_PORT); //Port no.
/*Bind the Socket*/
if (bind(Socket, (SOCKADDR *)& ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("Server: bind() failed! Error code: %d.\n", WSAGetLastError());
closesocket(Socket);
WSACleanup();
system("PAUSE");
return -1;
}
/*Listen on socket for a client*/
if (listen(Socket, 1) == SOCKET_ERROR)
{
printf("Sever: listen() failed! Error code: %d.\n", WSAGetLastError());
closesocket(Socket);
WSACleanup();
system("PAUSE");
return -1;
}
else
printf("Server: Listening on port %d.\n\n", LISTENING_PORT);
/*Accept a connection from a client*/
AcceptSocket = accept(Socket, NULL, NULL);
if (AcceptSocket == SOCKET_ERROR)
{
printf("Server: accept() failed! Error code: %d.\n", WSAGetLastError());
closesocket(Socket);
WSACleanup();
system("PAUSE");
return -1;
}
else
printf("Server: accept() OK!\n");
/*Receive and send data*/
//return_Code = recv(Socket, recvBuff, DEFAULT_BUFFLEN, 0);
do {
return_Code = recv(Socket, recvBuff, DEFAULT_BUFFLEN, 0);//THIS LINE RETURNS -1, SO THE MESSAGE IS NEVER PROCESSED.
//IT GOES TO RECEIVE FAILED THEN SHUTDOWN FAILED.
if (return_Code > 0)
{
if (recvBuff[0] == 'G' && recvBuff[1] == 'E' && recvBuff[2] == 'T')
{
printf("GET MESSAGE RECEIVED");
}
else if (recvBuff[0] == 'S' && recvBuff[1] == 'E' && recvBuff[2] == 'T')
{
printf("SET MESSAGE RECEIVED");
}
else
{
printf("Error: GET and SET NOT RECIEVED");
}
}
else if (return_Code == 0)
printf("Server: Connection closed!");
else
printf("Server: recv() failed! Error code: %d.\n", WSAGetLastError());
} while (return_Code > 0);
/*Disconnect*/
if (shutdown(Socket, SD_BOTH) != 0)
printf("Server: shutdown() failed! Error code: %d.\n", WSAGetLastError());
else
printf("Server: shutdown() OK!\n");
getchar();
return 0;
}
Socket is the socket that's listening for connections. AcceptSocket is the connection between your program and the client. You need to call recv with AcceptSocket, not Socket.
Also, shutdown doesn't work on listening sockets, only connected ones. The same thing applies - you can shutdown AcceptSocket but not Socket.

socket functions fails after two connect failure

while(1)
{
if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
cout<<"Failed to obtain socket descriptor";
continue;
}
/* Try to connect the remote */
if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
{
cout << "Failed to Connect with Server\n";
closesocket(sockfd);
WSACleanup();
continue;
}
recv(sockfd, revbuf, LENGTH, 0);
closesocket(sockfd);
WSACleanup();
sleep(1000);
}
In above code, the socket functions fails after connect function fails two times. Since i am closing socket after connect failure, my socket function should be working all the time right? Can anyone explain why socket function is failing?

Winsock send () over TCP in C

I'm just confusing using the send() function in Winsock. Does this code actually send a string "Hello" over TCP ?. I managed to establish a connection with a TCP client in LabVIEW but it seems like that this TCP server doesn't send anything.
#define DEFAULT_BUFLEN 1024
#include<stdio.h>
#include<winsock2.h>
#include<Ws2tcpip.h>
#include<stdlib.h>
#include<string.h>
#include<stdint.h>
#include<stddef.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s , new_socket;
struct sockaddr_in server , client;
int c;
int iResult;
char *sendbuf = "Hello";
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 13000 );
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
}
puts("Bind done");
//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(s , (struct sockaddr *)&client, &c);
if (new_socket == INVALID_SOCKET)
{
printf("accept failed with error code : %d" , WSAGetLastError());
}
iResult = send( new_socket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR)
{
wprintf(L"send failed with error: %d\n", WSAGetLastError());
closesocket(new_socket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %d\n", iResult);
// shutdown the connection since no more data will be sent
iResult = shutdown(new_socket, SD_SEND);
if (iResult == SOCKET_ERROR)
{
wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
closesocket(new_socket);
WSACleanup();
return 1;
}
}
Your code is not initialzing WinSock, not allocating any SOCKET object, and not establishing a connection between the socket and a peer before calling send(), so to answer your question:
NO, your code is NOT sending a string over TCP.
HOWEVER, if you fill in the missing pieces - call socket() to create a TCP socket, and call bind()/listen()/accept() to establish a TCP connection with a peer - then YES, your code will be sending the string over TCP.
You need to do something more like the following instead. This is just a simple example that establishes a single TCP connection and then exits once the string has been sent to the client. In a real-world application, you would need to leave the server socket open and continuously calling accept() if you want to service multiple client connections over time, even if just a single client ever connects, disconnects, and reconnects:
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int iResult;
char *sendbuf = "Hello";
iResult = WSAStartup(MAKEWORD(2, 0), &wsa);
if (iResult != 0)
{
wprintf(L"WinSock startup failed with error: %d\n", iResult);
return 1;
}
server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_socket == INVALID_SOCKET)
{
wprintf(L"socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
memset(&server_addr, 0, sizeof(server_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(some port number here);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (sockaddr*)&server_addr, sizeof(server_addr)) != 0)
{
wprintf(L"bind failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
if (listen(server_socket, 1) != 0)
{
wprintf(L"listen failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
iResult = sizeof(client_addr);
client_socket = accept(server_socket, (sockaddr*)&client_addr, &iResult);
if (client_socket == SOCKET_ERROR)
{
wprintf(L"accept failed with error: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
closesocket(server_socket);
iResult = send(client_socket, sendbuf, strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR)
{
wprintf(L"send failed with error: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return 1;
}
printf("Bytes Sent: %d\n", iResult);
closesocket(client_socket);
WSACleanup();
return 0;
}
Update: based on your updated code, try this:
#include <stdio.h>
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#pragma comment(lib, "ws2_32.lib") //Winsock Library
int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int c, iResult;
char *sendbuf = "Hello";
printf("Initializing Winsock...\n");
iResult = WSAStartup(MAKEWORD(2,2), &wsa);
if (iResult != 0)
{
printf("WinSock initialization Failed. Error Code : %d", iResult);
return 1;
}
printf("WinSock Initialized.\n");
//Create a socket
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Could not create socket. Error Code : %d" , WSAGetLastError());
WSACleanup();
return 1;
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons( 13000 );
//Bind the listening port
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
{
printf("Bind failed. Error Code : %d", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
printf("Socket bound to port 13000.\n");
//Listen to incoming connection
if (listen(server_socket, 1) == SOCKET_ERROR)
{
printf("Listen failed. Error Code : %d", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
//Accept an incoming connection
printf("Waiting for incoming connection...\n");
c = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &c);
if (client_socket == INVALID_SOCKET)
{
printf("Accept failed. Error Code : %d", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}
printf("Client connected from %s:%hu\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
//Stop accepting incoming connections
closesocket(server_socket);
// Send string to client
iResult = send(client_socket, sendbuf, strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR)
{
printf("Send failed. Error Code : %d\n", WSAGetLastError());
iResult = 1;
}
else
{
printf("Bytes Sent: %d\n", iResult);
iResult = 0;
}
// shutdown the connection since no more data will be sent
if (shutdown(client_socket, SD_SEND) == SOCKET_ERROR)
{
printf("Shutdown failed. Error Code : %d\n", WSAGetLastError());
iResult = 1;
}
closesocket(client_socket);
WSACleanup();
return iResult;
}
No, it does not send the string "Hello". Even if the socket bind/accept/etc connection is OK, 'send(client_socket, sendbuf, strlen(sendbuf), 0);' does not send a C string. It sends five bytes, whereas the the C string "Hello" requires six bytes. Try:
send(client_socket, sendbuf, 1+strlen(sendbuf), 0);
A very high percentage of networking C code problems can be found by searching the source text for 'strlen'. printf(%s..), and assuming that TCP transfers messages longer than one byte, accounts for the rest:)

Client fails to connect

I'm a novice/beginner programmer having problems getting some simple client/server C code working. My end goal is to send a 'stream' of azimuth/elevation data from a server to a client, and then convert that data as it is received (it will just be a division, but I don't really know how to do this either) into position data for a pan/tilt unit, and then output the converted data via serial to the pan/tilt head. (I'll likely be back to ask about that later...)
Right now I'm just trying to figure out how to get the data sent and received. I grabbed code from this website. http://www.tenouk.com/Winsock/Winsock2example3.html. I had to move a few declarations around to get the code to compile.
I'm using Windows 7 and VS2010 professional on the client pc. There is no router in between the client and server (they're directly connected via ethernet).
Using the debugger, I found that I'm getting hung up at this point.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.266.66.255");
clientService.sin_port = htons(55555);
if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
else
{
printf("Client: connect() is OK.\n");
printf("Client: Can start sending and receiving data...\n");
}
I always get the "failed to connect" message, and I'm not sure why. I am using the correct IP address of the host computer (I changed it before putting on here).
If this is a bad example to use, I'm open to starting over with another example. I've tried several of the 'echo' examples commonly found online, and I'm getting similar problems. I can give more info on my overall program goals as well if that would help. The rest of the client code is below. I'm using the server code (with declarations moved around) from the same link. Thanks.
int main()
{
int m_socket;
struct sockaddr_in clientService;
int bytesSent;
int bytesRecv = SOCKET_ERROR;
// Be careful with the array bound, provide some checking mechanism...
char sendbuf[200] = "This is a test string from client";
char recvbuf[200] = "";
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Client: Error at WSAStartup().\n");
else
printf("Client: WSAStartup() is OK.\n");
// Create a socket
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
else
printf("Client: socket() is OK.\n");
// Connect to a server.
// Just test using the localhost, you can try other IP address
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("12.233.21.254");
clientService.sin_port = htons(55555);
if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
else
{
printf("Client: connect() is OK.\n");
printf("Client: Can start sending and receiving data...\n");
}
// Send and receive data.
// Receives some test string to server...
while(bytesRecv == SOCKET_ERROR)
{
bytesRecv = recv(m_socket, recvbuf, 200, 0);
if (bytesRecv == 0 || bytesRecv == WSAECONNRESET)
{
printf("Client: Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return 0;
else
{
printf("Client: recv() is OK.\n");
printf("Client: Received data is: \"%s\"\n", recvbuf);
printf("Client: Bytes received is: %ld.\n", bytesRecv);
}
}
// Sends some test data to server...
bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
if(bytesSent == SOCKET_ERROR)
printf("Client: send() error %ld.\n", WSAGetLastError());
else
{
printf("Client: send() is OK - Bytes sent: %ld\n", bytesSent);
printf("Client: The test string sent: \"%s\"\n", sendbuf);
}
WSACleanup();
return 0;
}
Your original code (after I changed the IP and port, obviously) connected to my web-server just fine, but I did tweak it a bit (below).
While it might be a bit much, CSocketServer contains a wealth of good ol' WinSock code that's been tried and held true.
Anyway, this code connected to my local web server, sent a rudimentary request and received a response.
int WSATest()
{
// Initialize Winsock.
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
{
printf("Client: Error at WSAStartup().\n");
return 0;
}
// Create a socket
SOCKET m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET)
{
printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 0;
}
else {
printf("Client: socket() is OK.\n");
}
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(9990);
//Connect to the remote peer:
if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR)
{
printf("Client: connect() - Failed to connect.\n");
WSACleanup();
return 0;
}
else
{
printf("Client: connect() is OK.\n");
printf("Client: Can start sending and receiving data...\n");
}
//Very, very basic HTTP request.
int iSendResult = send(m_socket, "GET / \n\n", 8, 0);
if(iSendResult == SOCKET_ERROR)
{
printf("Failed to send data, error %d.\n", WSAGetLastError());
return 0;
}
while(true)
{
char sRecvBuffer[200];
int iRecvResult = recv(m_socket, sRecvBuffer, sizeof(sRecvBuffer) - 1, 0);
if(iRecvResult <= SOCKET_ERROR)
{
printf("Failed to receive data, error %d.\n", WSAGetLastError());
break;
}
else if(iRecvResult == 0)
{
//Graceful disconnect.
break;
}
else {
//Be sure to terminate the buffer.
sRecvBuffer[iRecvResult] = '\0';
}
printf("Received: [%s] for [%d] bytes.\n", sRecvBuffer, iRecvResult);
}
WSACleanup();
return 0;
}
int main()
{
WSATest();
system("Pause");
}
Why not add a WSAGetLastError() to check the actual error?

Resources