Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working of a DLL for a test and i keep getting socket errors.

i am trying to to connect to a server localhost, when it connects i close the server and i want it to continue to try and connect but as i open the server again it keeps disconnecting

C++
bool ISExit = false;
bool Disconnect = false;
void Networking(){
	WSAData data;
	int wsResult = WSAStartup(MAKEWORD(2, 2), &data);
	if (wsResult != 0)
	{
		std::cout << "Can't start Winsock, Err #" << wsResult << std::endl;
		return;
	}

	// Create socket
	SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
	if (sock == INVALID_SOCKET)
	{
		std::cout << "Can't create socket, Err #" << WSAGetLastError() << std::endl;
		WSACleanup();
		return;
	}

	// Fill in a hint structure
	sockaddr_in hint;
	hint.sin_family = AF_INET;
	hint.sin_port = htons(port);
	inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);

	// Connect to server
	int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
	if (connResult == SOCKET_ERROR)
	{
		std::cout << "Can't connect to server, Err #" << WSAGetLastError() << std::endl;
		closesocket(sock);
		WSACleanup();
		return;
	}

	// while loop to send and receive data
	while (!IsExit)
	{
		std::cout << "IsExit = " << std::boolalpha << IsExit << std::endl;
		std::cout << "Disconnect = " << std::boolalpha << Disconnect << std::endl;
		std::string output{ GetData(sock) };
		std::cout << output;
		if (output == "exit\n") {
			IsExit = true;
		}
		if (Disconnect) {
			IsExit = true;
		}
	}

	// Gracefully close down everything
	closesocket(sock);
	WSACleanup();
}

int main(){
    while(true){
        Networking();
    }
}


What I have tried:

i have tried closing the sock and reopening it but that didn't work and i tried to see why and for the life of me i can't see whats wrong with my code.

right i belive the problem is here
std::string GetData(SOCKET sock) {
	char buf[1024];

	int bytesReceived = recv(sock, buf, 1024, 0);
	if (bytesReceived == SOCKET_ERROR)
		std::cout << "SOCKET ERROR (GETDATA())\n";
		IsExit = true;

	return std::string(buf, 0, bytesReceived);
}
Posted
Updated 25-Apr-20 3:41am
v4
Comments
Shao Voon Wong 25-Apr-20 6:00am    
What is the console output? Disconnect might be set to true by default?
WOLF 2018 25-Apr-20 6:32am    
disconnect is set to false
[no name] 25-Apr-20 8:33am    
How is about IsExit? I don't see that you initialize it to false...
WOLF 2018 25-Apr-20 8:47am    
well it is set to false

bool IsExit = false;
while (!IsExit)
{
std::string input;
std::getline(std::cin, input);
SendData(sock, input);
std::string output{ GetData(sock) };
std::cout << output << std::endl;
if (output == "exit\n") {
IsExit = true;
}
if (Disconnect) {
IsExit = true;
}
}
[no name] 25-Apr-20 8:49am    
Ok, I only don't see that in the code you posted in the question ;)

Ok i found the problem

std::string GetData(SOCKET sock) {
	char buf[1024];

	int bytesReceived = recv(sock, buf, 1024, 0);
	if (bytesReceived == SOCKET_ERROR)
		std::cout << "SOCKET ERROR (GETDATA())\n";
		IsExit = true;

	return std::string(buf, 0, bytesReceived);
}


It works when i fix the {}

std::string GetData(SOCKET sock) {
	char buf[1024];

	int bytesReceived = recv(sock, buf, 1024, 0);
	if (bytesReceived == SOCKET_ERROR) {
		std::cout << "SOCKET ERROR (GETDATA())\n";
		IsExit = true;
	}

	return std::string(buf, 0, bytesReceived);
}


Thanks 0x0aAA for the help
 
Share this answer
 
Comments
[no name] 25-Apr-20 9:45am    
Great you could solve it. I don't think I was a big help :-)
Because IsExit is global (whyever) you need to set it to false each time before the loop, otherwhise IsExit reamains true after calling Networking once:
...
// while loop to send and receive data
IsExit= false;
while (!IsExit)
{
   ...
}
 
Share this answer
 
Comments
WOLF 2018 25-Apr-20 9:16am    
i tried that and it still does the samething
[no name] 25-Apr-20 9:20am    
Strange. So I think you need to debug your code to find the reason.
WOLF 2018 25-Apr-20 9:23am    
because its a DLL i am unable to debug it in VS 2019
WOLF 2018 25-Apr-20 9:18am    
this is what i get from the server

Ncat: Version 5.59BETA1 ( http://nmap.org/ncat )
Ncat: Listening on 0.0.0.0:3000
Ncat: Connection from 127.0.0.1:53169.
hello
Ncat: Version 5.59BETA1 ( http://nmap.org/ncat )
Ncat: Listening on 0.0.0.0:3000
Ncat: Connection from 127.0.0.1:53170.

and the client output is

Can't connect to server, Err #10061
Can't connect to server, Err #10061
IsExit = false
Disconnect = false
hello
IsExit = false
Disconnect = false
SOCKET ERROR (GETDATA()) // thats because i disconnected
[no name] 25-Apr-20 9:23am    
Looks like I misunderstand you, I thought first run of Networking was successful. So can you confirm, even first run of Networking fails?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900