Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fibonacci One

WSAENOTCONN problem

This topic is 5296 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
// 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)

Share this post


Link to post
Share on other sites
Advertisement
error = recv(listeningSocket, buffer, 256, 0);

You are trying to read from the listening socket, not the socket you got from accept.

Share this post


Link to post
Share on other sites
I remember looking at that and thinking something was off, but then forgot all about it a minute later.

Thanks for your help...works perfectly now.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!