// included the winsock library
#include <winsock.h>
#include <iostream.h>
#define NETWORK_ERROR -1
#define NETWORK_OK 0
int main()
{
WORD sockVersion;
WSADATA wsaData;
int error = 0;
char buffer[256];
sockVersion = MAKEWORD(2, 0);
WSAStartup(sockVersion, &wsaData);
SOCKET listeningSocket;
listeningSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listeningSocket == INVALID_SOCKET)
{
cout << WSAGetLastError() << endl;
WSACleanup();
return NETWORK_ERROR;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(8888);
error = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if (error == SOCKET_ERROR)
{
cout << WSAGetLastError() << endl;
WSACleanup();
return NETWORK_ERROR;
}
error = listen(listeningSocket, 10);
if (error == SOCKET_ERROR)
{
cout << WSAGetLastError() << endl;
WSACleanup();
return NETWORK_ERROR;
}
cout << "listening for client" << endl;
SOCKET theClient;
theClient = accept(listeningSocket, NULL, NULL);
if (theClient == INVALID_SOCKET)
{
cout << "accept(listeningSocket, NULL, NULL) failed" << endl;
WSACleanup();
return NETWORK_ERROR;
}
cout << "client connected" << endl;
error = recv(listeningSocket, buffer, 256, 0);
if (error == SOCKET_ERROR)
{
cout << WSAGetLastError() << endl;
WSACleanup();
return NETWORK_ERROR;
}
cout << buffer << endl;
closesocket(theClient);
closesocket(listeningSocket);
// Shutdown Winsock
WSACleanup();
return NETWORK_OK;
}
Let me know if you need to see the client source (I can''t imagine why you would...but who knows)
WSAENOTCONN problem
I''m just trying to make a simple program that connects two computers then sends a simple mess from one to the other. Everything works fine until I ask the server to call recv(..). I end up getting the error message WSAENOTCONN (Socket is not connected).
Here''s the source:
error = recv(listeningSocket, buffer, 256, 0);
You are trying to read from the listening socket, not the socket you got from
accept
.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement