c++ socket connection "Connection refused"

I'm writing a server/client application. my server works great on local ip addresses and i am able to test all of its features using 127.0.0.1 as ip address and communicate with my server. Today i used my public ip address as ip address in my server application and i configured my port forwarding and checks it with common tools on network and everyone says my port is open an actually i get port checker site packets on my server that shows everything works fine. but when i want to connect from my client application that it is written in c++(also my server is in c++) i get error code 10061 from myclient function that code is below "No connection could be made because the target machine actively refused it" :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
WSADATA WSAData;
	SOCKET server;
	SOCKADDR_IN addr;

	WSAStartup(MAKEWORD(2, 0), &WSAData);
	server = socket(AF_INET, SOCK_STREAM, 0);



	addr.sin_addr.s_addr = inet_addr("151.243.159.78");
	addr.sin_family = AF_INET;
	addr.sin_port = htons(13673);

	int err = connect(server, (SOCKADDR *)&addr, sizeof(addr));

	if (err == 0)
	{
		std::cout << "Connected to server!" << std::endl;

		char buffer[1024] = { 'h', 'e', 'l', 'l', 'o', '.' };
		send(server, buffer, sizeof(buffer), 0);
		std::cout << "Message sent!" << std::endl;

		closesocket(server);
		WSACleanup();
		std::cout << "Socket closed." << std::endl << std::endl;
	}
	else
	{
		std::cout << "Error occured : " << WSAGetLastError();
	}


	getchar();
	return 0;


and here is my server code that listen on 13673 port :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
WSADATA WSAData;

	SOCKET server, client;

	SOCKADDR_IN serverAddr, clientAddr;

	WSAStartup(MAKEWORD(2, 0), &WSAData);
	server = socket(AF_INET, SOCK_STREAM,0);

	serverAddr.sin_addr.s_addr = htons(INADDR_ANY);
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(13673);

	bind(server, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
	listen(server, SOMAXCONN);

	cout << "Listening for incoming connections..." << endl;

	char buffer[1024];
	int clientAddrSize = sizeof(clientAddr);
	if ((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
	{
		cout << "Client connected!" << endl;
		recv(client, buffer, sizeof(buffer), 0);
		cout << "Client says: " << buffer << endl;
		memset(buffer, 0, sizeof(buffer));

		closesocket(client);
		cout << "Client disconnected." << endl;
	}
	getchar();


i have to mention that i tested port checker sites from my friend home and it did send a packet to my server. also i have a wireless usb adaptor connected to my computer and i am connected with it to my modem.

i did search all the internet and everyone said that in this error common issues are you are not listening on that port or you are blocked by a firewall program but i have to say that i did both of them,it means i used -netstat -anb and it shows i am listening on port 13673 and also i configure firewall to let any connection goes through it but still no luck :(

any helps will greatly be appreciated.
Last edited on
I would think there is a problem with the routing. So that the port is open but does not route the packets to your sever correctly.

By the way: You may consider to use boost asio. Which has a working chat client/server example.
I agree, this is a routing issue my Iranian friend. Loop-back works because that is what it is for. My guess is that your router doesn't want to dial back into itself, because that's weird behavior. Try your program on two different devices, or better yet some VM's, with their internal network addresses.
Topic archived. No new replies allowed.